src/lr35902/lr35902.c (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#include "lr35902.h"
7
8#include "isa-lr35902.h"
9
10void LR35902Init(struct LR35902Core* cpu) {
11 cpu->master->init(cpu, cpu->master);
12 size_t i;
13 for (i = 0; i < cpu->numComponents; ++i) {
14 if (cpu->components[i] && cpu->components[i]->init) {
15 cpu->components[i]->init(cpu, cpu->components[i]);
16 }
17 }
18}
19
20void LR35902Deinit(struct LR35902Core* cpu) {
21 if (cpu->master->deinit) {
22 cpu->master->deinit(cpu->master);
23 }
24 size_t i;
25 for (i = 0; i < cpu->numComponents; ++i) {
26 if (cpu->components[i] && cpu->components[i]->deinit) {
27 cpu->components[i]->deinit(cpu->components[i]);
28 }
29 }
30}
31
32void LR35902SetComponents(struct LR35902Core* cpu, struct LR35902Component* master, int extra, struct LR35902Component** extras) {
33 cpu->master = master;
34 cpu->numComponents = extra;
35 cpu->components = extras;
36}
37
38
39void LR35902HotplugAttach(struct LR35902Core* cpu, size_t slot) {
40 if (slot >= cpu->numComponents) {
41 return;
42 }
43 cpu->components[slot]->init(cpu, cpu->components[slot]);
44}
45
46void LR35902HotplugDetach(struct LR35902Core* cpu, size_t slot) {
47 if (slot >= cpu->numComponents) {
48 return;
49 }
50 cpu->components[slot]->deinit(cpu->components[slot]);
51}
52
53void LR35902Reset(struct LR35902Core* cpu) {
54 cpu->af = 0;
55 cpu->bc = 0;
56 cpu->de = 0;
57 cpu->hl = 0;
58
59 cpu->sp = 0;
60 cpu->pc = 0;
61
62 cpu->instruction = 0;
63
64 cpu->cycles = 0;
65 cpu->nextEvent = 0;
66 cpu->executionState = LR35902_CORE_FETCH;
67 cpu->halted = 0;
68
69 cpu->irqh.reset(cpu);
70}
71
72void LR35902RaiseIRQ(struct LR35902Core* cpu, uint8_t vector) {
73 cpu->irqPending = true;
74 cpu->irqVector = vector;
75}
76
77void LR35902Tick(struct LR35902Core* cpu) {
78 ++cpu->cycles;
79 enum LR35902ExecutionState state = cpu->executionState;
80 ++cpu->executionState;
81 cpu->executionState &= 3;
82 switch (state) {
83 case LR35902_CORE_FETCH:
84 if (cpu->irqPending) {
85 cpu->pc = cpu->irqVector;
86 cpu->irqPending = false;
87 cpu->irqh.setInterrupts(cpu, false);
88 // TODO: stall
89 }
90 cpu->bus = cpu->memory.load8(cpu, cpu->pc);
91 break;
92 case LR35902_CORE_DECODE:
93 cpu->instruction = _lr35902InstructionTable[cpu->bus];
94 ++cpu->pc;
95 break;
96 case LR35902_CORE_EXECUTE:
97 cpu->instruction(cpu);
98 break;
99 case LR35902_CORE_MEMORY_LOAD:
100 cpu->bus = cpu->memory.load8(cpu, cpu->index);
101 break;
102 case LR35902_CORE_MEMORY_STORE:
103 cpu->memory.store8(cpu, cpu->index, cpu->bus);
104 break;
105 case LR35902_CORE_READ_PC:
106 cpu->bus = cpu->memory.load8(cpu, cpu->pc);
107 ++cpu->pc;
108 cpu->executionState = LR35902_CORE_READ_PC_STALL;
109 break;
110 case LR35902_CORE_MEMORY_MOVE_INDEX_LOAD:
111 cpu->executionState = LR35902_CORE_MEMORY_LOAD;
112 break;
113 case LR35902_CORE_MEMORY_MOVE_INDEX_STORE:
114 cpu->executionState = LR35902_CORE_MEMORY_STORE;
115 break;
116 case LR35902_CORE_READ_PC_STALL:
117 case LR35902_CORE_STALL:
118 break;
119 }
120 if (cpu->cycles >= cpu->nextEvent) {
121 cpu->irqh.processEvents(cpu);
122 }
123}