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
11CXX_GUARD_START
12
13#include "core/cpu.h"
14#include "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 : 4;
27#else
28 unsigned : 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 uint8_t* activeRegion;
58 uint16_t activeMask;
59 uint16_t activeRegionEnd;
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 void (*halt)(struct LR35902Core* cpu);
68 void (*stop)(struct LR35902Core* cpu);
69
70 void (*hitIllegal)(struct LR35902Core* cpu);
71};
72
73struct LR35902Core {
74#pragma pack(push, 1)
75 union {
76 struct {
77 union FlagRegister f;
78 uint8_t a;
79 };
80 uint16_t af;
81 };
82#pragma pack(pop)
83 union {
84 struct {
85 uint8_t c;
86 uint8_t b;
87 };
88 uint16_t bc;
89 };
90 union {
91 struct {
92 uint8_t e;
93 uint8_t d;
94 };
95 uint16_t de;
96 };
97 union {
98 struct {
99 uint8_t l;
100 uint8_t h;
101 };
102 uint16_t hl;
103 };
104 uint16_t sp;
105 uint16_t pc;
106
107 uint16_t index;
108
109 int32_t cycles;
110 int32_t nextEvent;
111 enum LR35902ExecutionState executionState;
112 bool halted;
113
114 uint8_t bus;
115 bool condition;
116 LR35902Instruction instruction;
117
118 bool irqPending;
119 uint16_t irqVector;
120
121 struct LR35902Memory memory;
122 struct LR35902InterruptHandler irqh;
123
124 struct mCPUComponent* master;
125
126 size_t numComponents;
127 struct mCPUComponent** components;
128};
129
130static inline uint16_t LR35902ReadHL(struct LR35902Core* cpu) {
131 uint16_t hl;
132 LOAD_16LE(hl, 0, &cpu->hl);
133 return hl;
134}
135
136static inline void LR35902WriteHL(struct LR35902Core* cpu, uint16_t hl) {
137 STORE_16LE(hl, 0, &cpu->hl);
138}
139
140static inline uint16_t LR35902ReadBC(struct LR35902Core* cpu) {
141 uint16_t bc;
142 LOAD_16LE(bc, 0, &cpu->bc);
143 return bc;
144}
145
146static inline void LR35902WriteBC(struct LR35902Core* cpu, uint16_t bc) {
147 STORE_16LE(bc, 0, &cpu->bc);
148}
149
150static inline uint16_t LR35902ReadDE(struct LR35902Core* cpu) {
151 uint16_t de;
152 LOAD_16LE(de, 0, &cpu->de);
153 return de;
154}
155
156static inline void LR35902WriteDE(struct LR35902Core* cpu, uint16_t de) {
157 STORE_16LE(de, 0, &cpu->de);
158}
159
160void LR35902Init(struct LR35902Core* cpu);
161void LR35902Deinit(struct LR35902Core* cpu);
162void LR35902SetComponents(struct LR35902Core* cpu, struct mCPUComponent* master, int extra, struct mCPUComponent** extras);
163void LR35902HotplugAttach(struct LR35902Core* cpu, size_t slot);
164void LR35902HotplugDetach(struct LR35902Core* cpu, size_t slot);
165
166void LR35902Reset(struct LR35902Core* cpu);
167void LR35902RaiseIRQ(struct LR35902Core* cpu, uint8_t vector);
168
169void LR35902Tick(struct LR35902Core* cpu);
170void LR35902Run(struct LR35902Core* cpu);
171
172CXX_GUARD_END
173
174#endif