forked from dhansel/Altair8800
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumsys.cpp
117 lines (99 loc) · 2.06 KB
/
numsys.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
#include <Arduino.h>
#include "numsys.h"
#include "mem.h"
#include "serial.h"
static byte numsys = NUMSYS_HEX;
static byte hexToDec(int hc)
{
if( hc>96 ) hc -= 32;
if( hc >= 65 && hc <= 70 )
return hc - 65 + 10;
else if( hc >= 48 && hc <= 57 )
return hc - 48;
return 255;
}
void numsys_toggle()
{
numsys = (numsys+1) % 3;
}
void numsys_set(byte sys)
{
numsys = sys;
}
void numsys_print_byte(byte b)
{
if( numsys==NUMSYS_HEX )
{
static char hexchars[17] = "0123456789ABCDEF";
Serial.write(hexchars[b>>4]);
Serial.write(hexchars[b&0x0f]);
}
else if( numsys==NUMSYS_OCT )
{
byte d;
d = (b&0700) >> 6;
Serial.print(d);
d = (b&0070) >> 3;
Serial.print(d);
d = (b&0007);
Serial.print(d);
}
else
Serial.print(b);
}
void numsys_print_word(uint16_t w)
{
if( numsys==NUMSYS_HEX )
{
numsys_print_byte(w >> 8);
numsys_print_byte(w & 0xff);
}
else if( numsys==NUMSYS_OCT )
{
Serial.print((w>>15) & 007);
Serial.print((w>>12) & 007);
Serial.print((w>> 9) & 007);
Serial.print((w>> 6) & 007);
Serial.print((w>> 3) & 007);
Serial.print( w & 007);
}
else
{
Serial.print(w);
}
}
void numsys_print_mem(uint16_t addr, byte num, bool printBrackets)
{
byte i;
if( printBrackets ) Serial.print('[');
for(i=0; i<num; i++)
{ numsys_print_byte(MREAD(addr+i)); if(i+1<num) Serial.print(' '); }
if( printBrackets ) Serial.print(']');
}
uint16_t numsys_read_word()
{
byte b;
uint16_t w = 0;
int c = -1;
while( c!=13 && c!=10 && c!=32 && c!=9 && c!='-' && c!=':' )
{
c=-1;
while(c<0) c = serial_read();
if( numsys==NUMSYS_HEX && (b=hexToDec(c))!=255 )
{
Serial.write(c);
w = w << 4 | b;
}
else if( numsys==NUMSYS_OCT && c>=48 && c<=55 )
{
Serial.write(c);
w = w << 3 | (c-48);
}
else if( c>=48 && c<=57 )
{
Serial.write(c);
w = w * 10 + (c-48);
}
}
return w;
}