-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathz80em.cpp
148 lines (127 loc) · 4.5 KB
/
z80em.cpp
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
/****************************************************************************/
/*** ***/
/*** Copyright (C) 2014 David Ryan (ptcryan) ***/
/*** This program is free software; you can redistribute it and/or modify ***/
/*** it under the terms of the GNU General Public License as published by ***/
/*** the Free Software Foundation; either version 3 of the License, or ***/
/*** (at your option) any later version. ***/
/*** ***/
/*** This program is distributed in the hope that it will be useful, ***/
/*** but WITHOUT ANY WARRANTY; without even the implied warranty of ***/
/*** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ***/
/*** GNU General Public License for more details. ***/
/*** ***/
/****************************************************************************/
#include <SPI.h>
#include <SD.h>
#include <stdio.h>
#include <stdlib.h>
#include "Z80em.h"
#include "Z80IO.h"
// set up variables using the SD utility library functions:
Sd2Card card;
void DumpMem(int start, int stop);
// Create the memory space that will emulate RAM
volatile byte PC_MEM[SYSTEM_MEMORY_SIZE]; // Due has 96KB. So 64K is ok!
void setup(void) {
// setup the serial port for debug purposes
SerialUSB.begin(SERIAL_SPEED);
while (!SerialUSB.available()) {
SerialUSB.println("Press any key to begin.");
delay(1000);
}
#ifdef HW_DISK_LED_ENABLE
digitalWrite(HW_DISK_LED, LOW);
pinMode(HW_DISK_LED, OUTPUT);
#endif // HW_DISK_LED_ENABLE
// Print banner & info
SerialUSB.println();
SerialUSB.println("Zilog Z80 PC emulator");
SerialUSB.println("Running on ARM M3");
SerialUSB.print("Version ");
SerialUSB.println(VERSION);
SerialUSB.print("Built on ");
SerialUSB.print(__DATE__);
SerialUSB.print(" at ");
SerialUSB.println(__TIME__);
SerialUSB.print(SYSTEM_MEMORY_SIZE / 1024);
SerialUSB.print("K bytes");
SerialUSB.println(" of RAM available");
SerialUSB.print("\nInitializing SD card...");
uint8_t cardStatus = card.init(SPI_HALF_SPEED, CS_SD);
if (!cardStatus) {
SerialUSB.println("initialization failed.");
SerialUSB.print("Card status error = ");
SerialUSB.println(cardStatus);
return;
} else {
SerialUSB.println("SD card detected.");
}
// fill PC RAM with 0xCB
SerialUSB.print("Running PC RAM tests...");
uint32_t i;
for (i = 0; i < SYSTEM_MEMORY_SIZE; i++) {
PC_MEM[i] = 0xcb;
}
// verify memory
bool memTestPass = true;
for (i = 0; i < SYSTEM_MEMORY_SIZE; i++) {
if (PC_MEM[i] != 0xcb) {
memTestPass = false;
SerialUSB.print(" failed at ");
SerialUSB.println(i, HEX);
}
}
if (memTestPass == true) {
SerialUSB.println("Pass");
}
// clear memory to 0
for (i = 0; i < SYSTEM_MEMORY_SIZE; i++) {
PC_MEM[i] = 0;
}
// Reset the CPU
Z80_Reset();
// Use the port interfaces we already have
// to load the system Cold Start Loader
SerialUSB.println("");
SerialUSB.println("booting from boot sector...");
// The ipl is located on track 0 sector 0 of disk
Z80_Out(0x10, 0); // track=0
Z80_Out(0x12, 0); // sector=0
Z80_Out(0x14, 0x00); // DMA low addr=00
Z80_Out(0x15, 0x20); // DMA high addr=20
Z80_Out(0x16, 1); // read sector into RAM
// set PC to beginning of ipl
Z80_Regs Regs;
Z80_GetRegs(&Regs);
Regs.PC.D = 0x2000;
Z80_SetRegs(&Regs);
// Start the CPU
Z80();
}
void loop(void) {
SerialUSB.println("System stopped.");
while (1) {}
}
void DumpMem(int start, int stop) {
for (int i = start; i < stop; i+=16) {
SerialUSB.print(i, HEX);
SerialUSB.print(": ");
for (int j = i; j < i+16; j++) {
if (PC_MEM[j] < 0x10) {
SerialUSB.print("0"); // Add leading zero for alignment
}
SerialUSB.print(PC_MEM[j], HEX);
SerialUSB.print(" ");
}
SerialUSB.print("| ");
for (int k = i; k < i+16; k++) {
if (PC_MEM[k] > 31) {
SerialUSB.write(PC_MEM[k]);
} else {
SerialUSB.print(".");
}
}
SerialUSB.println();
}
}