-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMINIL_monitor_improved.ino
386 lines (353 loc) · 8.56 KB
/
MINIL_monitor_improved.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
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
376
377
378
379
380
381
382
383
384
385
386
/* MINIL Machine-Code Monitor
Justin Miller - justinmiller.io - 4th October 2021
Based on original by:
David Johnson-Davies - www.technoblogy.com - 19th June 2014
- Meant to run on Arduino Nano instead of ATtiny85
- Ported to Arduino Keypad library use
- Ported to non-TinySPI 7-segment display (normal SPI)
- Added inline instruction set documentation
- Modified Enter button handling for board layout
- Added instruction delay to behave more like ATtiny85
- Added [0-7]9 register display instruction
- Added [0-7]F delay instruction
CC BY 4.0
Licensed under a Creative Commons Attribution 4.0 International license:
http://creativecommons.org/licenses/by/4.0/
*/
/* Instruction Set
= Loads [0-7][0-7]
45 R4 = R5
07 R0 = R7
= Jumps [JZ 80-BF, JNZ C0-FF]
C3 JNZ 3
84 JZ 4
= Special [0-7][9-F]
9 OUT Display register
A ADD Add 1 to register
B BRI Set LED brightness to register value (0-255)
C CLR Clear register
D DEC Decrement register
E ENT Enter value to register
F DLY Wait register value * 10 milliseconds
*/
// EEPROM library
#include <EEPROM.h>
// Matrix keypad setup
#include <Keypad.h>
const byte rows = 4;
const byte cols = 4;
char keys[rows][cols] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[rows] = { 5, 4, 3, 2 };
byte colPins[cols] = { 9, 8, 7, 6 };
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
// Button & LED setup
const int ButtonGnd = A0;
const int ButtonTrg = A2;
const int LED = 10;
// Seven segment display setup
#include <SPI.h>
const int SegCS = A3;
const int Clear_Display = 0x76;
const int Decimal_Control = 0x77;
const int Cursor_Control = 0x79;
const int Brightness_Control = 0x7A;
// MINIL setup
unsigned int Register[8];
void setup(void) {
pinMode(ButtonGnd, OUTPUT);
digitalWrite(ButtonGnd, LOW);
pinMode(ButtonTrg, INPUT_PULLUP);
pinMode(SegCS, OUTPUT);
digitalWrite(SegCS, HIGH);
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV64);
ClearDisplay();
delay(1000);
}
// Display utilities
void DisplayBegin() {
digitalWrite(SegCS, LOW);
}
void DisplayEnd() {
digitalWrite(SegCS, HIGH);
}
// Clear display
void ClearDisplay() {
DisplayBegin();
SPI.transfer(Brightness_Control);
delay(1);
SPI.transfer(255);
delay(1);
SPI.transfer(Clear_Display);
DisplayEnd();
}
// Display a 2 digit hex number
void DisplayTwo(int n, int offset) {
DisplayBegin();
SPI.transfer(Cursor_Control);
delay(1);
SPI.transfer(offset);
DisplayEnd();
DisplayBegin();
SPI.transfer((n>>4) & 0xFF);
delay(1);
SPI.transfer(n & 0x0F);
DisplayEnd();
}
// Display a four digit hex number
void Display(int n) {
DisplayBegin();
SPI.transfer(Decimal_Control);
delay(1);
SPI.transfer(0);
delay(1);
SPI.transfer(Cursor_Control);
delay(1);
SPI.transfer(0);
delay(1);
int i=16;
do {
i = i - 4;
SPI.transfer((n>>i) & 0x0F);
delay(1);
} while (i != 0);
DisplayEnd();
}
// Display colon
void DisplayColon() {
DisplayBegin();
SPI.transfer(Decimal_Control);
delay(1);
SPI.transfer(0x10);
DisplayEnd();
}
// Display running
void DisplayRunning() {
DisplayBegin();
SPI.transfer(Clear_Display);
delay(1);
SPI.transfer(Decimal_Control);
delay(1);
SPI.transfer(0x0F);
DisplayEnd();
}
// Display Go
void DisplayGo() {
DisplayBegin();
SPI.transfer(Cursor_Control);
delay(1);
SPI.transfer(0);
delay(1);
SPI.transfer(Decimal_Control);
delay(1);
SPI.transfer(0x10);
delay(1);
SPI.transfer('G');
delay(1);
SPI.transfer('o');
delay(1);
SPI.transfer(' ');
delay(1);
SPI.transfer(' ');
DisplayEnd();
}
// Display Error
void DisplayError() {
DisplayBegin();
SPI.transfer(Cursor_Control);
delay(1);
SPI.transfer(0);
DisplayEnd();
DisplayBegin();
SPI.transfer('E');
delay(1);
SPI.transfer('r');
DisplayEnd();
}
// Returns the keypad character or -1 if no button pressed
char ReadKeypad() {
char key = keypad.getKey();
if (key != '\0') {
int keyNum = key - '0';
if (keyNum < 0 || keyNum > 9) {
if (key == 'A') return 10;
if (key == 'B') return 11;
if (key == 'C') return 12;
if (key == 'D') return 13;
if (key == '*') return 14;
if (key == '#') return 15;
} else {
return keyNum;
}
}
return -1;
}
// Wait until release keypad key
void WaitReleaseKey() {
do delay(100); while (ReadKeypad() != -1);
}
// Read button
boolean ReadButton() {
return (digitalRead(ButtonTrg) == LOW);
}
// Waits until button up, or 1 second
boolean LongPress() {
long time = millis();
do {
delay(100);
if (millis()-time > 1000) return true;
} while (ReadButton());
return false;
}
// Reads a specified number of key presses and display them
// Returns -1 immediately if button was pressed, or result
// Uses long so can return an unsigned int or -1
long GetData(int keys) {
long Input = 0;
int Nibble;
boolean Press;
for (int i=0; i<keys; i++) {
Input = Input << 4;
do {
Nibble = ReadKeypad();
if (ReadButton()) return -1;
} while (Nibble < 0);
DisplayBegin();
if (i==0) {
SPI.transfer(Cursor_Control);
delay(1);
SPI.transfer(4-keys);
delay(1);
for (int i=0;i<keys;i++) {
SPI.transfer('_');
delay(1);
}
SPI.transfer(Cursor_Control);
delay(1);
SPI.transfer(4-keys);
delay(1);
}
SPI.transfer(Nibble);
DisplayEnd();
WaitReleaseKey();
Input = Input | Nibble;
}
return Input;
}
// MINIL Interpreter - Run the user's program
void Run() {
long Word;
int Sign;
unsigned int PC, Value, Jump;
char Inst, Reg, Special, Type;
boolean ZeroFlag = false;
PC = 0;
DisplayRunning();
do {
// Get next Instruction
Inst = EEPROM.read(PC++);
delayMicroseconds(500);
Type = Inst & 0x88;
Reg = (Inst >> 4) & 0x07;
if ((Inst & 0x80) != 0) {
// Jump instructions
Jump = Inst & 0x3F;
if (((Inst & 0x40) == 0) == ZeroFlag) PC = Jump;
} else if (Type == 0) {
// Load Instruction
Register[Reg] = Register[Inst & 0x07];
} else if (Type == 0x08) {
// Special Instructions
Special = Inst & 0x0F;
Value = Register[Reg];
if (Special == 0x09) {
// Display
Display(Value);
} else if ((Special == 0x0A) || (Special == 0x0D)) {
// Decrement or Add1 and convert to BCD
if (Special == 0x0A) Sign = 1; else Sign = -1;
Value = Value + Sign;
for (int i=0; i<16; i=i+4) {
// Binary-coded decimal correction - must be a neater way!
if ((Value & (unsigned int)(0xF<<i)) > (unsigned int)(0x9<<i)) {
Value = Value + Sign * (unsigned int)(0x6<<i);
}
}
Register[Reg] = Value;
ZeroFlag = (Value == 0);
} else if (Special == 0x0B) {
// Brightness - Convert value from BCD
Value = ((Value>>8) & 0x0F)*100 + ((Value>>4) & 0x0F)*10 + (Value & 0x0F);
analogWrite(LED,255-Value);
} else if (Special == 0x0C) {
// Clear
Register[Reg] = 0;
} else if (Special == 0x0E) {
// Display and enter
Display(Value);
do {
Word = GetData(4);
if (Word != -1) Register[Reg] = Word;
} while (Word != -1);
LongPress();
DisplayRunning();
} else if (Special == 0x0F) {
// Delay - Convert value from BCD
Value = ((Value>>8) & 0x0F)*100 + ((Value>>4) & 0x0F)*10 + (Value & 0x0F);
delay(Value*10);
} else {
// Invalid special instruction
ClearDisplay();
DisplayError();
DisplayTwo(Special, 2);
do ; while (!ReadButton());
}
}
} while (!ReadButton());
}
void loop() {
unsigned int PC, Value;
boolean Command;
long Word;
char Reg;
analogWrite(LED,255);
DisplayGo();
do ; while (ReadButton());
Word = GetData(1);
WaitReleaseKey();
if (Word <= 7) {
Reg = Word;
Value = Register[Reg];
// Display register contents
Display(Value);
do {
Word = GetData(4);
if (Word != -1) Register[Reg] = Word;
} while (Word != -1);
} else if (Word == 14) {
// * = Data input
PC = 0;
do {
do {
Word = EEPROM.read(PC);
ClearDisplay();
DisplayTwo(PC, 0);
DisplayTwo(Word, 2);
DisplayColon();
// Wait for key
Word = GetData(2);
if (Word != -1) EEPROM.write(PC, Word);
} while (Word != -1);
Command = LongPress();
PC++;
} while (!Command);
} else if (Word == 15) {
// # = Run Program
Run();
}
}