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