-
Notifications
You must be signed in to change notification settings - Fork 11
/
readrom.ino
179 lines (160 loc) · 3.65 KB
/
readrom.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
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
int pin_d0 = 2;
int pin_d1 = 3;
int pin_d2 = 4;
int pin_d3 = 5;
int pin_d4 = 6;
int pin_d5 = 7;
int pin_d6 = 8;
int pin_d7 = 9;
int pin_sel_low = 10;
int pin_sel_high = 11;
int pin_cs = 12;
void set_port_as_input()
{
pinMode(pin_d0, INPUT);
pinMode(pin_d1, INPUT);
pinMode(pin_d2, INPUT);
pinMode(pin_d3, INPUT);
pinMode(pin_d4, INPUT);
pinMode(pin_d5, INPUT);
pinMode(pin_d6, INPUT);
pinMode(pin_d7, INPUT);
}
void set_port_as_output()
{
pinMode(pin_d0, OUTPUT);
pinMode(pin_d1, OUTPUT);
pinMode(pin_d2, OUTPUT);
pinMode(pin_d3, OUTPUT);
pinMode(pin_d4, OUTPUT);
pinMode(pin_d5, OUTPUT);
pinMode(pin_d6, OUTPUT);
pinMode(pin_d7, OUTPUT);
}
void set_port_value(byte value)
{
digitalWrite(pin_d0, (value&1) ? HIGH : LOW);
digitalWrite(pin_d1, (value&2) ? HIGH : LOW);
digitalWrite(pin_d2, (value&4) ? HIGH : LOW);
digitalWrite(pin_d3, (value&8) ? HIGH : LOW);
digitalWrite(pin_d4, (value&16) ? HIGH : LOW);
digitalWrite(pin_d5, (value&32) ? HIGH : LOW);
digitalWrite(pin_d6, (value&64) ? HIGH : LOW);
digitalWrite(pin_d7, (value&128) ? HIGH : LOW);
}
byte read_port_value()
{
byte val = 0;
if(digitalRead(pin_d0))
val |= 1;
if(digitalRead(pin_d1))
val |= 2;
if(digitalRead(pin_d2))
val |= 4;
if(digitalRead(pin_d3))
val |= 8;
if(digitalRead(pin_d4))
val |= 16;
if(digitalRead(pin_d5))
val |= 32;
if(digitalRead(pin_d6))
val |= 64;
if(digitalRead(pin_d7))
val |= 128;
return val;
}
byte read_cycle(short address)
{
digitalWrite(pin_cs, HIGH);
digitalWrite(pin_sel_low, LOW);
digitalWrite(pin_sel_high, LOW);
set_port_as_output();
set_port_value(address & 0xff);
digitalWrite(pin_sel_low, HIGH);
//delay(10);
digitalWrite(pin_sel_low, LOW);
set_port_value((address & 0xff00)>>8);
digitalWrite(pin_sel_high, HIGH);
//delay(10);
digitalWrite(pin_sel_high, LOW);
set_port_as_input();
digitalWrite(pin_cs, LOW);
//delay(10);
byte val = read_port_value();
digitalWrite(pin_cs, HIGH);
return val;
}
char cmdbuf[64];
int cmd_index;
void setup()
{
// put your setup code here, to run once:
set_port_as_input();
pinMode(pin_sel_low, OUTPUT);
digitalWrite(pin_sel_low, LOW);
pinMode(pin_sel_high, OUTPUT);
digitalWrite(pin_sel_high, LOW);
pinMode(pin_cs, OUTPUT);
digitalWrite(pin_cs, HIGH);
Serial.begin(9600);
cmd_index = 0;
cmdbuf[0] = 0;
}
void loop()
{
// put your main code here, to run repeatedly:
if(Serial.available() > 0)
{
char b = Serial.read();
if(b!='\n')
{
if(cmd_index<63)
cmdbuf[cmd_index++] = b;
}
else
{
cmdbuf[cmd_index] = 0;
if(cmd_index>0)
{
if(cmdbuf[0]=='m' && cmdbuf[1]==' ')
{
short addr_start;
short addr_end;
int num = sscanf(cmdbuf+2, "%x%x", &addr_start, &addr_end);
if(num==0)
Serial.println('?');
else
{
if(num==1)
addr_end = addr_start + 16;
short addr = addr_start;
short n = 0;
while(addr!=addr_end)
{
if(n==0)
{
char buf[16];
sprintf(buf, "\n%04x ", addr);
Serial.print(buf);
}
n++;
if(n==16)
n = 0;
byte data = read_cycle(addr);
char buf[16];
sprintf(buf, "%02x ", data);
Serial.print(buf);
addr++;
}
}
Serial.println(' ');
}
else
{
Serial.println('?');
}
cmd_index = 0;
}
}
}
}