forked from dhansel/Altair8800
-
Notifications
You must be signed in to change notification settings - Fork 0
/
host_mega.cpp
375 lines (292 loc) · 9.25 KB
/
host_mega.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#ifdef __AVR_ATmega2560__
#include <Arduino.h>
#include "Altair8800.h"
#include "config.h"
#include "mem.h"
#include "host_mega.h"
#if NUM_DRIVES>0
#error Arduino MEGA port does not support disk drives. Set NUM_DRIVES to 0 in config.h
#endif
#if USE_THROTTLE>0
#error Throttling neither supported nor necessary for Arduino MEGA. Set USE_THROTTLE to 0 in config.h
#endif
/*
Runs emulation at about 0.5 Mhz clock speed (about 1/4 speed of original Altair8800)
Function switch pin mapping (digital input):
RUN => 20
STOP => 21 (Port D, bit 0)
STEP => 4
SLOW => 5
EXAMINE => 6
EXAMINE NEXT => 7
DEPOSIT => 8
DEPOSIT NEXT => 9
RESET => 18 (Port D, bit 3)
CLR => 19 (Port D, bit 2)
PROTECT => 16
UNPROTECT => 17
AUX1 UP => 14
AUX1 DOWN => 15
AUX2 UP => 3 (PORT E, bit 5)
AUX2 DOWN => 2 (PORT E, bit 4)
Address/Data switch pin mapping (analog input):
A0...15 => A0...15
Status LED mapping (digital output):
+A0..7 => 22, 23, ..., 29 (PORTA)
+A8..15 => 37, 36, ..., 30 (PORTC)
+D0..8 => 49, 48, ..., 42 (PORTL)
*INT => 53 \
WO => 52 |
STACK => 51 |
HLTA => 50 | STATUS (PORTB)
OUT => 10 |
M1 => 11 |
INP => 12 |
MEMR => 13 /
INTE => 38
PROT => 39
WAIT => 40
*HLDA => 41
*/
uint16_t host_read_status_leds()
{
uint16_t res = PORTB;
res |= PORTD & 0x80 ? ST_INTE : 0;
res |= PORTG & 0x04 ? ST_PROT : 0;
res |= PORTG & 0x02 ? ST_WAIT : 0;
res |= PORTG & 0x01 ? ST_HLDA : 0;
return res;
}
//------------------------------------------------------------------------------------------------------
void host_copy_flash_to_ram(void *dst, const void *src, uint32_t len)
{
for(uint32_t i=0; i<len; i++)
((byte *) dst)[i] = pgm_read_byte(((byte *) src)+i);
}
void host_write_data(const void *data, uint32_t addr, uint32_t len)
{
byte *b = (byte *) data;
for(uint32_t i=0; i<len; i++) EEPROM.write(addr+i, b[i]);
}
void host_read_data(void *data, uint32_t addr, uint32_t len)
{
byte *b = (byte *) data;
for(uint32_t i=0; i<len; i++) b[i] = EEPROM.read(addr+i);
}
void host_move_data(uint32_t to, uint32_t from, uint32_t len)
{
uint32_t i;
if( from<to )
{
for(i=0; i<len; i++) EEPROM.write(to+len-i-1, EEPROM.read(from+len-i-1));
}
else
{
for(i=0; i<len; i++) EEPROM.write(to+i, EEPROM.read(from+i));
}
}
// --------------------------------------------------------------------------------------------------
uint32_t host_get_random()
{
return (((uint32_t) random(0,65535)) * 65536l | random(0,65535));
}
// --------------------------------------------------------------------------------------------------
volatile static bool timer_running[4];
volatile static HostTimerFnTp timer_fn[4];
ISR(TIMER1_COMPA_vect) { noInterrupts(); timer_fn[0](); interrupts(); }
ISR(TIMER3_COMPA_vect) { noInterrupts(); timer_fn[1](); interrupts(); }
ISR(TIMER4_COMPA_vect) { noInterrupts(); timer_fn[2](); interrupts(); }
ISR(TIMER5_COMPA_vect) { noInterrupts(); timer_fn[3](); interrupts(); }
void host_interrupt_timer_setup(byte tid, uint32_t microseconds, HostTimerFnTp f)
{
noInterrupts();
// clk/256 prescaler (62.5kHz) gives a timer range from 16us to 1.4s
// with a resolution of 16us
uint16_t compare = microseconds/16;
switch( tid )
{
case 0:
{
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = compare;
TCCR1B |= (1 << WGM12); // CTC mode
TCCR1B &= ~0x07; // prescale=0 (disable timer)
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
}
case 1:
{
TCCR3A = 0;
TCCR3B = 0;
TCNT3 = 0;
OCR3A = compare;
TCCR3B |= (1 << WGM12); // CTC mode
TCCR3B &= ~0x07; // prescale=0 (disable timer)
TIMSK3 |= (1 << OCIE1A); // enable timer compare interrupt
}
case 2:
{
TCCR4A = 0;
TCCR4B = 0;
TCNT4 = 0;
OCR4A = compare;
TCCR4B |= (1 << WGM12); // CTC mode
TCCR4B &= ~0x07; // prescale=0 (disable timer)
TIMSK4 |= (1 << OCIE1A); // enable timer compare interrupt
}
case 3:
{
TCCR5A = 0;
TCCR5B = 0;
TCNT5 = 0;
OCR5A = compare;
TCCR5B |= (1 << WGM12); // CTC mode
TCCR5B &= ~0x07; // prescale=0 (disable timer)
TIMSK5 |= (1 << OCIE1A); // enable timer compare interrupt
}
}
timer_running[tid] = false;
timer_fn[tid] = f;
interrupts();
}
void host_interrupt_timer_start(byte tid)
{
noInterrupts();
// set clk/256 prescaler (enable timer)
timer_running[tid] = true;
switch( tid )
{
case 0: TCCR1B |= 1<<CS12; break;
case 1: TCCR3B |= 1<<CS12; break;
case 2: TCCR4B |= 1<<CS12; break;
case 3: TCCR5B |= 1<<CS12; break;
}
interrupts();
}
void host_interrupt_timer_stop(byte tid)
{
noInterrupts();
// set prescaler to 0 (disables timer)
switch( tid )
{
case 0: TCCR1B &= ~0x07; break;
case 1: TCCR3B &= ~0x07; break;
case 2: TCCR4B &= ~0x07; break;
case 3: TCCR5B &= ~0x07; break;
}
timer_running[tid] = false;
interrupts();
}
bool host_interrupt_timer_running(byte tid)
{
return timer_running[tid];
}
// --------------------------------------------------------------------------------------------------
volatile static uint16_t switches_pulse = 0;
volatile static uint16_t switches_debounced = 0;
static uint32_t debounceTime[16];
static const byte function_switch_pin[16] = {20, 21, 4, 5, 6, 7, 8, 9, 18, 19, 16, 17, 14, 15, 3, 2};
static const uint16_t function_switch_irq[16] = {0, INT_SW_STOP, 0, 0, 0, 0, 0, 0, INT_SW_RESET, INT_SW_CLR,
0, 0, 0, 0, INT_SW_AUX2UP, INT_SW_AUX2DOWN};
static void switch_check(byte i)
{
if( millis()>debounceTime[i] )
{
uint16_t bitval = 1<<i;
bool d1 = !digitalRead(function_switch_pin[i]);
bool d2 = (switches_debounced & bitval) ? true : false;
if( d1 && !d2 )
{
switches_debounced |= bitval;
switches_pulse |= bitval;
if( function_switch_irq[i] ) altair_interrupt(function_switch_irq[i]);
debounceTime[i] = millis() + 100;
}
else if( !d1 && d2 )
{
switches_debounced &= ~bitval;
switches_pulse &= ~bitval;
debounceTime[i] = millis() + 100;
}
}
}
bool host_read_function_switch(byte i)
{
return !digitalRead(function_switch_pin[i]);
}
bool host_read_function_switch_debounced(byte i)
{
if( function_switch_irq[i]==0 ) switch_check(i);
return (switches_debounced & (1<<i)) ? true : false;
}
bool host_read_function_switch_edge(byte i)
{
if( function_switch_irq[i]==0 ) switch_check(i);
uint16_t bitval = 1<<i;
bool b = switches_pulse & bitval ? true : false;
if( b ) switches_pulse &= ~bitval;
return b;
}
uint16_t host_read_function_switches_edge()
{
for(byte i=0; i<16; i++)
if( function_switch_irq[i]==0 )
switch_check(i);
uint16_t res = switches_pulse;
switches_pulse &= ~res;
return res;
}
void host_reset_function_switch_state()
{
for(byte i=0; i<16; i++) debounceTime[i]=0;
switches_debounced = 0;
switches_pulse = 0;
}
static void switch_interrupt(int i)
{
switch_check(i);
}
static void switch_interrupt1() { switch_interrupt(SW_STOP); }
static void switch_interrupt2() { switch_interrupt(SW_RESET); }
static void switch_interrupt3() { switch_interrupt(SW_CLR); }
static void switch_interrupt4() { switch_interrupt(SW_AUX2UP); }
static void switch_interrupt5() { switch_interrupt(SW_AUX2DOWN); }
static void switches_setup()
{
attachInterrupt(digitalPinToInterrupt(function_switch_pin[SW_STOP]), switch_interrupt1, CHANGE);
attachInterrupt(digitalPinToInterrupt(function_switch_pin[SW_RESET]), switch_interrupt2, CHANGE);
attachInterrupt(digitalPinToInterrupt(function_switch_pin[SW_CLR]), switch_interrupt3, CHANGE);
attachInterrupt(digitalPinToInterrupt(function_switch_pin[SW_AUX2UP]), switch_interrupt4, CHANGE);
attachInterrupt(digitalPinToInterrupt(function_switch_pin[SW_AUX2DOWN]), switch_interrupt5, CHANGE);
delay(1);
host_reset_function_switch_state();
}
// --------------------------------------------------------------------------------------------------
void host_serial_setup(byte iface, unsigned long baud, bool set_primary_interface)
{
if( iface==0 )
{
Serial.end();
Serial.begin(baud);
Serial.setTimeout(10000);
}
}
void host_setup()
{
int i;
for(i=0; i<8; i++)
{
pinMode(2+i, INPUT_PULLUP);
pinMode(14+i, INPUT_PULLUP);
}
for(i=10; i<14; i++) pinMode(i, OUTPUT);
for(i=22; i<54; i++) pinMode(i, OUTPUT);
switches_setup();
// TODO: Find a way to initialize random number generator. Unfortunately
// this is hard since all analog pins are connected and therefore the
// usual analogRead(0) method always returns either 0 or 1023, depending
// on the setting of SW0
randomSeed(analogRead(0));
}
#endif