Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.63 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3.  
  4. # Address space is 0x00 00 - 0x07 FF or 0 - 2047
  5. # Address bits
  6. # 0000 0000 0000 0000
  7. # xxxx x        - unused
  8. # .... .n       - Mode switch 0 positive 1 = twos complement
  9. # .... ..nn     - Digit select 00 = ones, 01 = tens, 10 = hundreds, 11 = sign
  10. # .... .... nnnn nnnn   - Value
  11.  
  12. # Two's Complement
  13. # 0-127 as positive
  14. # -128  1000 0000   0x80
  15. # -1    1111 1111   0xFF
  16.  
  17. # Data bits
  18. # Dabc defg     - Segments on display
  19.  
  20. digits = [ 0x7E, 0x30, 0x6D, 0x79, 0x33, 0x5B, 0x5F, 0x70, 0x7F, 0x7B ]
  21.  
  22. rom = bytearray([0x00] * 2048)
  23.  
  24. # Positive values with leading zeros
  25. # For i in range(0-255)
  26. # Ones place 0-9 repeating over 0x00 00 - 0x00 FF   digits[ i        % 10   ]
  27. # Tens place 0-9 repeating over 0x01 00 - 0x01 FF   digits[ int(i / 10)  % 10   ]
  28. # Hundreds   0-9 repeating over 0x10 00 - 0x10 FF   digits[ int(i / 100) % 10   ]
  29. # Sign 0x03 00 - 0x03 FF - everything initialized to 0x00
  30.  
  31. # Positive values without leading zeros
  32. # Ones:     0x00 00 - 0x00 FF = digits[i % 10]
  33.  
  34. for i in range(0x00,(0xFF + 1)):
  35.     rom[i] = digits[i % 10]
  36.  
  37. # Tens:     0x01 0A - 0x01 FF = digits[ int(i / 10) % 10]
  38.  
  39.     if(i >= 0x0A):
  40.         rom[i + 0x100] = digits[int(i / 10) % 10]
  41.  
  42. # Hundreds: 0x02 64 - 0x02 FF = digits[int(i / 100) % 10]
  43.  
  44.     if(i >= 0x64):
  45.         rom[i + 0x200] = digits[int(i / 100) % 10]
  46.  
  47. # Sign:  Do nothing for rom[i + 0x0300]
  48.  
  49. # ------------------
  50.  
  51. # Two's Complement
  52. # Ones: 0x04 xx, Tens: 0x05 xx, Hundreds: 0x06 xx, Sign 0x07 xx
  53. # 0000 01xx 0000 0000   = 0 Ones: rom[0x04 00 - 0x04 7F]
  54. # 0000 01xx 0000 0001   = 1
  55. # 0000 01xx 0111 1111   = 127
  56. # 0000 01xx 1000 0000   = -128  0x80
  57. # 0000 01xx 1111 1111   = -1
  58.  
  59. def twoComp(number):
  60.     return 0xff & -number
  61.     # This returns positive integers corresponding to 0x80 = 128 and 0xFF = 1
  62.     # which can be chopped up by the usual dividing by ten and 100 to get the
  63.     # right digits to display, and the sign digit is just always set
  64.  
  65. for i in range(0x00,(0x7F + 1)):
  66. # Positive ones digit
  67.     rom[i + 0x0400] = digits[i % 10]
  68.  
  69. # Positive tens digit
  70.     if(i >= 0x0A):
  71.         rom[i + 0x500] = digits[int(i / 10) % 10]
  72.  
  73. # Positive hundreds digit
  74.     if(i >= 0x64):
  75.         rom[i + 0x600] = digits[int(i / 100) % 10]
  76.  
  77. # Positive sign digit do nothing 0x0700 - 0x077F
  78.    
  79. for i in range(0x80,(0xFF + 1)):
  80. # Negative ones digit
  81.     rom[i + 0x400] = digits[twoComp(i) % 10]
  82.  
  83. # Negative tens digit
  84.     if(i < 0xF7):
  85.         rom[i + 0x500] = digits[int(twoComp(i) / 10) % 10]
  86.  
  87. # Negative hundreds digit
  88.     if(i < 0x9D):
  89.         rom[i + 0x600] = digits[int(twoComp(i) / 100) % 10]
  90.  
  91. # Negative sign 'digit' set pin D0 for the middle bar on the 7-segment
  92.     rom[i + 0x700] = 0x01
  93.  
  94. # -----------------
  95.  
  96.  
  97. with open('out.bin','wb') as out:
  98.     out.write(rom)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement