-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.c
60 lines (45 loc) · 1.73 KB
/
controller.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "controller.h"
#include "memory.h"
#include "mos6502.h"
void mos6502_controller_reset(mos6502_t * mos6502) {
size_t i;
/* Pull RES low */
mos6502_set_pin_res(mos6502, BIT_ZERO, false);
/* Neutralize other inputs */
mos6502_set_pin_clk(mos6502, BIT_ONE, false);
mos6502_set_pin_rdy(mos6502, BIT_ONE, false);
mos6502_set_pin_so(mos6502, BIT_ZERO, false);
mos6502_set_pin_irq(mos6502, BIT_ONE, false);
mos6502_set_pin_nmi(mos6502, BIT_ONE, false);
/* Sync all inputs */
mos6502_sync(mos6502);
/* Toggle CLK input for 8 full cycles */
for (i = 0; i < 16; i++) {
mos6502_set_pin_clk(mos6502, !mos6502_get_pin_clk(mos6502), true);
}
/* Pull RES high and sync */
mos6502_set_pin_res(mos6502, BIT_ONE, true);
}
void mos6502_controller_step(mos6502_t * mos6502, mos6502_memory_t * memory) {
/* Toggle CLK input and sync */
mos6502_set_pin_clk(mos6502, !mos6502_get_pin_clk(mos6502), true);
/* Access memory only after rising clock edge */
if (mos6502_get_pin_clk(mos6502)) {
unsigned short addr = mos6502_get_pin_ab(mos6502);
if (mos6502_get_pin_rw(mos6502) == BIT_ZERO) {
/* Write memory from data bus */
mos6502_memory_write(memory, addr, mos6502_get_pin_db(mos6502));
} else {
/* Read memory into data bus and sync */
mos6502_set_pin_db(mos6502, mos6502_memory_read(memory, addr), true);
}
}
}
void mos6502_controller_run(mos6502_t * mos6502, mos6502_memory_t * memory, size_t cycles) {
size_t i;
/* Each step is a half-cycle */
for (i = 0; i < cycles; i++) {
mos6502_controller_step(mos6502, memory);
mos6502_controller_step(mos6502, memory);
}
}