generic_sram.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. """
  2. # TOP2049 Open Source programming suite
  3. #
  4. # Generic SRAM chip
  5. #
  6. # Copyright (c) 2011 Michael Buesch <m@bues.ch>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. """
  22. from libtoprammer.chip import *
  23. class Chip_genericSRAM(Chip):
  24. def __init__(self, chipPackage, chipPinVCC, chipPinGND,
  25. VCCVoltage,
  26. nrAddressBits, nrDataBits):
  27. Chip.__init__(self,
  28. chipPackage = chipPackage,
  29. chipPinVCC = chipPinVCC,
  30. chipPinGND = chipPinGND)
  31. self.VCCVoltage = VCCVoltage
  32. self.nrAddressBits = nrAddressBits
  33. self.nrAddressBytes = int(math.ceil((float(self.nrAddressBits) - 0.1) / 8))
  34. self.nrDataBits = nrDataBits
  35. assert(nrDataBits == 8)
  36. def erase(self):
  37. self.writeRAM(int2byte(0) * self.__sizeBytes())
  38. def test(self):
  39. generic = GenericAlgorithms(self)
  40. generic.simpleTest(self.readRAM, self.writeRAM,
  41. self.__sizeBytes())
  42. def readRAM(self):
  43. image = []
  44. self.progressMeterInit("Reading SRAM", self.__sizeBytes())
  45. self.__turnOnChip()
  46. self.__setControlPins(CE=0, OE=0, WE=1)
  47. nrBytes = 0
  48. for addr in range(0, self.__sizeBytes()):
  49. self.progressMeter(addr)
  50. self.__setAddress(addr)
  51. self.__readData()
  52. nrBytes += 1
  53. if nrBytes == self.top.getBufferRegSize():
  54. image.append(self.top.cmdReadBufferReg(nrBytes))
  55. nrBytes = 0
  56. image.append(self.top.cmdReadBufferReg(nrBytes))
  57. self.__setControlPins(CE=1, OE=1, WE=1)
  58. self.progressMeterFinish()
  59. return b"".join(image)
  60. def writeRAM(self, image):
  61. if len(image) > self.__sizeBytes():
  62. self.throwError("Invalid memory image size %d (expected max %d)" %\
  63. (len(image), self.__sizeBytes()))
  64. self.progressMeterInit("Writing SRAM", self.__sizeBytes())
  65. self.__turnOnChip()
  66. self.__setControlPins(CE=0, OE=1, WE=1)
  67. for addr in range(0, len(image)):
  68. self.progressMeter(addr)
  69. self.__setAddress(addr)
  70. self.__writeData(image[addr])
  71. self.__setControlPins(CE=0, OE=1, WE=0)
  72. self.top.cmdDelay(0.00000007) # Delay at least 70 nsec
  73. self.__setControlPins(CE=0, OE=1, WE=1)
  74. self.__setControlPins(CE=1, OE=1, WE=1)
  75. self.progressMeterFinish()
  76. def __sizeBytes(self):
  77. return (1 << self.nrAddressBits)
  78. def __turnOnChip(self):
  79. self.__setControlPins(CE=1, OE=1, WE=1)
  80. self.top.cmdSetVCCVoltage(self.VCCVoltage)
  81. self.applyGND(True)
  82. self.applyVCC(True)
  83. self.lastAddress = None
  84. def __setControlPins(self, CE=1, OE=1, WE=1):
  85. value = 0
  86. if CE:
  87. value |= 1
  88. if OE:
  89. value |= 2
  90. if WE:
  91. value |= 4
  92. self.top.cmdFPGAWrite(0x11, value)
  93. def __writeData(self, data):
  94. data = byte2int(data)
  95. self.top.cmdFPGAWrite(0x10, data)
  96. def __readData(self):
  97. self.top.cmdFPGARead(0x10)
  98. def __setAddress(self, addr):
  99. for i in range(0, self.nrAddressBytes):
  100. shift = 8 * i
  101. mask = 0xFF << shift
  102. if self.lastAddress is None or\
  103. (self.lastAddress & mask) != (addr & mask):
  104. self.top.cmdFPGAWrite(0x12 + i,
  105. (addr & mask) >> shift)
  106. self.lastAddress = addr