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 uint16_t (*load16)(struct LR35902Core*, uint16_t address);
52 uint8_t (*load8)(struct LR35902Core*, uint16_t address);
53
54 void (*store16)(struct LR35902Core*, uint16_t address, int16_t value);
55 void (*store8)(struct LR35902Core*, uint16_t address, int8_t value);
56
57 uint8_t* activeRegion;
58 uint16_t activeMask;
59 void (*setActiveRegion)(struct LR35902Core*, uint16_t address);
60};
61
62struct LR35902InterruptHandler {
63 void (*reset)(struct LR35902Core* cpu);
64 void (*processEvents)(struct LR35902Core* cpu);
65 void (*setInterrupts)(struct LR35902Core* cpu, bool enable);
66 void (*halt)(struct LR35902Core* cpu);
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)
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 bool 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
141static inline void LR35902WriteHL(struct LR35902Core* cpu, uint16_t hl) {
142 STORE_16LE(hl, 0, &cpu->hl);
143}
144
145static inline uint16_t LR35902ReadBC(struct LR35902Core* cpu) {
146 uint16_t bc;
147 LOAD_16LE(bc, 0, &cpu->bc);
148 return bc;
149}
150
151static inline void LR35902WriteBC(struct LR35902Core* cpu, uint16_t bc) {
152 STORE_16LE(bc, 0, &cpu->bc);
153}
154
155static inline uint16_t LR35902ReadDE(struct LR35902Core* cpu) {
156 uint16_t de;
157 LOAD_16LE(de, 0, &cpu->de);
158 return de;
159}
160
161static inline void LR35902WriteDE(struct LR35902Core* cpu, uint16_t de) {
162 STORE_16LE(de, 0, &cpu->de);
163}
164
165void LR35902Init(struct LR35902Core* cpu);
166void LR35902Deinit(struct LR35902Core* cpu);
167void LR35902SetComponents(struct LR35902Core* cpu, struct LR35902Component* master, int extra, struct LR35902Component** extras);
168void LR35902HotplugAttach(struct LR35902Core* cpu, size_t slot);
169void LR35902HotplugDetach(struct LR35902Core* cpu, size_t slot);
170
171void LR35902Reset(struct LR35902Core* cpu);
172void LR35902RaiseIRQ(struct LR35902Core* cpu, uint8_t vector);
173
174void LR35902Tick(struct LR35902Core* cpu);
175
176#endif