all repos — mgba @ 4e64b4fde474693d6175335efbc142c98e08c163

mGBA Game Boy Advance Emulator

src/gb/io.c (view raw)

  1/* Copyright (c) 2013-2016 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#include "io.h"
  7
  8#include "gb/gb.h"
  9
 10void GBIOInit(struct GB* gb) {
 11	memset(gb->memory.io, 0, sizeof(gb->memory.io));
 12}
 13
 14void GBIOReset(struct GB* gb) {
 15	memset(gb->memory.io, 0, sizeof(gb->memory.io));
 16
 17	GBIOWrite(gb, 0x05, 0);
 18	GBIOWrite(gb, 0x06, 0);
 19	GBIOWrite(gb, 0x07, 0);
 20	GBIOWrite(gb, 0x10, 0x80);
 21	GBIOWrite(gb, 0x11, 0xBF);
 22	GBIOWrite(gb, 0x12, 0xF3);
 23	GBIOWrite(gb, 0x12, 0xF3);
 24	GBIOWrite(gb, 0x14, 0xBF);
 25	GBIOWrite(gb, 0x16, 0x3F);
 26	GBIOWrite(gb, 0x17, 0x00);
 27	GBIOWrite(gb, 0x19, 0xBF);
 28	GBIOWrite(gb, 0x1A, 0x7F);
 29	GBIOWrite(gb, 0x1B, 0xFF);
 30	GBIOWrite(gb, 0x1C, 0x9F);
 31	GBIOWrite(gb, 0x1E, 0xBF);
 32	GBIOWrite(gb, 0x20, 0xFF);
 33	GBIOWrite(gb, 0x21, 0x00);
 34	GBIOWrite(gb, 0x22, 0x00);
 35	GBIOWrite(gb, 0x23, 0xBF);
 36	GBIOWrite(gb, 0x24, 0x77);
 37	GBIOWrite(gb, 0x25, 0xF3);
 38	GBIOWrite(gb, 0x26, 0xF1);
 39	GBIOWrite(gb, 0x40, 0x91);
 40	GBIOWrite(gb, 0x42, 0x00);
 41	GBIOWrite(gb, 0x43, 0x00);
 42	GBIOWrite(gb, 0x45, 0x00);
 43	GBIOWrite(gb, 0x47, 0xFC);
 44	GBIOWrite(gb, 0x48, 0xFF);
 45	GBIOWrite(gb, 0x49, 0xFF);
 46	GBIOWrite(gb, 0x4A, 0x00);
 47	GBIOWrite(gb, 0x4B, 0x00);
 48	GBIOWrite(gb, 0xFF, 0x00);
 49}
 50
 51void GBIOWrite(struct GB* gb, unsigned address, uint8_t value) {
 52	switch (address) {
 53	case REG_IF:
 54		gb->memory.io[REG_IF] = value;
 55		GBUpdateIRQs(gb);
 56		return;
 57	case REG_LCDC:
 58		// TODO: handle GBC differences
 59		value = gb->video.renderer->writeVideoRegister(gb->video.renderer, address, value);
 60		GBVideoWriteLCDC(&gb->video, value);
 61		break;
 62	case REG_SCY:
 63	case REG_SCX:
 64	case REG_WY:
 65	case REG_WX:
 66	case REG_BGP:
 67	case REG_OBP0:
 68	case REG_OBP1:
 69		value = gb->video.renderer->writeVideoRegister(gb->video.renderer, address, value);
 70		break;
 71	case REG_STAT:
 72		GBVideoWriteSTAT(&gb->video, value);
 73		break;
 74	case REG_IE:
 75		gb->memory.ie = value;
 76		GBUpdateIRQs(gb);
 77		return;
 78	default:
 79		// TODO: Log
 80		if (address >= GB_SIZE_IO) {
 81			return;
 82		}
 83		break;
 84	}
 85	gb->memory.io[address] = value;
 86}
 87
 88static uint8_t _readKeys(struct GB* gb) {
 89	uint8_t keys = *gb->keySource;
 90	switch (gb->memory.io[REG_JOYP] & 0x30) {
 91	case 0x10:
 92		keys >>= 4;
 93		break;
 94	case 0x20:
 95		break;
 96	default:
 97		// ???
 98		break;
 99	}
100	return (gb->memory.io[REG_JOYP] | 0xF) ^ (keys & 0xF);
101}
102
103uint8_t GBIORead(struct GB* gb, unsigned address) {
104	switch (address) {
105	case REG_JOYP:
106		return _readKeys(gb);
107	case REG_IF:
108		break;
109	case REG_IE:
110		return gb->memory.ie;
111	default:
112		// TODO: Log
113		if (address >= GB_SIZE_IO) {
114			return 0;
115		}
116		break;
117	}
118	return gb->memory.io[address];
119}