summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2018-06-22 20:33:08 -0400
committerPaul Duncan <pabs@pablotron.org>2018-06-22 20:33:08 -0400
commitc5615c88bd1a1db2088f52c8ba28dc3182ea245e (patch)
tree09c3ea6259aa54fa53fd034fc3aa9a94a3da609b
parenta256ef0d7b38b7026c396f2d90ec31fb2c816df2 (diff)
downloadgb-c-c5615c88bd1a1db2088f52c8ba28dc3182ea245e.tar.bz2
gb-c-c5615c88bd1a1db2088f52c8ba28dc3182ea245e.zip
clean up states
-rw-r--r--gb.h12
-rwxr-xr-xgen.rb4
-rw-r--r--ops.yaml61
3 files changed, 33 insertions, 44 deletions
diff --git a/gb.h b/gb.h
index 1dab2e2..44f80b0 100644
--- a/gb.h
+++ b/gb.h
@@ -8,12 +8,12 @@ extern "C" {
#include <stdint.h>
typedef enum {
- STATE_RUN,
- STATE_STOP,
- STATE_HALT,
- STATE_INVALID,
- STATE_NOT_IMPLEMENTED,
- STATE_LAST,
+ GB_CPU_STATE_RUN,
+ GB_CPU_STATE_STOP,
+ GB_CPU_STATE_HALT,
+ GB_CPU_STATE_INVALID,
+ GB_CPU_STATE_NOT_IMPLEMENTED,
+ GB_CPU_STATE_LAST,
} gb_cpu_state_t;
#define GB_BTN_UP 1
diff --git a/gen.rb b/gen.rb
index 315be3e..4b468d5 100755
--- a/gen.rb
+++ b/gen.rb
@@ -18,7 +18,7 @@ DATA['ops'].each do |set_id, ops|
when 'XX'
[
"case 0x%s: /* op: %s, cat: %s */",
- " invalid(ctx, old_pc);",
+ " cpu_set_state(ctx, old_pc, GB_CPU_STATE_INVALID);",
" break;",
].join("\n") % [
hex,
@@ -55,7 +55,7 @@ DATA['ops'].each do |set_id, ops|
hex,
op['id'],
op['cat'],
- op['code'] || 'not_implemented(ctx, old_pc, op);',
+ op['code'] || 'cpu_set_state(ctx, old_pc, GB_CPU_STATE_NOT_IMPLEMENTED);',
pc_expr,
time_expr,
]
diff --git a/ops.yaml b/ops.yaml
index 4d82cce..1a14607 100644
--- a/ops.yaml
+++ b/ops.yaml
@@ -266,8 +266,7 @@ ops:
h:
c:
code: |
- ctx->cpu.state = STATE_STOP;
- cpu_ww(ctx, RW_PC, old_pc); // FIXME?
+ cpu_set_state(ctx, old_pc, GB_CPU_STATE_STOP);
- id: LD DE, d16
hex: 0x11
cat: "16-bit load/store/move"
@@ -1900,7 +1899,7 @@ ops:
h:
c:
code: |
- ctx->cpu.state = STATE_HALT;
+ cpu_set_state(ctx, old_pc, GB_CPU_STATE_HALT);
- id: LD (HL), A
hex: 0x77
cat: "8-bit load/store/move"
@@ -3402,7 +3401,7 @@ ops:
cat: "invalid"
op: XX
code: |
- invalid(ctx, old_pc);
+ cpu_set_state(ctx, old_pc, GB_CPU_STATE_INVALID);
- id: CALL NC, a16
hex: 0xD4
cat: "jumps/calls"
@@ -3543,7 +3542,7 @@ ops:
cat: "invalid"
op: XX
code: |
- invalid(ctx, old_pc);
+ cpu_set_state(ctx, old_pc, GB_CPU_STATE_INVALID);
- id: CALL C, a16
hex: 0xDC
cat: "jumps/calls"
@@ -3586,7 +3585,7 @@ ops:
h: H
c: C
code: |
- invalid(ctx, old_pc);
+ cpu_set_state(ctx, old_pc, GB_CPU_STATE_INVALID);
- id: RST 18H
hex: 0xDF
cat: "jumps/calls"
@@ -3661,7 +3660,7 @@ ops:
cat: "invalid"
op: XX
code: |
- invalid(ctx, old_pc);
+ cpu_set_state(ctx, old_pc, GB_CPU_STATE_INVALID);
- id: PUSH HL
hex: 0xE5
cat: "16-bit load/store/move"
@@ -3759,7 +3758,7 @@ ops:
cat: "invalid"
op: XX
code: |
- invalid(ctx, old_pc);
+ cpu_set_state(ctx, old_pc, GB_CPU_STATE_INVALID);
- id: XX
hex: 0xEC
cat: "invalid"
@@ -3769,7 +3768,7 @@ ops:
cat: "invalid"
op: XX
code: |
- invalid(ctx, old_pc);
+ cpu_set_state(ctx, old_pc, GB_CPU_STATE_INVALID);
- id: XOR d8
hex: 0xEE
cat: "8-bit math"
@@ -3869,7 +3868,7 @@ ops:
cat: "invalid"
op: XX
code: |
- invalid(ctx, old_pc);
+ cpu_set_state(ctx, old_pc, GB_CPU_STATE_INVALID);
- id: PUSH AF
hex: 0xF5
cat: "16-bit load/store/move"
@@ -3985,13 +3984,13 @@ ops:
cat: "invalid"
op: XX
code: |
- invalid(ctx, old_pc);
+ cpu_set_state(ctx, old_pc, GB_CPU_STATE_INVALID);
- id: XX
hex: 0xFD
cat: "invalid"
op: XX
code: |
- invalid(ctx, old_pc);
+ cpu_set_state(ctx, old_pc, GB_CPU_STATE_INVALID);
- id: CP d8
hex: 0xFE
cat: "8-bit math"
@@ -9142,23 +9141,13 @@ templates:
}
static void
- invalid(
- gb_t * const ctx,
- const uint16_t pc
- ) {
- ctx->cpu.state = STATE_INVALID;
- cpu_ww(ctx, RW_PC, pc); // FIXME?
- }
-
- static void
- not_implemented(
+ cpu_set_state(
gb_t * const ctx,
const uint16_t pc,
- const uint16_t op
+ const gb_cpu_state_t state
) {
- UNUSED(op);
- ctx->cpu.state = STATE_NOT_IMPLEMENTED;
- cpu_ww(ctx, RW_PC, pc); // FIXME?
+ ctx->cpu.state = state;
+ UNUSED(pc);
}
static void
@@ -10219,7 +10208,7 @@ templates:
cpu_step(
gb_t * const ctx
) {
- if (ctx->cpu.state == STATE_RUN) {
+ if (ctx->cpu.state == GB_CPU_STATE_RUN) {
// get pc, and current op
const uint16_t old_pc = cpu_rw(ctx, RW_PC),
op = mmu_rb(ctx, old_pc);
@@ -10232,12 +10221,12 @@ templates:
switch (mmu_rb(ctx, old_pc + 1)) {
<%= switches['cb'] %>
default:
- not_implemented(ctx, old_pc, op);
+ cpu_set_state(ctx, old_pc, GB_CPU_STATE_NOT_IMPLEMENTED);
}
break;
default:
- not_implemented(ctx, old_pc, op);
+ cpu_set_state(ctx, old_pc, GB_CPU_STATE_NOT_IMPLEMENTED);
}
// increment cpu clock
@@ -10245,7 +10234,7 @@ templates:
// return clock
return clock;
- } else if (ctx->cpu.state == STATE_HALT || ctx->cpu.state == STATE_STOP) {
+ } else if (ctx->cpu.state == GB_CPU_STATE_HALT || ctx->cpu.state == GB_CPU_STATE_STOP) {
// return 4 clock step
return 4;
} else {
@@ -10258,7 +10247,7 @@ templates:
cpu_handle_interrupts(
gb_t * const ctx
) {
- if (ctx->cpu.state == STATE_RUN) {
+ if (ctx->cpu.state == GB_CPU_STATE_RUN) {
if (ctx->cpu.ime) {
// get interrupt vector (masked against enabled interrupts)
const uint8_t iv = mmu_rb(ctx, PORT_IE) & mmu_rb(ctx, PORT_IF);
@@ -10286,7 +10275,7 @@ templates:
}
}
}
- } else if (ctx->cpu.state == STATE_HALT) {
+ } else if (ctx->cpu.state == GB_CPU_STATE_HALT) {
// get interrupt vector (masked against enabled interrupts)
const uint8_t iv = mmu_rb(ctx, PORT_IE) & mmu_rb(ctx, PORT_IF);
bool done = false;
@@ -10302,7 +10291,7 @@ templates:
// (TCAGBD.pdf, 4.9)
// wake cpu
- ctx->cpu.state = STATE_RUN;
+ cpu_set_state(ctx, 0, GB_CPU_STATE_RUN);
// FIXME: according to url below, this should take 5 cycles
// (src: http://gbdev.gg8.se/wiki/articles/Interrupts)
@@ -10322,8 +10311,8 @@ templates:
}
}
}
- } else if (ctx->cpu.state == STATE_STOP) {
- // TODO: handle STATE_STOP
+ } else if (ctx->cpu.state == GB_CPU_STATE_STOP) {
+ // TODO: handle GB_CPU_STATE_STOP
} else {
}
}
@@ -10384,7 +10373,7 @@ templates:
gb_t * const ctx
) {
// set cpu state
- ctx->cpu.state = STATE_RUN;
+ cpu_set_state(ctx, 0, GB_CPU_STATE_RUN);
// init cpu registers
// (src: TCAGBD.pdf, 3.2)