-
Notifications
You must be signed in to change notification settings - Fork 0
/
PET.ino
149 lines (122 loc) · 3.04 KB
/
PET.ino
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <stdarg.h>
#include <SPI.h>
#include <r65emu.h>
#include <r6502.h>
#include <pia.h>
#include <via.h>
#include "config.h"
#include "screen.h"
#include "kbd.h"
#include "petio.h"
#include "sound.h"
#if ROM_SET == series4
#include "roms/basic4_b000.h"
#include "roms/basic4_c000.h"
#include "roms/basic4_d000.h"
#include "roms/kernal4.h"
#include "roms/edit4.h"
prom basic_b000(basic4_b000, 4096);
prom basic_c000(basic4_c000, 4096);
prom basic_d000(basic4_d000, 4096);
prom kernal(kernal4, 4096);
prom edit(edit4, 2048);
#elif ROM_SET == series2
#include "roms/basic2_c000.h"
#include "roms/basic2_d000.h"
#include "roms/kernal2.h"
#include "roms/edit2.h"
prom basic_c000(basic2_c000, 4096);
prom basic_d000(basic2_d000, 4096);
prom kernal(kernal2, 4096);
prom edit(edit2, 2048);
#else
#error "ROM_SET not defined"
#endif
ram<> pages[RAM_PAGES];
flash_filer files(PROGRAMS);
petio io(files);
kbd keyboard;
ps2_raw_kbd ps2(keyboard);
screen screen;
Memory memory;
r6502 cpu(memory);
sound sound;
const char *filename;
bool reset() {
bool sd = hardware_reset();
sound.reset();
io.reset();
ps2.reset();
screen.begin();
return sd;
}
void function_keys(uint8_t key) {
char buf[32];
switch (key) {
case 1:
reset();
break;
case 2:
filename = io.files.advance();
screen.status(filename? filename: "No file");
break;
case 3:
filename = io.files.rewind();
screen.status(filename? filename: "No file");
break;
case 4:
if (io.load_prg())
snprintf(buf, sizeof(buf), "Loaded %s", filename);
else
snprintf(buf, sizeof(buf), "Failed to load %s", filename);
screen.status(buf);
break;
case 6:
screen.status(io.files.checkpoint());
break;
case 7:
if (filename)
io.files.restore(filename);
break;
case 10:
hardware_debug_cpu();
break;
}
}
void setup() {
hardware_init(cpu);
for (int i = 0; i < RAM_PAGES; i++)
memory.put(pages[i], i * ram<>::page_size);
#if defined(USE_SPIRAM)
memory.put(sram, SPIRAM_BASE, SPIRAM_EXTENT);
#endif
memory.put(screen, 0x8000);
memory.put(io, 0xe800);
#if ROM_SET == series4
memory.put(basic_b000, 0xb000);
#endif
memory.put(basic_c000, 0xc000);
memory.put(basic_d000, 0xd000);
memory.put(edit, 0xe000);
memory.put(kernal, 0xf000);
io.pia1.register_porta_write_handler([](uint8_t b) { keyboard.write(b & 0x0f); });
io.pia1.register_porta_read_handler([]() { return keyboard.row() | 0x80; });
io.pia1.register_portb_read_handler([]() { return keyboard.read(); });
io.via.register_irq_handler([](bool irq) { if (irq) cpu.raise(0); });
io.via.register_ca2_handler([](bool ca2) { screen.set_upper(ca2); });
io.via.register_sr_write_handler([](uint8_t r) { sound.octave(r); });
io.via.register_t2lo_write_handler([](uint8_t r) { sound.frequency(r); });
io.via.register_acr_write_handler([](uint8_t r) {
sound.on_off((r & VIA::ACR_SHIFT_MASK) == VIA::ACR_SO_T2_RATE);
});
ps2.register_fnkey_handler(function_keys);
bool sd = reset();
if (!sd)
screen.status("No SD Card");
else if (!io.start())
screen.status("Failed to open " PROGRAMS);
}
void loop() {
ps2.poll();
hardware_run();
}