all repos — mgba @ 9170dd26daebb375884c48817c50bc8208a18cc6

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, 1)
 36
 37enum LR35902ExecutionState {
 38	LR35902_CORE_FETCH = 0,
 39	LR35902_CORE_DECODE,
 40	LR35902_CORE_STALL,
 41	LR35902_CORE_EXECUTE,
 42
 43	LR35902_CORE_MEMORY_LOAD = 5,
 44	LR35902_CORE_MEMORY_STORE = 9,
 45	LR35902_CORE_MEMORY_MOVE_INDEX_LOAD,
 46	LR35902_CORE_MEMORY_MOVE_INDEX_STORE,
 47	LR35902_CORE_READ_PC,
 48	LR35902_CORE_READ_PC_STALL,
 49};
 50
 51struct LR35902Memory {
 52	uint16_t (*load16)(struct LR35902Core*, uint16_t address);
 53	uint8_t (*load8)(struct LR35902Core*, uint16_t address);
 54
 55	void (*store16)(struct LR35902Core*, uint16_t address, int16_t value);
 56	void (*store8)(struct LR35902Core*, uint16_t address, int8_t value);
 57
 58	uint8_t* activeRegion;
 59	uint16_t activeMask;
 60	void (*setActiveRegion)(struct LR35902Core*, uint16_t address);
 61};
 62
 63struct LR35902InterruptHandler {
 64	void (*reset)(struct LR35902Core* cpu);
 65	void (*processEvents)(struct LR35902Core* cpu);
 66	void (*setInterrupts)(struct LR35902Core* cpu, bool enable);
 67
 68	void (*hitStub)(struct LR35902Core* cpu);
 69};
 70
 71// TODO: Merge with ARMComponent?
 72struct LR35902Component {
 73	uint32_t id;
 74	void (*init)(struct LR35902Core* cpu, struct LR35902Component* component);
 75	void (*deinit)(struct LR35902Component* component);
 76};
 77
 78struct LR35902Core {
 79#pragma pack(push, 1)
 80	union {
 81		struct {
 82			union FlagRegister f;
 83			uint8_t a;
 84		};
 85		uint16_t af;
 86	};
 87#pragma pack(pop, 1)
 88	union {
 89		struct {
 90			uint8_t c;
 91			uint8_t b;
 92		};
 93		uint16_t bc;
 94	};
 95	union {
 96		struct {
 97			uint8_t e;
 98			uint8_t d;
 99		};
100		uint16_t de;
101	};
102	union {
103		struct {
104			uint8_t l;
105			uint8_t h;
106		};
107		uint16_t hl;
108	};
109	uint16_t sp;
110	uint16_t pc;
111
112	uint16_t index;
113
114	int32_t cycles;
115	int32_t nextEvent;
116	enum LR35902ExecutionState executionState;
117	int halted;
118
119	uint8_t bus;
120	bool condition;
121	LR35902Instruction instruction;
122
123	bool irqPending;
124	uint16_t irqVector;
125
126	struct LR35902Memory memory;
127	struct LR35902InterruptHandler irqh;
128
129	struct LR35902Component* master;
130
131	size_t numComponents;
132	struct LR35902Component** components;
133};
134
135static inline uint16_t LR35902ReadHL(struct LR35902Core* cpu) {
136	uint16_t hl;
137	LOAD_16LE(hl, 0, &cpu->hl);
138	return hl;
139}
140
141void LR35902Init(struct LR35902Core* cpu);
142void LR35902Deinit(struct LR35902Core* cpu);
143void LR35902SetComponents(struct LR35902Core* cpu, struct LR35902Component* master, int extra, struct LR35902Component** extras);
144void LR35902HotplugAttach(struct LR35902Core* cpu, size_t slot);
145void LR35902HotplugDetach(struct LR35902Core* cpu, size_t slot);
146
147void LR35902Reset(struct LR35902Core* cpu);
148void LR35902RaiseIRQ(struct LR35902Core* cpu, uint8_t vector);
149
150void LR35902Tick(struct LR35902Core* cpu);
151
152#endif