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