forked from dhansel/Altair8800
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumsys.cpp
202 lines (173 loc) · 4.08 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
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
// -----------------------------------------------------------------------------
// Altair 8800 Simulator
// Copyright (C) 2017 David Hansel
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// -----------------------------------------------------------------------------
#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_bin(byte b)
{
for(byte i=0; i<8; i++)
{
Serial.print(b & 0x80 ? '1' : '0');
b = b * 2;
}
}
void numsys_print_byte(byte b)
{
if( numsys==NUMSYS_HEX )
{
if( b<16 ) Serial.print('0');
Serial.print(b, HEX);
}
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
{
if( b<10 ) Serial.print(' ');
if( b<100 ) Serial.print(' ');
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
{
if( w<10 ) Serial.print(' ');
if( w<100 ) Serial.print(' ');
if( w<1000 ) Serial.print(' ');
if( w<10000 ) Serial.print(' ');
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(']');
}
static byte numsys_read_hex_digit()
{
while( true )
{
char c = serial_read();
if( c>='0' && c<='9' )
return c-'0';
else if( c>='A' && c<='F' )
return c-'A'+10;
else if( c>='a' && c<='f' )
return c-'a'+10;
}
}
byte numsys_read_hex_byte()
{
byte b;
b = numsys_read_hex_digit() * 16;
b += numsys_read_hex_digit();
return b;
}
uint16_t numsys_read_hex_word()
{
uint16_t w;
w = (uint16_t) numsys_read_hex_byte() * 256;
w += (uint16_t) numsys_read_hex_byte();
return w;
}
uint16_t numsys_read_word(bool *ESC)
{
byte b;
uint16_t w = 0;
int c = -1;
if( ESC!=NULL ) *ESC = false;
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);
}
else if( c==27 && ESC!=NULL )
{
*ESC = true;
return 0;
}
}
return w;
}
byte numsys_get()
{
return numsys;
}
byte numsys_get_byte_length()
{
return numsys==NUMSYS_HEX ? 2 : 3;
}