all repos — mgba @ 20b2e8af7e266f01501a123692da07bedf574cdb

mGBA Game Boy Advance Emulator

src/lr35902/lr35902.h (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#ifndef LR35902_H
  7#define LR35902_H
  8
  9#include "util/common.h"
 10
 11#include "lr35902/isa-lr35902.h"
 12
 13struct LR35902Core;
 14
 15#pragma pack(push, 1)
 16union FlagRegister {
 17	struct {
 18#if defined(__POWERPC__) || defined(__PPC__)
 19		unsigned z : 1;
 20		unsigned n : 1;
 21		unsigned h : 1;
 22		unsigned c : 1;
 23		unsigned : 4;
 24#else
 25		unsigned : 4;
 26		unsigned c : 1;
 27		unsigned h : 1;
 28		unsigned n : 1;
 29		unsigned z : 1;
 30#endif
 31	};
 32
 33	uint8_t packed;
 34};
 35#pragma pack(pop)
 36
 37enum LR35902ExecutionState {
 38	LR35902_CORE_FETCH = 0,
 39	LR35902_CORE_IDLE_0,
 40	LR35902_CORE_IDLE_1,
 41	LR35902_CORE_EXECUTE = 3,
 42
 43	LR35902_CORE_MEMORY_LOAD = 4,
 44	LR35902_CORE_MEMORY_STORE = 8,
 45	LR35902_CORE_READ_PC = 12,
 46	LR35902_CORE_STALL = 16,
 47	LR35902_CORE_OP2 = 20
 48};
 49
 50struct LR35902Memory {
 51	uint8_t (*load8)(struct LR35902Core*, uint16_t address);
 52	void (*store8)(struct LR35902Core*, uint16_t address, int8_t value);
 53
 54	uint8_t* activeRegion;
 55	uint16_t activeMask;
 56	void (*setActiveRegion)(struct LR35902Core*, uint16_t address);
 57};
 58
 59struct LR35902InterruptHandler {
 60	void (*reset)(struct LR35902Core* cpu);
 61	void (*processEvents)(struct LR35902Core* cpu);
 62	void (*setInterrupts)(struct LR35902Core* cpu, bool enable);
 63	void (*halt)(struct LR35902Core* cpu);
 64
 65	void (*hitStub)(struct LR35902Core* cpu);
 66};
 67
 68// TODO: Merge with ARMComponent?
 69struct LR35902Component {
 70	uint32_t id;
 71	void (*init)(struct LR35902Core* cpu, struct LR35902Component* component);
 72	void (*deinit)(struct LR35902Component* component);
 73};
 74
 75struct LR35902Core {
 76#pragma pack(push, 1)
 77	union {
 78		struct {
 79			union FlagRegister f;
 80			uint8_t a;
 81		};
 82		uint16_t af;
 83	};
 84#pragma pack(pop)
 85	union {
 86		struct {
 87			uint8_t c;
 88			uint8_t b;
 89		};
 90		uint16_t bc;
 91	};
 92	union {
 93		struct {
 94			uint8_t e;
 95			uint8_t d;
 96		};
 97		uint16_t de;
 98	};
 99	union {
100		struct {
101			uint8_t l;
102			uint8_t h;
103		};
104		uint16_t hl;
105	};
106	uint16_t sp;
107	uint16_t pc;
108
109	uint16_t index;
110
111	int32_t cycles;
112	int32_t nextEvent;
113	enum LR35902ExecutionState executionState;
114	bool halted;
115
116	uint8_t bus;
117	bool condition;
118	LR35902Instruction instruction;
119
120	bool irqPending;
121	uint16_t irqVector;
122
123	struct LR35902Memory memory;
124	struct LR35902InterruptHandler irqh;
125
126	struct LR35902Component* master;
127
128	size_t numComponents;
129	struct LR35902Component** components;
130};
131
132static inline uint16_t LR35902ReadHL(struct LR35902Core* cpu) {
133	uint16_t hl;
134	LOAD_16LE(hl, 0, &cpu->hl);
135	return hl;
136}
137
138static inline void LR35902WriteHL(struct LR35902Core* cpu, uint16_t hl) {
139	STORE_16LE(hl, 0, &cpu->hl);
140}
141
142static inline uint16_t LR35902ReadBC(struct LR35902Core* cpu) {
143	uint16_t bc;
144	LOAD_16LE(bc, 0, &cpu->bc);
145	return bc;
146}
147
148static inline void LR35902WriteBC(struct LR35902Core* cpu, uint16_t bc) {
149	STORE_16LE(bc, 0, &cpu->bc);
150}
151
152static inline uint16_t LR35902ReadDE(struct LR35902Core* cpu) {
153	uint16_t de;
154	LOAD_16LE(de, 0, &cpu->de);
155	return de;
156}
157
158static inline void LR35902WriteDE(struct LR35902Core* cpu, uint16_t de) {
159	STORE_16LE(de, 0, &cpu->de);
160}
161
162void LR35902Init(struct LR35902Core* cpu);
163void LR35902Deinit(struct LR35902Core* cpu);
164void LR35902SetComponents(struct LR35902Core* cpu, struct LR35902Component* master, int extra, struct LR35902Component** extras);
165void LR35902HotplugAttach(struct LR35902Core* cpu, size_t slot);
166void LR35902HotplugDetach(struct LR35902Core* cpu, size_t slot);
167
168void LR35902Reset(struct LR35902Core* cpu);
169void LR35902RaiseIRQ(struct LR35902Core* cpu, uint8_t vector);
170
171void LR35902Tick(struct LR35902Core* cpu);
172
173#endif