-
Notifications
You must be signed in to change notification settings - Fork 1
/
baseconv.js
330 lines (279 loc) · 8.67 KB
/
baseconv.js
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
// Conversion functions for:
// Decimal -> Binary
// Binary -> Decimal
function dec2bin(num,bits)
{
var i, binRep, neg;
neg = false;
binRep = "";
if (num < 0)
{
num = -num;
neg = true;
}
for(i=0; i<bits; i++)
{
if (num % 2 == 0)
binRep = "0" + binRep;
else
binRep = "1" + binRep;
num = Math.floor(num / 2);
}
if (neg)
binRep = twosComp(binRep);
return(binRep);
}
function bin2dec(num, isSigned)
{
var theVal, i, neg;
neg = false;
theVal = 0;
if (num.charAt(0) == '1' && isSigned)
{
neg = true;
num = twosComp(num);
}
for (i=0; i<num.length; i++)
{
theVal = theVal + Math.pow(2,i)*(parseInt(num.charAt(num.length-i-1)));
}
if (neg)
theVal = -theVal;
return theVal;
}
function twosComp(binNum)
// Convert the base2Value to its two's complement format and
// return it in a new bitData object. The original value is
// unchagned.
//
// NOTE:Taking the twos complement means flipping all of the bits
// then adding one. These two things can be done in sequence
// or simultaneously. The following solution does them
// simultaneously. The basic strategy is as follows:
//
// Starting with the LSB of the number:
// Scan each bit moving towards the MSB
// Leave each bit unchanged upto and including the
// the first 1.
// Flip all of the bits to the left of the first 1.
//
{
var flipEM, i, twosCVal;
twosCVal = "";
flipEM = false; // Flag that indicates if the
// bits should be being flipped yet.
// I.e. Are we to the left of the first
// 1 yet?
for (i=binNum.length-1; i>=0; i--)
{
if (flipEM)
// The first one has been reached so this bit
// needs to be flipped.
twosCVal = ((parseInt(binNum.charAt(i)) + 1) % 2) +
twosCVal;
else
{
// If the first one hasn't been reached just
// copy the bit. If this is the first one
// set the flipEM flag.
twosCVal = binNum.charAt(i) + twosCVal;
flipEM = (binNum.charAt(i) == '1')
}
}
return twosCVal;
}
function getInstruction(theOpcode)
{
var theOP;
theOP = theOpcode.substring(0,9);
if (theOP == "100000010") // LOAD
return "LOAD R" +
getNumber(theOpcode.substring(9,11),2,10,2) + " " +
getNumber(theOpcode.substring(11,16),2,10,5);
else if (theOP == "100000100") // STORE
return "STORE " +
getNumber(theOpcode.substring(11,16),2,10,5) + " R" +
getNumber(theOpcode.substring(9,11),2,10,2);
else if (theOP == "100100010") // MOVE
return "MOVE R" +
getNumber(theOpcode.substring(12,14),2,10,2) + " R" +
getNumber(theOpcode.substring(14,16),2,10,2);
else if (theOP == "101000010") // ADD
return "ADD" + getRRR(theOpcode);
else if (theOP == "101000100") // SUB
return "SUB" + getRRR(theOpcode);
else if (theOP == "101000110") // AND
return "AND" + getRRR(theOpcode);
else if (theOP == "101001000") // OR
return "OR" + getRRR(theOpcode);
else if (theOP == "000000010") // BRANCH
return "BRANCH " +
getNumber(theOpcode.substring(11,16),2,10,5);
else if (theOP == "000000100") // BZERO
return "BZERO " +
getNumber(theOpcode.substring(11,16),2,10,5);
else if (theOP == "000000110") // BNEG
return "BNEG " +
getNumber(theOpcode.substring(11,16),2,10,5);
else if (theOpcode == "0000000000000000") // NOP
return "NOP";
else if (theOpcode == "1111111111111111") // HALT
return "HALT";
else
return "ILLEGAL OPCODE";
}
function getRRR(theOpcode)
{
return " R" + getNumber(theOpcode.substring(10,12),2,10,2) +
" R" + getNumber(theOpcode.substring(12,14),2,10,2) +
" R" + getNumber(theOpcode.substring(14,16),2,10,2);
}
function getOpcode(theNum)
{
var key;
key = theNum.substring(0,2);
if (key == "LO") // LOAD
return "100000010" +
getNumber(theNum.substring(6,7),10,2,2) +
getNumber(theNum.substring(8,theNum.length),10,2,5);
else if (key == "ST") // STORE
return "100000100" +
getNumber(theNum.substring(theNum.length-1,theNum.length),10,2,2) +
getNumber(theNum.substring(6,theNum.length-3),10,2,5);
else if (key == "MO") // MOVE
return "100100010000" +
getNumber(theNum.substring(6,7),10,2,2) +
getNumber(theNum.substring(9,10),10,2,2);
else if (key == "AD") // ADD
return "1010000100" +
getNumber(theNum.substring(5,6),10,2,2) +
getNumber(theNum.substring(8,9),10,2,2) +
getNumber(theNum.substring(11,12),10,2,2);
else if (key == "SU") // SUB
return "1010001000" +
getNumber(theNum.substring(5,6),10,2,2) +
getNumber(theNum.substring(8,9),10,2,2) +
getNumber(theNum.substring(11,12),10,2,2);
else if (key == "AN") // AND
return "1010001100" +
getNumber(theNum.substring(5,6),10,2,2) +
getNumber(theNum.substring(8,9),10,2,2) +
getNumber(theNum.substring(11,12),10,2,2);
else if (key == "OR") // OR
return "1010010000" +
getNumber(theNum.substring(4,5),10,2,2) +
getNumber(theNum.substring(7,8),10,2,2) +
getNumber(theNum.substring(10,11),10,2,2);
else if (key == "BR") // BRANCH
return "00000001000" +
getNumber(theNum.substring(7,theNum.length),10,2,5);
else if (key == "BZ") // BZERO
return "00000010000" +
getNumber(theNum.substring(6,theNum.length),10,2,5);
else if (key == "BN") // BNEG
return "00000011000" +
getNumber(theNum.substring(5,theNum.length),10,2,5);
else if (key == "NO") // NOP
return "0000000000000000";
else if (key == "HA") // HALT
return "1111111111111111";
else
return "ILLEGAL INSTRUCTION";
}
function getNumber(theNum,fromBase,toBase,bits)
{
var base2Val, isSigned;
isSigned=false;
if (toBase == -10)
{
toBase = 10;
isSigned = true;
}
// Convert from whatever base the number is currently in
// to base 2.
if (fromBase == 10 || fromBase == -10)
base2Val = dec2bin(theNum,bits);
else if (fromBase == -1)
base2Val = getOpcode(theNum);
else if (fromBase == 2)
base2Val = theNum;
// Now convert from base2 to whatever base the number
// needs to be in.
if (toBase == 10)
return bin2dec(base2Val,isSigned);
else if (toBase == -1)
return getInstruction(base2Val);
else if (toBase == 2)
return base2Val;
else
return "BAD BASE CONVERSION";
}
function validInstruction(theInst)
{
return validLOAD(theInst) || validSTORE(theInst) ||
validMOVE(theInst) || validArithOrLogic(theInst) ||
validBRANCH(theInst) || validHALTorNOP(theInst);
}
function isValidRegister(theReg)
{
return (theReg.charAt(0) == 'R' &&
parseInt(theReg.substring(1,theReg.length)) >= 0 &&
parseInt(theReg.substring(1,theReg.length)) <= 3);
}
function isValidMemory(theMem)
{
return (parseInt(theMem) >= 0 &&
parseInt(theMem) <= 31);
}
function validLOAD(theInst)
{
var parts;
parts = theInst.split(" ");
return (parts[0] == "LOAD" &&
isValidRegister(parts[1]) &&
isValidMemory(parts[2]) &&
parts.length == 3);
}
function validSTORE(theInst)
{
var parts;
parts = theInst.split(" ");
return (parts[0] == "STORE" &&
isValidMemory(parts[1]) &&
isValidRegister(parts[2]) &&
parts.length == 3);
}
function validMOVE(theInst)
{
var parts;
parts = theInst.split(" ");
return (parts[0] == "MOVE" &&
isValidRegister(parts[1]) &&
isValidRegister(parts[2]) &&
parts.length == 3);
}
function validArithOrLogic(theInst)
{
var parts;
parts = theInst.split(" ");
return ((parts[0] == "ADD" || parts[0] == "SUB" || parts[0] == "AND" || parts[0] == "OR") &&
isValidRegister(parts[1]) &&
isValidRegister(parts[2]) &&
isValidRegister(parts[3]) &&
parts.length == 4);
}
function validBRANCH(theInst)
{
var parts;
parts = theInst.split(" ");
return ((parts[0] == "BRANCH" || parts[0] == "BZERO" || parts[0] == "BNEG") &&
isValidMemory(parts[1]) &&
parts.length == 2);
}
function validHALTorNOP(theInst)
{
var parts;
parts = theInst.split(" ");
return ((parts[0] == "HALT" || parts[0] == "NOP") &&
parts.length == 1);
}