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 (*hitIllegal)(struct LR35902Core* cpu);
66 void (*hitStub)(struct LR35902Core* cpu);
67};
68
69// TODO: Merge with ARMComponent?
70struct LR35902Component {
71 uint32_t id;
72 void (*init)(struct LR35902Core* cpu, struct LR35902Component* component);
73 void (*deinit)(struct LR35902Component* component);
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 uint16_t irqVector;
123
124 struct LR35902Memory memory;
125 struct LR35902InterruptHandler irqh;
126
127 struct LR35902Component* master;
128
129 size_t numComponents;
130 struct LR35902Component** components;
131};
132
133static inline uint16_t LR35902ReadHL(struct LR35902Core* cpu) {
134 uint16_t hl;
135 LOAD_16LE(hl, 0, &cpu->hl);
136 return hl;
137}
138
139static inline void LR35902WriteHL(struct LR35902Core* cpu, uint16_t hl) {
140 STORE_16LE(hl, 0, &cpu->hl);
141}
142
143static inline uint16_t LR35902ReadBC(struct LR35902Core* cpu) {
144 uint16_t bc;
145 LOAD_16LE(bc, 0, &cpu->bc);
146 return bc;
147}
148
149static inline void LR35902WriteBC(struct LR35902Core* cpu, uint16_t bc) {
150 STORE_16LE(bc, 0, &cpu->bc);
151}
152
153static inline uint16_t LR35902ReadDE(struct LR35902Core* cpu) {
154 uint16_t de;
155 LOAD_16LE(de, 0, &cpu->de);
156 return de;
157}
158
159static inline void LR35902WriteDE(struct LR35902Core* cpu, uint16_t de) {
160 STORE_16LE(de, 0, &cpu->de);
161}
162
163void LR35902Init(struct LR35902Core* cpu);
164void LR35902Deinit(struct LR35902Core* cpu);
165void LR35902SetComponents(struct LR35902Core* cpu, struct LR35902Component* master, int extra, struct LR35902Component** extras);
166void LR35902HotplugAttach(struct LR35902Core* cpu, size_t slot);
167void LR35902HotplugDetach(struct LR35902Core* cpu, size_t slot);
168
169void LR35902Reset(struct LR35902Core* cpu);
170void LR35902RaiseIRQ(struct LR35902Core* cpu, uint8_t vector);
171
172void LR35902Tick(struct LR35902Core* cpu);
173
174#endif