all repos — mgba @ 3b24e94018c2f26f503db72b058444882fff4ccf

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