forked from dhansel/Altair8800
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhost_pc.cpp
254 lines (195 loc) · 5.05 KB
/
host_pc.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#ifdef _WIN32
#include <Arduino.h>
#include <time.h>
#include <string>
#include "Altair8800.h"
#include "mem.h"
#include "serial.h"
#include "cpucore.h"
#include "host_pc.h"
#include "Windows.h"
#include "profile.h"
byte data_leds;
uint16_t status_leds;
uint16_t addr_leds;
byte stop_request;
static FILE *storagefile = NULL;
//#define DEBUG
bool host_read_function_switch(byte i)
{
return false;
}
bool host_read_function_switch_debounced(byte i)
{
return false;
}
bool host_read_function_switch_edge(int i)
{
return false;
}
uint16_t host_read_function_switches_edge()
{
return 0;
}
void host_reset_function_switch_state()
{
}
// ----------------------------------------------------------------------------------
void host_write_data(const void *data, uint32_t addr, uint32_t len)
{
#ifdef DEBUG
printf("Writing %i bytes to 0x%04x: ", len, addr);
for(int i=0; i<len; i++) printf("%02x ", ((byte *) data)[i]);
printf("\n");
#endif
if( storagefile )
{
fseek(storagefile, addr, SEEK_SET);
fwrite(data, len, 1, storagefile);
fflush(storagefile);
}
}
void host_read_data(void *data, uint32_t addr, uint32_t len)
{
if( storagefile )
{
fseek(storagefile, addr, SEEK_SET);
fread(data, len, 1, storagefile);
}
#ifdef DEBUG
printf("Reading %i bytes from 0x%04x: ", len, addr);
for(int i=0; i<len; i++) printf("%02x ", ((byte *) data)[i]);
printf("\n");
#endif
}
void host_move_data(uint32_t to, uint32_t from, uint32_t len)
{
void *buf = malloc(len);
host_read_data(buf, from, len);
host_write_data(buf, to, len);
free(buf);
}
void host_copy_flash_to_ram(void *dst, const void *src, uint32_t len)
{
memcpy(dst, src, len);
}
// ----------------------------------------------------------------------------------
static FILE *open_file(const char *filename)
{
char fnamebuf[30];
sprintf(fnamebuf, "disks\\%s", filename);
return fopen(fnamebuf, "rb");
}
bool host_file_exists(const char *filename)
{
FILE *f = open_file(filename);
if( f )
{
fclose(f);
return true;
}
return false;
}
int host_get_file_size(const char *filename)
{
int res = -1;
FILE *f = open_file(filename);
if( f )
{
if( fseek(f, 0, SEEK_END)==0 )
res = ftell(f);
fclose(f);
}
return res;
}
uint32_t host_read_file(const char *filename, uint32_t offset, uint32_t len, void *buffer)
{
uint32_t res = 0;
FILE *f = open_file(filename);
if( f )
{
if( fseek(f, offset, SEEK_SET)==0 )
res = fread(buffer, 1, len, f);
fclose(f);
}
//printf("host_read_file('%s', %04x, %04x, %p)=%04x\n", filename, offset, len, buffer, res);
return res;
}
uint32_t host_write_file(const char *filename, uint32_t offset, uint32_t len, void *buffer)
{
char fnamebuf[30];
sprintf(fnamebuf, "disks\\%s", filename);
uint32_t res = 0;
FILE *f = fopen(fnamebuf, "r+b");
if( !f ) f = fopen(fnamebuf, "w+b");
if( f )
{
if( fseek(f, offset, SEEK_SET)==0 )
res = fwrite(buffer, 1, len, f);
fclose(f);
}
//printf("host_write_file('%s', %04x, %04x, %p)=%04x\n", filename, offset, len, buffer, res);
return res;
}
// ----------------------------------------------------------------------------------
uint32_t host_get_random()
{
return rand()*65536l | rand();
}
void host_check_interrupts()
{
static unsigned long prevCtrlC = 0;
if( Serial.available() )
{
byte c = Serial.read();
if( c == 3 )
{
// CTRL-C was pressed. If we receive two CTRL-C in short order
// then we terminate the emulator.
if( millis()>prevCtrlC+250 )
prevCtrlC = millis();
else
exit(0);
}
serial_receive_host_data(0, c);
}
}
void host_serial_setup(byte iface, unsigned long baud, bool set_primary_interface)
{
// no setup to be done
}
void host_setup()
{
data_leds = 0;
status_leds = 0;
addr_leds = 0;
stop_request = 0;
// open storage data file for mini file system
storagefile = fopen("AltairStorage.dat", "r+b");
if( storagefile==NULL )
{
void *chunk = calloc(1024, 1);
storagefile = fopen("AltairStorage.dat", "wb");
if( storagefile!=NULL )
{
uint32_t size;
for( size = 0; (size+1024) < HOST_STORAGESIZE; size+=1024 )
fwrite(chunk, 1024, 1, storagefile);
fwrite(chunk, HOST_STORAGESIZE-size, 1, storagefile);
fclose(storagefile);
}
storagefile = fopen("AltairStorage.dat", "r+b");
}
// send CTRL-C to input instead of processing it (otherwise the
// emulator would immediately quit if CTRL-C is pressed) and we
// could not use CTRL-C to stop a running BASIC example.
// CTRL-C is handled in host_check_interrupts (above) such that
// pressing it twice within 250ms will cause the emulator to terminate.
DWORD mode;
HANDLE hstdin = GetStdHandle(STD_INPUT_HANDLE);
GetConsoleMode(hstdin, &mode);
SetConsoleMode(hstdin, mode & ~ENABLE_PROCESSED_INPUT);
// initialize random number generator
srand(time(NULL));
}
#endif