This repository was archived by the owner on May 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmega6502.ino
More file actions
170 lines (154 loc) · 3.58 KB
/
mega6502.ino
File metadata and controls
170 lines (154 loc) · 3.58 KB
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
#include <Arduino.h>
#include <avr/pgmspace.h>
// 2400 for the authentic experience, 9600 for something more enjoyable
#define BAUD 2400
#include <util/setbaud.h>
#include "rom.h"
#define CLOCK 53
#define RESET 52
#define RW 51
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
byte ram[0x1000];
byte pia[0x14];
#define ADDR ((addrh << 8) | addrl)
static uint8_t memRead() {
const uint8_t addrh = PINC;
const uint8_t addrl = PINA;
const uint16_t addr = ADDR;
uint8_t val;
switch (addr >> 12) {
default:
// nothing in the address space, just return zero.
return 0x00;
case 0x0:
val = ram[addr];
return val;
case 0xd:
// fake 6821
switch (addrl) {
case 0x10:
val = 0x80 | UDR0;
break;
case 0x11:
val = UCSR0A & _BV(RXC0);
break;
case 0x12:
val = UCSR0A & _BV(UDRE0) ? 0x00 : 0xFF;
break;
default:
printf("pia: invalid read at %04x\n", addr);
val = 0;
break;
}
return val;
case 0xe:
val = pgm_read_byte(erom + (addr & 0x0fff));
return val;
case 0xf:
val = pgm_read_byte(from + (addr & 0x0fff));
return val;
}
}
static void memWrite() {
const uint8_t addrh = PINC;
const uint8_t addrl = PINA;
const uint16_t addr = ADDR;
const uint8_t val = PINL;
switch (addr >> 12) {
default:
// nothing in the address space ignore the write;
break;
case 0x0:
ram[addr] = val;
break;
case 0xd:
switch (addrl) {
default:
printf("pia: invalid write at %04x: %02x\n", addr, val);
break;
case 0x12:
putchar(val & 0x7f);
break;
case 0x11:
// ignore write to $D011, PIA is not configurable.
case 0x13:
// ignore write to $D013, PIA is not configurable.
break;
}
break;
}
}
int uart_putchar(char c, FILE *stream) {
if (c == '\r') {
uart_putchar('\n', stream);
}
loop_until_bit_is_set(UCSR0A, UDRE0);
UDR0 = c;
return 1;
}
static FILE uartout = {0} ;
void uart_init(void) {
UBRR0H = UBRRH_VALUE;
UBRR0L = UBRRL_VALUE;
#if USE_2X
UCSR0A |= _BV(U2X0);
#else
UCSR0A &= ~(_BV(U2X0));
#endif
UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); /* 8-bit data */
UCSR0B = _BV(RXEN0) | _BV(TXEN0); /* Enable RX and TX */
fdev_setup_stream (&uartout, uart_putchar, NULL, _FDEV_SETUP_WRITE);
stdout = &uartout ;
}
void setup() {
uart_init();
printf("RESET\n");
pinMode(CLOCK, OUTPUT);
pinMode(RESET, OUTPUT);
pinMode(RW, INPUT);
digitalWrite(RESET, HIGH);
// setup PORTC to read the low byte of the address
DDRC = 0x00;
PORTC = 0x00;
// setup PORTA to do the same for the high byte
DDRA = 0x00;
PORTA = 0x00;
// send reset pulse
digitalWrite(RESET, LOW);
digitalWrite(CLOCK, HIGH);
digitalWrite(CLOCK, LOW);
digitalWrite(CLOCK, HIGH);
digitalWrite(CLOCK, LOW);
digitalWrite(CLOCK, HIGH);
digitalWrite(CLOCK, LOW);
digitalWrite(CLOCK, HIGH);
digitalWrite(CLOCK, LOW);
digitalWrite(CLOCK, HIGH);
digitalWrite(CLOCK, LOW);
digitalWrite(CLOCK, HIGH);
digitalWrite(RESET, HIGH);
}
void loop() {
while (true) {
cli();
cbi(PORTB, 0);
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t"); // 4 * 62.5ns delay @ 16mhz
sbi(PORTB, 0);
sei();
if (PINB & _BV(PB2)) {
// read cycle
DDRL = 0xff;
PORTL = memRead();
} else {
// write cycle
DDRL = 0x00;
PORTL = 0;
memWrite();
}
}
}