Advertisement
flip_floopOG

Microcode EEPROM file generation 3x 28c256

Apr 23rd, 2024 (edited)
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.83 KB | None | 0 0
  1. output1 = [0] * 32768
  2. output2 = [0] * 32768
  3. output3 = [0] * 32768
  4.  
  5.  
  6. # define opcodes
  7. LDA =  0b00000001
  8. ADD =  0b00000010
  9. SUB =  0b00000011
  10. STA =  0b00000100
  11. LDI =  0b00000101
  12. JMP =  0b00000110
  13. JZ  =  0b00000111
  14. JC  =  0b00001000
  15. ADI =  0b00001001
  16. OUT =  0b00001110
  17. HALT = 0b00001111
  18.  
  19. # define control signals
  20. HLT =  0b100000000000000000000000
  21. MI  =  0b010000000000000000000000
  22. RI  =  0b001000000000000000000000
  23. RO  =  0b000100000000000000000000
  24. IO  =  0b000010000000000000000000
  25. II  =  0b000001000000000000000000
  26. AI  =  0b000000100000000000000000
  27. AO   = 0b000000010000000000000000
  28.  
  29. EO   = 0b000000001000000000000000
  30. SU   = 0b000000000100000000000000
  31. BI   = 0b000000000010000000000000
  32. OI   = 0b000000000001000000000000
  33. CE   = 0b000000000000100000000000
  34. CO   = 0b000000000000010000000000
  35. J    = 0b000000000000001000000000
  36. FI   = 0b000000000000000100000000
  37.  
  38. STI  = 0b000000000000000010000000
  39. STO  = 0b000000000000000001000000
  40. STIN = 0b000000000000000000100000
  41. STDC = 0b000000000000000000010000
  42.  
  43.  
  44.  
  45. # define CPU flags EEPROM address bits
  46. CF_ADDR_BIT = 13
  47. ZF_ADDR_BIT = 12
  48.  
  49. # define microcode array
  50. microcode = [
  51.     [LDA, MI|CO, RO|II|CE, IO|MI, RO|AI],              # 00000001 LDA
  52.     [ADD, MI|CO, RO|II|CE, IO|MI, RO|BI, EO|AI|FI],    # 00000010 ADD
  53.     [SUB, MI|CO, RO|II|CE, IO|MI, RO|BI, EO|AI|SU|FI], # 00000011 SUB
  54.     [STA, MI|CO, RO|II|CE, IO|MI, AO|RI],              # 00000100 STA
  55.     [LDI, MI|CO, RO|II|CE, IO|AI],                     # 00000101 LDI
  56.     [JMP, MI|CO, RO|II|CE, IO|J],                      # 00000110 JMP
  57.     [JZ,  MI|CO, RO|II|CE],                            # 00000111 JZ
  58.     [JC,  MI|CO, RO|II|CE],                            # 00001000 JC
  59.     [ADI, MI|CO, RO|II|CE, IO|BI, EO|AI|FI],           # 00001001 ADI
  60.     [OUT, MI|CO, RO|II|CE, AO|OI],                     # 00001110 OUT
  61.     [HALT, MI|CO, RO|II|CE, HLT]                       # 00001111 HLT
  62. ]
  63.  
  64. # populate ROM with the first two micro-instruction stages for all possible opcodes
  65. for i in range(4096):
  66.     address = i << 3
  67.     output1[address] = ((MI|CO) & 0xFF00) >> 16
  68.     output1[address+1] = ((RO|II|CE) & 0xFF00) >> 16
  69.     output2[address] = ((MI|CO) & 0x00FF) >> 8
  70.     output2[address+1] = ((RO|II|CE) & 0x00FF) >> 8
  71.     output3[address] = (MI|CO) & 0x00FF
  72.     output3[address+1] = (RO|II|CE) & 0x00FF
  73.  
  74. # load ROM with micro-instructions    
  75. for cf in [0,1]:
  76.     for zf in [0,1]:
  77.  
  78.         # calculate base address as a function of CF and ZF
  79.         base_addr = (cf * 2 ** CF_ADDR_BIT) | (zf * 2 ** ZF_ADDR_BIT)
  80.  
  81.         # set up JC microcode according to CF flag value
  82.         if cf == 1:
  83.             # override default JC micro-code for CF = 1
  84.             microcode[7] = [JC, MI|CO, RO|II|CE, IO|J]
  85.         else:
  86.             microcode[7] = [JC, MI|CO, RO|II|CE]
  87.  
  88.         # set up JZ microcode according to ZF flag value
  89.         if zf == 1:
  90.             # override default JZ micro-code for ZF = 1
  91.             microcode[6] = [JZ, MI|CO, RO|II|CE, IO|J]
  92.         else:
  93.             microcode[6] = [JZ, MI|CO, RO|II|CE]
  94.  
  95.         # populate micro-code
  96.         for inst in microcode:
  97.             opCode = inst[0]
  98.             signals = inst[1:]
  99.             for stage, signal in enumerate(signals):
  100.                 address = base_addr | (opCode << 3) | stage
  101.                 output1[address] = (signal & 0xFF00) >> 16
  102.                 output2[address] = (signal & 0x00FF) >>8
  103.                 output3[address] = signal & 0x00FF
  104.  
  105. with open('eeprom1.bin', 'wb') as file1:
  106.     for x in output1:
  107.         file1.write(x.to_bytes(1, byteorder='big', signed=False))
  108.  
  109. with open('eeprom2.bin', 'wb') as file2:
  110.     for x in output2:
  111.         file2.write(x.to_bytes(1, byteorder='big', signed=False))
  112.        
  113. with open('eeprom3.bin', 'wb') as file3:
  114.     for x in output3:
  115.         file3.write(x.to_bytes(1, byteorder='big', signed=False))
  116.  
  117.  
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement