41
41
SYMBOL_TABLE = {}
42
42
43
43
44
- # Public Functions
45
- def receive_params (value_table ):
46
- if value_table :
47
- raise RuntimeError ('Custom parameters are not supported' )
48
-
49
- def is_blank (line ):
50
- """Return whether a line is blank and not an instruction."""
51
- return __RE_BLANK__ .match (line ) is not None
52
-
53
- def get_parts (line ):
54
- """Break down an instruction into 3 parts: Label, Opcode, Operand"""
55
- m = __RE_PARTS__ .match (line )
56
- try :
57
- return m .group ('Label' ), m .group ('Opcode' ), m .group ('Operands' )
58
- except :
59
- return None
60
-
61
- def instruction_class (name ):
62
- """Translate a given instruction name to its corresponding class name."""
63
- return ALIASES .get (name , name )
64
-
65
44
# Private Variables
66
45
__OFFSET_SIZE__ = BIT_WIDTH - OPCODE_WIDTH - (REGISTER_WIDTH * 2 )
67
46
assert (__OFFSET_SIZE__ > 0 ) # Sanity check
@@ -210,8 +189,8 @@ def opcode():
210
189
raise NotImplementedError ()
211
190
212
191
@staticmethod
213
- def size ( ):
214
- """Return how many binary machine-level instructions the instruction will expand to. """
192
+ def pc ( pc , ** kwargs ):
193
+ """Return the new PC after assembling the given instruction """
215
194
raise NotImplementedError ()
216
195
217
196
@staticmethod
@@ -243,10 +222,10 @@ class add(Instruction):
243
222
@staticmethod
244
223
def opcode ():
245
224
return 0
246
-
225
+
247
226
@staticmethod
248
- def size ( ):
249
- return 1
227
+ def pc ( pc , ** kwargs ):
228
+ return pc + 1
250
229
251
230
@staticmethod
252
231
def binary (operands , ** kwargs ):
@@ -262,10 +241,10 @@ class neg(Instruction):
262
241
@staticmethod
263
242
def opcode ():
264
243
return 1
265
-
244
+
266
245
@staticmethod
267
- def size ( ):
268
- return 1
246
+ def pc ( pc , operands ):
247
+ return pc + 1
269
248
270
249
@staticmethod
271
250
def binary (operands , ** kwargs ):
@@ -282,10 +261,10 @@ class addi(Instruction):
282
261
@staticmethod
283
262
def opcode ():
284
263
return 2
285
-
264
+
286
265
@staticmethod
287
- def size ( ):
288
- return 1
266
+ def pc ( pc , ** kwargs ):
267
+ return pc + 1
289
268
290
269
@staticmethod
291
270
def binary (operands , ** kwargs ):
@@ -304,8 +283,8 @@ def opcode():
304
283
return 3
305
284
306
285
@staticmethod
307
- def size ( ):
308
- return 1
286
+ def pc ( pc , ** kwargs ):
287
+ return pc + 1
309
288
310
289
@staticmethod
311
290
def binary (operands , ** kwargs ):
@@ -324,8 +303,8 @@ def opcode():
324
303
return 4
325
304
326
305
@staticmethod
327
- def size ( ):
328
- return 1
306
+ def pc ( pc , ** kwargs ):
307
+ return pc + 1
329
308
330
309
@staticmethod
331
310
def binary (operands , ** kwargs ):
@@ -343,8 +322,8 @@ def opcode():
343
322
return 5
344
323
345
324
@staticmethod
346
- def size ( ):
347
- return 1
325
+ def pc ( pc , ** kwargs ):
326
+ return pc + 1
348
327
349
328
@staticmethod
350
329
def binary (operands , ** kwargs ):
@@ -365,8 +344,8 @@ def opcode():
365
344
return 6
366
345
367
346
@staticmethod
368
- def size ( ):
369
- return 1
347
+ def pc ( pc , ** kwargs ):
348
+ return pc + 1
370
349
371
350
@staticmethod
372
351
def binary (operands , ** kwargs ):
@@ -385,8 +364,8 @@ def opcode():
385
364
return 7
386
365
387
366
@staticmethod
388
- def size ( ):
389
- return 1
367
+ def pc ( pc , ** kwargs ):
368
+ return pc + 1
390
369
391
370
@staticmethod
392
371
def binary (operands , ** kwargs ):
@@ -413,8 +392,8 @@ def opcode():
413
392
return None
414
393
415
394
@staticmethod
416
- def size ( ):
417
- return 4
395
+ def pc ( pc , ** kwargs ):
396
+ return pc + 4
418
397
419
398
@staticmethod
420
399
def binary (operands , ** kwargs ):
@@ -458,8 +437,8 @@ def opcode():
458
437
return None
459
438
460
439
@staticmethod
461
- def size ( ):
462
- return 1
440
+ def pc ( pc , ** kwargs ):
441
+ return pc + 1
463
442
464
443
@staticmethod
465
444
def binary (operands , ** kwargs ):
@@ -476,8 +455,8 @@ def opcode():
476
455
return None
477
456
478
457
@staticmethod
479
- def size ( ):
480
- return 1
458
+ def pc ( pc , ** kwargs ):
459
+ return pc + 1
481
460
482
461
@staticmethod
483
462
def binary (operands , ** kwargs ):
@@ -496,8 +475,8 @@ def opcode():
496
475
return None
497
476
498
477
@staticmethod
499
- def size ( ):
500
- return 1
478
+ def pc ( pc , ** kwargs ):
479
+ return pc + 1
501
480
502
481
@staticmethod
503
482
def binary (operands , ** kwargs ):
@@ -506,3 +485,55 @@ def binary(operands, **kwargs):
506
485
@staticmethod
507
486
def hex (operands , ** kwargs ):
508
487
return [__bin2hex__ (instr ) for instr in halt .binary (operands , ** kwargs )]
488
+
489
+
490
+
491
+
492
+ # Public Functions
493
+ def receive_params (value_table ):
494
+ if value_table :
495
+ raise RuntimeError ('Custom parameters are not supported' )
496
+
497
+ def is_blank (line ):
498
+ """Return whether a line is blank and not an instruction."""
499
+ return __RE_BLANK__ .match (line ) is not None
500
+
501
+ def get_parts (line ):
502
+ """Break down an instruction into 3 parts: Label, Opcode, Operand"""
503
+ m = __RE_PARTS__ .match (line )
504
+ try :
505
+ return m .group ('Label' ), m .group ('Opcode' ), m .group ('Operands' )
506
+ except :
507
+ return None
508
+
509
+ def instruction_class (name ):
510
+ """Translate a given instruction name to its corresponding class name."""
511
+ return ALIASES .get (name , name )
512
+
513
+ def validate_pc (pc ):
514
+ """Returns or modifies the PC to a permitted value, if possible. Throws an error if the PC is invalid."""
515
+ if pc >= 2 ** BIT_WIDTH :
516
+ raise RuntimeError ("PC value {} is too large for {} bits." .format (pc , BIT_WIDTH ))
517
+
518
+ return pc
519
+
520
+ def output_generator (assembled_dict , output_format = 'binary' ):
521
+ """Returns a generator that creates output from {pc : assembly}-formatted dictionary."""
522
+ pc = 0
523
+ count = 0
524
+
525
+ while count < len (assembled_dict ):
526
+ if pc in assembled_dict :
527
+ yield assembled_dict [pc ]
528
+ pc += 1
529
+ count += 1
530
+ else :
531
+ if output_format == 'hex' :
532
+ results = noop .hex ()
533
+ elif output_format == 'binary' :
534
+ results = noop .binary ()
535
+
536
+ for r in results :
537
+ yield r
538
+
539
+ pc = noop .pc (pc )
0 commit comments