Advertisement
NeverCast

ALU Lut Gen

Aug 30th, 2018
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.35 KB | None | 0 0
  1. INPUT_WIDTH = 8
  2. OUT_WIDTH = 8
  3.  
  4. stream = open('alu_lut.hex', 'wb')
  5.  
  6. class Operation:
  7.     def __init__(self, function_num, name, transform):
  8.         self.num = function_num
  9.         self.name = name
  10.         self.transform = transform
  11.  
  12. def safe_div(x, y):
  13.     if y == 0:
  14.         return 0
  15.     return x // y
  16.  
  17. def safe_mod(x, y):
  18.     if y == 0:
  19.         return 0
  20.     _, mod = divmod(x, y)
  21.     return mod
  22.  
  23. operations = [
  24.     Operation(0, 'add', lambda x,y: x + y),
  25.     Operation(1, 'sub', lambda x,y: x - y),
  26.     Operation(2, 'or', lambda x,y: x | y),
  27.     Operation(3, 'and', lambda x,y: x & y),
  28.     Operation(4, 'xor', lambda x,y: x ^ y),
  29.     Operation(5, 'lshift', lambda x,y: x << y & 0x03),
  30.     Operation(6, 'rshift', lambda x,y: x >> y & 0x03),
  31.     Operation(7, 'mul', lambda x, y: x * y),
  32.     Operation(8, 'div', safe_div),
  33.     Operation(9, 'mod', safe_mod)
  34. ]
  35.  
  36. stream.write(bytes('v2.0 raw\r\n', 'ascii'))
  37. def generate():
  38.     for funcno in range(len(operations)):
  39.         operation = operations[funcno]
  40.         print('Generating', operation.name)
  41.         for y in range(2**INPUT_WIDTH):
  42.             for x in range(2**INPUT_WIDTH):
  43.                 result = int(operation.transform(x, y)) & 2**OUT_WIDTH
  44.                 stream.write('{:x}\r\n'.format(result).encode('ascii'))
  45.     stream.flush()
  46.  
  47. if __name__ == '__main__':
  48.     generate()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement