-
Notifications
You must be signed in to change notification settings - Fork 4
/
hello.ino
125 lines (103 loc) · 2.09 KB
/
hello.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
//
// This sketch uses code on the Z80 to write a greeting to the serial-console.
//
// We let this program run for a few thousand cycles, which should be
// sufficient to allow the text-output, "Hello\n", to complete.
//
// Steve
//
#include <z80retroshield.h>
//
// Our program, as hex.
//
// The source code is located in `hello.z80`.
//
unsigned char memory[32] =
{
0x3e, 0x48, 0xd3, 0x01, 0x3e, 0x65, 0xd3, 0x01, 0x3e, 0x6c, 0xd3, 0x01,
0xd3, 0x01, 0x3e, 0x6f, 0xd3, 0x01, 0x3e, 0x0a, 0xd3, 0x01, 0xc3, 0x16,
0x00
};
int memory_len = sizeof(memory) / sizeof(memory[0]);
//
// Our helper
//
Z80RetroShield cpu;
//
// RAM I/O function handler.
//
char memory_read(int address)
{
return (memory[address]);
}
//
// I/O function handler.
//
void io_write(int address, char byte)
{
if (address == 1)
Serial.write(byte);
}
//
// Setup routine: Called once.
//
void setup()
{
Serial.begin(115200);
//
// Setup callbacks.
//
// We must setup a memory-read callback, otherwise the program
// won't be fetched and executed.
//
cpu.set_memory_read(memory_read);
//
// Then we setup a callback to be executed every time an "out (x),y"
// instruction is encountered.
//
cpu.set_io_write(io_write);
//
// Configured.
//
Serial.println("Z80 configured; launching program.");
}
//
// Loop function: Called forever.
//
void loop()
{
//
// We stop running after a specific number of cycles
//
// Have we reached that point?
//
static bool done = false;
//
// Count the number of times we've pumped the Z80.
//
static int cycles = 0;
//
// Are we done? If so return.
//
if (done)
return;
//
// We stop running after running a specific number of cycles
//
if (cycles > 4096)
{
Serial.print("Z80 processor stopped ");
Serial.print(cycles);
Serial.println(" cycles executed.");
done = true;
return;
}
//
// Step the CPU.
//
cpu.Tick();
//
// We've run a tick.
//
cycles++;
}