Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- INPUT_WIDTH = 8
- OUT_WIDTH = 8
- stream = open('alu_lut.hex', 'wb')
- class Operation:
- def __init__(self, function_num, name, transform):
- self.num = function_num
- self.name = name
- self.transform = transform
- def safe_div(x, y):
- if y == 0:
- return 0
- return x // y
- def safe_mod(x, y):
- if y == 0:
- return 0
- _, mod = divmod(x, y)
- return mod
- operations = [
- Operation(0, 'add', lambda x,y: x + y),
- Operation(1, 'sub', lambda x,y: x - y),
- Operation(2, 'or', lambda x,y: x | y),
- Operation(3, 'and', lambda x,y: x & y),
- Operation(4, 'xor', lambda x,y: x ^ y),
- Operation(5, 'lshift', lambda x,y: x << y & 0x03),
- Operation(6, 'rshift', lambda x,y: x >> y & 0x03),
- Operation(7, 'mul', lambda x, y: x * y),
- Operation(8, 'div', safe_div),
- Operation(9, 'mod', safe_mod)
- ]
- stream.write(bytes('v2.0 raw\r\n', 'ascii'))
- def generate():
- for funcno in range(len(operations)):
- operation = operations[funcno]
- print('Generating', operation.name)
- for y in range(2**INPUT_WIDTH):
- for x in range(2**INPUT_WIDTH):
- result = int(operation.transform(x, y)) & 2**OUT_WIDTH
- stream.write('{:x}\r\n'.format(result).encode('ascii'))
- stream.flush()
- if __name__ == '__main__':
- generate()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement