USB Device Programming class project. This project allows a Python/Tk program running on a PC host to monitor/control a test-CPU programmed into an altera development board.

Dependencies:   C12832_lcd USBDevice mbed-rtos mbed mmSPI

Committer:
gatedClock
Date:
Sun Sep 01 02:58:57 2013 +0000
Revision:
4:92539904a4ad
Child:
8:db29cce17a33
add the project Python files.  the tab-formatting needs to be redone.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gatedClock 4:92539904a4ad 1 #!/usr/bin/python -tt # tt: detect mixed space/tab.
gatedClock 4:92539904a4ad 2 #---copyright-----------------------------------#-------------------------------
gatedClock 4:92539904a4ad 3 # licensed for personal and academic use.
gatedClock 4:92539904a4ad 4 # commercial use must be approved by the account-holder of
gatedClock 4:92539904a4ad 5 # gated.clock@gmail.com
gatedClock 4:92539904a4ad 6 #-------imports---------------------------------#-------------------------------
gatedClock 4:92539904a4ad 7 from Tkinter import * # Tk graphics.
gatedClock 4:92539904a4ad 8 import mmUSBserial # USB Serial object.
gatedClock 4:92539904a4ad 9 from mmUSBserial import *
gatedClock 4:92539904a4ad 10 import time # Time object.
gatedClock 4:92539904a4ad 11 from time import time, sleep
gatedClock 4:92539904a4ad 12 import threading # Threading object.
gatedClock 4:92539904a4ad 13 import tkFileDialog # File-select dialog object.'
gatedClock 4:92539904a4ad 14 import os # import os library.
gatedClock 4:92539904a4ad 15 #=======class===================================#===============================
gatedClock 4:92539904a4ad 16 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 17 '''
gatedClock 4:92539904a4ad 18 licensed for personal and accedemic use.
gatedClock 4:92539904a4ad 19 commercial use must be approved by the account-holder of
gatedClock 4:92539904a4ad 20 gated.clock@gmail.com
gatedClock 4:92539904a4ad 21 '''
gatedClock 4:92539904a4ad 22 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 23 '''
gatedClock 4:92539904a4ad 24 description:
gatedClock 4:92539904a4ad 25
gatedClock 4:92539904a4ad 26 python/Tk code for interfacing with the mbed.org system.
gatedClock 4:92539904a4ad 27 this code provides a UI which is used to communicate over USB
gatedClock 4:92539904a4ad 28 to the mbed.org processor, and in turn the toy CPU programmed
gatedClock 4:92539904a4ad 29 into the altera device.
gatedClock 4:92539904a4ad 30
gatedClock 4:92539904a4ad 31 this code provides a UI which is used to monitor and write-to the
gatedClock 4:92539904a4ad 32 toy CPU's registers and its main memory.
gatedClock 4:92539904a4ad 33
gatedClock 4:92539904a4ad 34 I/O capability:
gatedClock 4:92539904a4ad 35
gatedClock 4:92539904a4ad 36 * R0 write/read.
gatedClock 4:92539904a4ad 37 * R1 write/read.
gatedClock 4:92539904a4ad 38 * R2 write/read.
gatedClock 4:92539904a4ad 39 * R3 write/read.
gatedClock 4:92539904a4ad 40 * PC write/read. (that's 'Program Counter'.)
gatedClock 4:92539904a4ad 41 * IR write/read. (that's 'Instruction Register'.)
gatedClock 4:92539904a4ad 42 * MM address. (that's 'Main Memory.')
gatedClock 4:92539904a4ad 43 * MM content.
gatedClock 4:92539904a4ad 44
gatedClock 4:92539904a4ad 45 Button Controls:
gatedClock 4:92539904a4ad 46
gatedClock 4:92539904a4ad 47 * USB Connect/Disconnect.
gatedClock 4:92539904a4ad 48 * Register Read.
gatedClock 4:92539904a4ad 49 * Register Write.
gatedClock 4:92539904a4ad 50 * Main Memory Read.
gatedClock 4:92539904a4ad 51 * Main Memory Write.
gatedClock 4:92539904a4ad 52 * Main Memory Load-From-File.
gatedClock 4:92539904a4ad 53 * Main Memory Dump-To-File.
gatedClock 4:92539904a4ad 54 * Step CPU.
gatedClock 4:92539904a4ad 55 * Run-Fast, Run-Slow, Stop CPU.
gatedClock 4:92539904a4ad 56 * Run Test.
gatedClock 4:92539904a4ad 57 * Exit.
gatedClock 4:92539904a4ad 58
gatedClock 4:92539904a4ad 59 '''
gatedClock 4:92539904a4ad 60 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 61 if (1): # allow further indentation.
gatedClock 4:92539904a4ad 62 class mmUI: # UI class.
gatedClock 4:92539904a4ad 63 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 64 def __init__(self,masterWidget): # constructor.
gatedClock 4:92539904a4ad 65
gatedClock 4:92539904a4ad 66 self.__masterWidget = masterWidget # promote to object scope.
gatedClock 4:92539904a4ad 67
gatedClock 4:92539904a4ad 68 # master widget title.
gatedClock 4:92539904a4ad 69 self.__masterWidget.wm_title("CPU Control")
gatedClock 4:92539904a4ad 70
gatedClock 4:92539904a4ad 71 self.__frame = Frame(masterWidget) # create frame widget.
gatedClock 4:92539904a4ad 72 self.__frame.grid(column=0, row=0) # use grid packer.
gatedClock 4:92539904a4ad 73
gatedClock 4:92539904a4ad 74 self.__connected = 0 # not yet __connected to USB.
gatedClock 4:92539904a4ad 75 self.__isRunning = 0 # program execution not running.
gatedClock 4:92539904a4ad 76 self.__testRunning = 0 # test mode not runing.
gatedClock 4:92539904a4ad 77 self.__testIter = 0 # test mode iteration count.
gatedClock 4:92539904a4ad 78 self.__sleepVal = 0.04 # serial transceive sleep.
gatedClock 4:92539904a4ad 79
gatedClock 4:92539904a4ad 80
gatedClock 4:92539904a4ad 81 self.uiR0 (0, 1, 10) # label/entry widgets (x, y, width)
gatedClock 4:92539904a4ad 82 self.uiR1 (0, 2, 10)
gatedClock 4:92539904a4ad 83 self.uiR2 (0, 3, 10)
gatedClock 4:92539904a4ad 84 self.uiR3 (0, 4, 10)
gatedClock 4:92539904a4ad 85 self.uiPC (0, 5, 10)
gatedClock 4:92539904a4ad 86 self.uiIR (0, 6, 10)
gatedClock 4:92539904a4ad 87 self.uimmADR (0, 7, 10)
gatedClock 4:92539904a4ad 88 self.uimmVAL (0, 8, 10)
gatedClock 4:92539904a4ad 89
gatedClock 4:92539904a4ad 90 self.uiConnect (3, 1, 8) # button widgets (x, y, width)
gatedClock 4:92539904a4ad 91 self.uiRegRead (3, 2, 8)
gatedClock 4:92539904a4ad 92 self.uiRegWrite(3, 3, 8)
gatedClock 4:92539904a4ad 93 self.uimmRead (3, 4, 8)
gatedClock 4:92539904a4ad 94 self.uimmWrite (3, 5, 8)
gatedClock 4:92539904a4ad 95 self.uiProg (3, 6, 8)
gatedClock 4:92539904a4ad 96 self.uimmDump (3, 7, 8)
gatedClock 4:92539904a4ad 97 self.uiStep (3, 8, 8)
gatedClock 4:92539904a4ad 98 self.uiRun (3, 9, 8)
gatedClock 4:92539904a4ad 99 self.uiTest (3, 10, 8)
gatedClock 4:92539904a4ad 100 self.uiExit (3, 11, 8)
gatedClock 4:92539904a4ad 101
gatedClock 4:92539904a4ad 102 self.refreshUI() # initial content display.
gatedClock 4:92539904a4ad 103
gatedClock 4:92539904a4ad 104 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 105 def __del__(self): # destructor.
gatedClock 4:92539904a4ad 106 print "object destroyed."
gatedClock 4:92539904a4ad 107 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 108 # WIDGETS: BEGIN LABEL/ENTRY PAIRS. #===============================
gatedClock 4:92539904a4ad 109 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 110 def uiR0(self,dColumn,dRow,dWidth): # R0.
gatedClock 4:92539904a4ad 111
gatedClock 4:92539904a4ad 112 dColumnPlusOne = dColumn + 1
gatedClock 4:92539904a4ad 113
gatedClock 4:92539904a4ad 114 self.__R0Label = Label(self.__frame, text="R0")
gatedClock 4:92539904a4ad 115 self.__R0Label.grid(column=dColumn, row=dRow, sticky=(E))
gatedClock 4:92539904a4ad 116
gatedClock 4:92539904a4ad 117 self.__R0Entry = Entry(self.__frame, width=dWidth, background="GREEN")
gatedClock 4:92539904a4ad 118 self.__R0Entry.grid(column=dColumnPlusOne, row=dRow, sticky=(W))
gatedClock 4:92539904a4ad 119 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 120 def uiR1(self,dColumn,dRow,dWidth): # R1.
gatedClock 4:92539904a4ad 121
gatedClock 4:92539904a4ad 122 dColumnPlusOne = dColumn + 1
gatedClock 4:92539904a4ad 123
gatedClock 4:92539904a4ad 124 self.__R1Label = Label(self.__frame, text="R1")
gatedClock 4:92539904a4ad 125 self.__R1Label.grid(column=dColumn, row=dRow, sticky=(E))
gatedClock 4:92539904a4ad 126
gatedClock 4:92539904a4ad 127 self.__R1Entry = Entry(self.__frame, width=dWidth, background="GREEN")
gatedClock 4:92539904a4ad 128 self.__R1Entry.grid(column=dColumnPlusOne, row=dRow, sticky=(W))
gatedClock 4:92539904a4ad 129 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 130 def uiR2(self,dColumn,dRow,dWidth): # R2.
gatedClock 4:92539904a4ad 131
gatedClock 4:92539904a4ad 132 dColumnPlusOne = dColumn + 1
gatedClock 4:92539904a4ad 133
gatedClock 4:92539904a4ad 134 self.__R2Label = Label(self.__frame, text="R2")
gatedClock 4:92539904a4ad 135 self.__R2Label.grid(column=dColumn, row=dRow, sticky=(E))
gatedClock 4:92539904a4ad 136
gatedClock 4:92539904a4ad 137 self.__R2Entry = Entry(self.__frame, width=dWidth, background="GREEN")
gatedClock 4:92539904a4ad 138 self.__R2Entry.grid(column=dColumnPlusOne, row=dRow, sticky=(W))
gatedClock 4:92539904a4ad 139 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 140 def uiR3(self,dColumn,dRow,dWidth): # R3.
gatedClock 4:92539904a4ad 141
gatedClock 4:92539904a4ad 142 dColumnPlusOne = dColumn + 1
gatedClock 4:92539904a4ad 143
gatedClock 4:92539904a4ad 144 self.__R3Label = Label(self.__frame, text="R3")
gatedClock 4:92539904a4ad 145 self.__R3Label.grid(column=dColumn, row=dRow, sticky=(E))
gatedClock 4:92539904a4ad 146
gatedClock 4:92539904a4ad 147 self.__R3Entry = Entry(self.__frame, width=dWidth, background="GREEN")
gatedClock 4:92539904a4ad 148 self.__R3Entry.grid(column=dColumnPlusOne, row=dRow, sticky=(W))
gatedClock 4:92539904a4ad 149 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 150 def uiPC(self,dColumn,dRow,dWidth): # program counter.
gatedClock 4:92539904a4ad 151
gatedClock 4:92539904a4ad 152 dColumnPlusOne = dColumn + 1
gatedClock 4:92539904a4ad 153
gatedClock 4:92539904a4ad 154 self.__PCLabel = Label(self.__frame, text="PC")
gatedClock 4:92539904a4ad 155 self.__PCLabel.grid(column=dColumn, row=dRow, sticky=(E))
gatedClock 4:92539904a4ad 156
gatedClock 4:92539904a4ad 157 self.__PCEntry = Entry(self.__frame, width=dWidth, background="ORANGE")
gatedClock 4:92539904a4ad 158 self.__PCEntry.grid(column=dColumnPlusOne, row=dRow, sticky=(W))
gatedClock 4:92539904a4ad 159 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 160 def uiIR(self,dColumn,dRow,dWidth): # instruction register.
gatedClock 4:92539904a4ad 161
gatedClock 4:92539904a4ad 162 dColumnPlusOne = dColumn + 1
gatedClock 4:92539904a4ad 163
gatedClock 4:92539904a4ad 164 self.__IRLabel = Label(self.__frame, text="IR")
gatedClock 4:92539904a4ad 165 self.__IRLabel.grid(column=dColumn, row=dRow, sticky=(E))
gatedClock 4:92539904a4ad 166
gatedClock 4:92539904a4ad 167 self.__IREntry = Entry(self.__frame, width=dWidth, background="ORANGE")
gatedClock 4:92539904a4ad 168 self.__IREntry.grid(column=dColumnPlusOne, row=dRow, sticky=(W))
gatedClock 4:92539904a4ad 169 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 170 def uimmADR(self,dColumn,dRow,dWidth):# mmADR.
gatedClock 4:92539904a4ad 171
gatedClock 4:92539904a4ad 172 dColumnPlusOne = dColumn + 1
gatedClock 4:92539904a4ad 173
gatedClock 4:92539904a4ad 174 self.__mmADRLabel = Label(self.__frame, text="mmADR")
gatedClock 4:92539904a4ad 175 self.__mmADRLabel.grid(column=dColumn, row=dRow, sticky=(E))
gatedClock 4:92539904a4ad 176
gatedClock 4:92539904a4ad 177 self.__mmADREntry = Entry(self.__frame, width=dWidth,
gatedClock 4:92539904a4ad 178 background="MAROON", foreground="WHITE")
gatedClock 4:92539904a4ad 179 self.__mmADREntry.grid(column=dColumnPlusOne, row=dRow, sticky=(W))
gatedClock 4:92539904a4ad 180 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 181 def uimmVAL(self,dColumn,dRow,dWidth):# mmVAL.
gatedClock 4:92539904a4ad 182
gatedClock 4:92539904a4ad 183 dColumnPlusOne = dColumn + 1
gatedClock 4:92539904a4ad 184
gatedClock 4:92539904a4ad 185 self.__mmVALLabel = Label(self.__frame, text="mmVAL")
gatedClock 4:92539904a4ad 186 self.__mmVALLabel.grid(column=dColumn, row=dRow, sticky=(E))
gatedClock 4:92539904a4ad 187
gatedClock 4:92539904a4ad 188 self.__mmVALEntry = Entry(self.__frame, width=dWidth,
gatedClock 4:92539904a4ad 189 background="MAROON", foreground="WHITE")
gatedClock 4:92539904a4ad 190 self.__mmVALEntry.grid(column=dColumnPlusOne, row=dRow, sticky=(W))
gatedClock 4:92539904a4ad 191 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 192 # WIDGETS: BEGIN BUTTONS. #===============================
gatedClock 4:92539904a4ad 193 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 194 # generate the 'connect' button.
gatedClock 4:92539904a4ad 195 def uiConnect(self,dColumn,dRow,dWidth):
gatedClock 4:92539904a4ad 196
gatedClock 4:92539904a4ad 197 self.__connectButton = Button(self.__frame, text="CONNECT",
gatedClock 4:92539904a4ad 198 command=self.selectConnectButtonPressed,
gatedClock 4:92539904a4ad 199 background="RED",
gatedClock 4:92539904a4ad 200 width = dWidth)
gatedClock 4:92539904a4ad 201
gatedClock 4:92539904a4ad 202 self.__connectButton.grid(column=dColumn, row=dRow, sticky=(E))
gatedClock 4:92539904a4ad 203 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 204 # generate the register-read button.
gatedClock 4:92539904a4ad 205 def uiRegRead(self,dColumn,dRow,dWidth):
gatedClock 4:92539904a4ad 206 self.__RegReadButton = Button(self.__frame, text="REG READ",
gatedClock 4:92539904a4ad 207 command=self.selectRegReadButtonPressed,
gatedClock 4:92539904a4ad 208 width = dWidth)
gatedClock 4:92539904a4ad 209
gatedClock 4:92539904a4ad 210 self.__RegReadButton.grid(column=dColumn, row=dRow, sticky=(W))
gatedClock 4:92539904a4ad 211 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 212 # generate the 'reg write' button.
gatedClock 4:92539904a4ad 213 def uiRegWrite(self,dColumn,dRow,dWidth):
gatedClock 4:92539904a4ad 214 self.__RegWriteButton = Button(self.__frame, text="REG WRITE",
gatedClock 4:92539904a4ad 215 command=self.selectRegWriteButtonPressed,
gatedClock 4:92539904a4ad 216 width = dWidth)
gatedClock 4:92539904a4ad 217
gatedClock 4:92539904a4ad 218 self.__RegWriteButton.grid(column=dColumn, row=dRow, sticky=(W))
gatedClock 4:92539904a4ad 219 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 220 # generate the mm-read button.
gatedClock 4:92539904a4ad 221 def uimmRead(self,dColumn,dRow,dWidth):
gatedClock 4:92539904a4ad 222 self.__mmReadButton = Button(self.__frame, text="MM READ",
gatedClock 4:92539904a4ad 223 command=self.selectmmReadButtonPressed,
gatedClock 4:92539904a4ad 224 width = dWidth)
gatedClock 4:92539904a4ad 225
gatedClock 4:92539904a4ad 226 self.__mmReadButton.grid(column=dColumn, row=dRow, sticky=(W))
gatedClock 4:92539904a4ad 227 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 228 # generate the 'mm write' button.
gatedClock 4:92539904a4ad 229 def uimmWrite(self,dColumn,dRow,dWidth):
gatedClock 4:92539904a4ad 230 self.__mmWriteButton = Button(self.__frame, text="MM WRITE",
gatedClock 4:92539904a4ad 231 command=self.selectmmWriteButtonPressed,
gatedClock 4:92539904a4ad 232 width = dWidth)
gatedClock 4:92539904a4ad 233
gatedClock 4:92539904a4ad 234 self.__mmWriteButton.grid(column=dColumn, row=dRow, sticky=(W))
gatedClock 4:92539904a4ad 235 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 236 def uiProg(self,dColumn,dRow,dWidth): # generate the 'program' button.
gatedClock 4:92539904a4ad 237 self.__ProgButton = Button(self.__frame, text="PROGRAM",
gatedClock 4:92539904a4ad 238 command=self.selectProgButtonPressed,
gatedClock 4:92539904a4ad 239 width = dWidth)
gatedClock 4:92539904a4ad 240
gatedClock 4:92539904a4ad 241 self.__ProgButton.grid(column=dColumn, row=dRow, sticky=(W))
gatedClock 4:92539904a4ad 242 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 243 # generate the 'mm dump' button.
gatedClock 4:92539904a4ad 244 def uimmDump(self,dColumn,dRow,dWidth):
gatedClock 4:92539904a4ad 245 self.__mmWriteButton = Button(self.__frame, text="DUMP",
gatedClock 4:92539904a4ad 246 command=self.selectmmDumpButtonPressed,
gatedClock 4:92539904a4ad 247 width = dWidth)
gatedClock 4:92539904a4ad 248
gatedClock 4:92539904a4ad 249 self.__mmWriteButton.grid(column=dColumn, row=dRow, sticky=(W))
gatedClock 4:92539904a4ad 250 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 251 def uiStep(self,dColumn,dRow,dWidth): # generate the 'step' button.
gatedClock 4:92539904a4ad 252 self.__StepButton = Button(self.__frame, text="STEP",
gatedClock 4:92539904a4ad 253 command=self.selectStepButtonPressed,
gatedClock 4:92539904a4ad 254 width = dWidth)
gatedClock 4:92539904a4ad 255
gatedClock 4:92539904a4ad 256 self.__StepButton.grid(column=dColumn, row=dRow, sticky=(W))
gatedClock 4:92539904a4ad 257 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 258 def uiRun(self,dColumn,dRow,dWidth): # generate the 'run' button.
gatedClock 4:92539904a4ad 259 self.__runButton = Button(self.__frame, text="RUN",
gatedClock 4:92539904a4ad 260 command=self.selectRunButtonPressed,width = dWidth)
gatedClock 4:92539904a4ad 261
gatedClock 4:92539904a4ad 262 self.__runButton.grid(column=dColumn, row=dRow, sticky=(E))
gatedClock 4:92539904a4ad 263 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 264 def uiTest(self,dColumn,dRow,dWidth): # generate the 'test' button.
gatedClock 4:92539904a4ad 265 self.__testButton = Button(self.__frame, text="TEST",
gatedClock 4:92539904a4ad 266 command=self.selectTestButtonPressed,width = dWidth)
gatedClock 4:92539904a4ad 267
gatedClock 4:92539904a4ad 268 self.__testButton.grid(column=dColumn, row=dRow, sticky=(E))
gatedClock 4:92539904a4ad 269 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 270 def uiExit(self,dColumn,dRow,dWidth): # generate the 'exit' button.
gatedClock 4:92539904a4ad 271
gatedClock 4:92539904a4ad 272 self.__isRunning = 0 # end the running thread.
gatedClock 4:92539904a4ad 273 sleep(2.0) # give thread time to stop.
gatedClock 4:92539904a4ad 274
gatedClock 4:92539904a4ad 275 self.__exitButton = Button(self.__frame, text="EXIT",
gatedClock 4:92539904a4ad 276 command=sys.exit,width = dWidth)
gatedClock 4:92539904a4ad 277
gatedClock 4:92539904a4ad 278 self.__exitButton.grid(column=dColumn, row=dRow, sticky=(E))
gatedClock 4:92539904a4ad 279 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 280 # BEGIN EXECUTE-WHEN-BUTTON-PRESSED CODE#===============================
gatedClock 4:92539904a4ad 281 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 282 def selectConnectButtonPressed(self): # execute CONNECT.
gatedClock 4:92539904a4ad 283 if (self.__connected == 0):
gatedClock 4:92539904a4ad 284 self.__iUSB = mmUSBserial(idVendor=0x1234, idProduct=0x0006)
gatedClock 4:92539904a4ad 285 self.__connected = 1
gatedClock 4:92539904a4ad 286 self.__connectButton["text"] = "DISCONNECT"
gatedClock 4:92539904a4ad 287 self.__connectButton["background"] = self.__frame.cget("background")
gatedClock 4:92539904a4ad 288 else:
gatedClock 4:92539904a4ad 289 del(self.__iUSB)
gatedClock 4:92539904a4ad 290 self.__connected = 0
gatedClock 4:92539904a4ad 291 self.__connectButton["text"] = "CONNECT"
gatedClock 4:92539904a4ad 292 self.__connectButton["background"] = "RED"
gatedClock 4:92539904a4ad 293 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 294 def selectRegReadButtonPressed(self): # REG READ button has been pressed.
gatedClock 4:92539904a4ad 295 self.refreshUI() # ensure proper text formats.
gatedClock 4:92539904a4ad 296
gatedClock 4:92539904a4ad 297
gatedClock 4:92539904a4ad 298 # read R0.
gatedClock 4:92539904a4ad 299 self.__iUSB.write(self.buildReadRegisterCommand("0"))
gatedClock 4:92539904a4ad 300 fetchTextR0 = self.__iUSB.read()
gatedClock 4:92539904a4ad 301 hexTextR0 = self.formatRegValPayload(fetchTextR0)
gatedClock 4:92539904a4ad 302 self.__R0Entry.delete(0,99)
gatedClock 4:92539904a4ad 303 self.__R0Entry.insert(0,hexTextR0)
gatedClock 4:92539904a4ad 304
gatedClock 4:92539904a4ad 305 # read R1.
gatedClock 4:92539904a4ad 306 self.__iUSB.write(self.buildReadRegisterCommand("1"))
gatedClock 4:92539904a4ad 307 fetchTextR1 = self.__iUSB.read()
gatedClock 4:92539904a4ad 308 hexTextR1 = self.formatRegValPayload(fetchTextR1)
gatedClock 4:92539904a4ad 309 self.__R1Entry.delete(0,99)
gatedClock 4:92539904a4ad 310 self.__R1Entry.insert(0,hexTextR1)
gatedClock 4:92539904a4ad 311
gatedClock 4:92539904a4ad 312
gatedClock 4:92539904a4ad 313 # read R2.
gatedClock 4:92539904a4ad 314 self.__iUSB.write(self.buildReadRegisterCommand("2"))
gatedClock 4:92539904a4ad 315 fetchTextR2 = self.__iUSB.read()
gatedClock 4:92539904a4ad 316 hexTextR2 = self.formatRegValPayload(fetchTextR2)
gatedClock 4:92539904a4ad 317 self.__R2Entry.delete(0,99)
gatedClock 4:92539904a4ad 318 self.__R2Entry.insert(0,hexTextR2)
gatedClock 4:92539904a4ad 319
gatedClock 4:92539904a4ad 320 # read R3.
gatedClock 4:92539904a4ad 321 self.__iUSB.write(self.buildReadRegisterCommand("3"))
gatedClock 4:92539904a4ad 322 fetchTextR3 = self.__iUSB.read()
gatedClock 4:92539904a4ad 323 hexTextR3 = self.formatRegValPayload(fetchTextR3)
gatedClock 4:92539904a4ad 324 self.__R3Entry.delete(0,99)
gatedClock 4:92539904a4ad 325 self.__R3Entry.insert(0,hexTextR3)
gatedClock 4:92539904a4ad 326
gatedClock 4:92539904a4ad 327
gatedClock 4:92539904a4ad 328 # read Program Counter.
gatedClock 4:92539904a4ad 329 self.__iUSB.write(self.buildReadRegisterCommand("4"))
gatedClock 4:92539904a4ad 330 fetchTextPC = self.__iUSB.read()
gatedClock 4:92539904a4ad 331 hexTextPC = self.formatRegValPayload(fetchTextPC)
gatedClock 4:92539904a4ad 332 self.__PCEntry.delete(0,99)
gatedClock 4:92539904a4ad 333 self.__PCEntry.insert(0,hexTextPC)
gatedClock 4:92539904a4ad 334
gatedClock 4:92539904a4ad 335 # read Instruction Register high.
gatedClock 4:92539904a4ad 336 self.__iUSB.write(self.buildReadRegisterCommand("5"))
gatedClock 4:92539904a4ad 337 fetchTextIRH = self.__iUSB.read()
gatedClock 4:92539904a4ad 338 hexTextIRH = self.formatRegValPayload(fetchTextIRH)
gatedClock 4:92539904a4ad 339
gatedClock 4:92539904a4ad 340
gatedClock 4:92539904a4ad 341 # read Instruction Register low.
gatedClock 4:92539904a4ad 342 self.__iUSB.write(self.buildReadRegisterCommand("6"))
gatedClock 4:92539904a4ad 343 fetchTextIRL = self.__iUSB.read()
gatedClock 4:92539904a4ad 344 hexTextIRL = self.formatRegValPayload(fetchTextIRL)
gatedClock 4:92539904a4ad 345 hexTextIR = hexTextIRH + hexTextIRL[2:]
gatedClock 4:92539904a4ad 346
gatedClock 4:92539904a4ad 347
gatedClock 4:92539904a4ad 348 self.__IREntry.delete(0,99) # 2-byte IR content to UI.
gatedClock 4:92539904a4ad 349 self.__IREntry.insert(0,hexTextIR)
gatedClock 4:92539904a4ad 350
gatedClock 4:92539904a4ad 351 #----
gatedClock 4:92539904a4ad 352 self.refreshUI() # ensure proper text formats
gatedClock 4:92539904a4ad 353 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 354 def selectmmReadButtonPressed(self): # MM READ button has been pressed.
gatedClock 4:92539904a4ad 355 self.refreshUI() # ensure proper text formats.
gatedClock 4:92539904a4ad 356
gatedClock 4:92539904a4ad 357 transmitmmADRText = self.formatByteStringForTransmit(self.__mmADREntry.get())
gatedClock 4:92539904a4ad 358 commandText = self.buildReadMMCommand(transmitmmADRText)
gatedClock 4:92539904a4ad 359
gatedClock 4:92539904a4ad 360 self.__iUSB.write(commandText)
gatedClock 4:92539904a4ad 361
gatedClock 4:92539904a4ad 362 fetchTextmmVAL = self.__iUSB.read()
gatedClock 4:92539904a4ad 363
gatedClock 4:92539904a4ad 364 buildHextmmVALString = fetchTextmmVAL[3:7]
gatedClock 4:92539904a4ad 365
gatedClock 4:92539904a4ad 366 self.__mmVALEntry.delete(0,99)
gatedClock 4:92539904a4ad 367 self.__mmVALEntry.insert(0,buildHextmmVALString)
gatedClock 4:92539904a4ad 368 #----
gatedClock 4:92539904a4ad 369 self.selectRegReadButtonPressed() # because registers are affected.
gatedClock 4:92539904a4ad 370 self.refreshUI() # ensure proper text formats
gatedClock 4:92539904a4ad 371 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 372 # before writing the register values currently displayed in the UI,
gatedClock 4:92539904a4ad 373 # this method will first run that data through a formatter, so that
gatedClock 4:92539904a4ad 374 # the data is transformed into '0x##' format. this is convenient for
gatedClock 4:92539904a4ad 375 # the user. for example, the user can type '12' in a register entry
gatedClock 4:92539904a4ad 376 # form, press the 'REG WRITE' button, and that data will be reformatted
gatedClock 4:92539904a4ad 377 # to appear as '0x12'.
gatedClock 4:92539904a4ad 378
gatedClock 4:92539904a4ad 379 def selectRegWriteButtonPressed(self):# REG WRITE button has been pressed.
gatedClock 4:92539904a4ad 380
gatedClock 4:92539904a4ad 381 self.refreshUI() # ensure proper text formats.
gatedClock 4:92539904a4ad 382 #----
gatedClock 4:92539904a4ad 383 # place those values in the
gatedClock 4:92539904a4ad 384 # standard format.
gatedClock 4:92539904a4ad 385 R0Val = self.formatByteStringForTransmit(self.__R0Entry.get())
gatedClock 4:92539904a4ad 386 R1Val = self.formatByteStringForTransmit(self.__R1Entry.get())
gatedClock 4:92539904a4ad 387 R2Val = self.formatByteStringForTransmit(self.__R2Entry.get())
gatedClock 4:92539904a4ad 388 R3Val = self.formatByteStringForTransmit(self.__R3Entry.get())
gatedClock 4:92539904a4ad 389 PCVal = self.formatByteStringForTransmit(self.__PCEntry.get())
gatedClock 4:92539904a4ad 390 IRVal = self.formatWordStringForTransmit(self.__IREntry.get())
gatedClock 4:92539904a4ad 391 #----
gatedClock 4:92539904a4ad 392 # write those values to the CPU.
gatedClock 4:92539904a4ad 393 self.__iUSB.write(self.buildWriteRegisterCommand("0",R0Val))
gatedClock 4:92539904a4ad 394 self.__iUSB.write(self.buildWriteRegisterCommand("1",R1Val))
gatedClock 4:92539904a4ad 395 self.__iUSB.write(self.buildWriteRegisterCommand("2",R2Val))
gatedClock 4:92539904a4ad 396 self.__iUSB.write(self.buildWriteRegisterCommand("3",R3Val))
gatedClock 4:92539904a4ad 397 self.__iUSB.write(self.buildWriteRegisterCommand("4",PCVal))
gatedClock 4:92539904a4ad 398 self.__iUSB.write(self.buildWriteIRCommand ( IRVal))
gatedClock 4:92539904a4ad 399 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 400 # before writing the main-memory values currently displayed in the UI,
gatedClock 4:92539904a4ad 401 # this method will first run that data through a formatter, so that
gatedClock 4:92539904a4ad 402 # the data is transformed into '0x##' or '0x####' format.
gatedClock 4:92539904a4ad 403 # this is convenient for the user.
gatedClock 4:92539904a4ad 404 # for example, the user can type '12' in the mmADR entry
gatedClock 4:92539904a4ad 405 # form, press the 'MM WRITE' button, and that data will be reformatted
gatedClock 4:92539904a4ad 406 # to appear as '0x12'.
gatedClock 4:92539904a4ad 407
gatedClock 4:92539904a4ad 408 def selectmmWriteButtonPressed(self): # mm WRITE button has been pressed.
gatedClock 4:92539904a4ad 409
gatedClock 4:92539904a4ad 410 self.refreshUI() # ensure proper text formats.
gatedClock 4:92539904a4ad 411
gatedClock 4:92539904a4ad 412 # obtain mm values currently
gatedClock 4:92539904a4ad 413 # displayed in the UI.
gatedClock 4:92539904a4ad 414 self.__transmitmmADRText = self.__mmADREntry.get()
gatedClock 4:92539904a4ad 415 self.__transmitmmVALText = self.__mmVALEntry.get()
gatedClock 4:92539904a4ad 416
gatedClock 4:92539904a4ad 417 # place those values in the
gatedClock 4:92539904a4ad 418 # standard format.
gatedClock 4:92539904a4ad 419 mmADRVAL = self.formatByteStringForTransmit(self.__mmADREntry.get())
gatedClock 4:92539904a4ad 420 mmVALVal = self.formatWordStringForTransmit(self.__mmVALEntry.get())
gatedClock 4:92539904a4ad 421
gatedClock 4:92539904a4ad 422 # write those values to the CPU.
gatedClock 4:92539904a4ad 423 self.__iUSB.write(self.buildWriteMMCommand(mmADRVAL,mmVALVal))
gatedClock 4:92539904a4ad 424
gatedClock 4:92539904a4ad 425 self.selectRegReadButtonPressed() # because registers are affected.
gatedClock 4:92539904a4ad 426 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 427 def selectProgButtonPressed(self): # Program button has been pressed.
gatedClock 4:92539904a4ad 428
gatedClock 4:92539904a4ad 429 # ask user for program source file
gatedClock 4:92539904a4ad 430 # name using a dialog.
gatedClock 4:92539904a4ad 431 fileName = tkFileDialog.askopenfilename(defaultextension='.txt')
gatedClock 4:92539904a4ad 432
gatedClock 4:92539904a4ad 433 print("memory load started.") # announce to user.
gatedClock 4:92539904a4ad 434 self.progamMainMemory(fileName) # main part of programming.
gatedClock 4:92539904a4ad 435 print("memory load completed.") # announce to user.
gatedClock 4:92539904a4ad 436 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 437 def selectmmDumpButtonPressed(self): # MM dump button has been pressed.
gatedClock 4:92539904a4ad 438
gatedClock 4:92539904a4ad 439 # obtain file-to-write-to name
gatedClock 4:92539904a4ad 440 # from user, using a dialog.
gatedClock 4:92539904a4ad 441 fileName = tkFileDialog.asksaveasfilename(defaultextension='.txt')
gatedClock 4:92539904a4ad 442
gatedClock 4:92539904a4ad 443 print("memory dump started.") # announce to user.
gatedClock 4:92539904a4ad 444 self.dumpMainMemory(fileName) # dump main memory to file.
gatedClock 4:92539904a4ad 445 print("memory dump completed.") # announce to user.
gatedClock 4:92539904a4ad 446 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 447 def selectStepButtonPressed(self): # STEP button has been pressed.
gatedClock 4:92539904a4ad 448 self.__iUSB.write("50000000") # the command code.
gatedClock 4:92539904a4ad 449 self.selectRegReadButtonPressed() # monitor the result.
gatedClock 4:92539904a4ad 450 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 451 def selectRunButtonPressed(self): # RUN/STOP button has been pressed.
gatedClock 4:92539904a4ad 452
gatedClock 4:92539904a4ad 453 if (self.__isRunning == 0): # start high speed run.
gatedClock 4:92539904a4ad 454 self.__isRunning = 1
gatedClock 4:92539904a4ad 455 self.__runButton["text"] = "SLOW"
gatedClock 4:92539904a4ad 456 self.t = threading.Thread(target=self.doRun)
gatedClock 4:92539904a4ad 457 self.t.start()
gatedClock 4:92539904a4ad 458
gatedClock 4:92539904a4ad 459 elif (self.__isRunning == 1): # switch to a slower run.
gatedClock 4:92539904a4ad 460 self.__isRunning = 2
gatedClock 4:92539904a4ad 461 self.__runButton["text"] = "STOP"
gatedClock 4:92539904a4ad 462
gatedClock 4:92539904a4ad 463 else: # stop the CPU.
gatedClock 4:92539904a4ad 464 self.__isRunning = 0
gatedClock 4:92539904a4ad 465 self.__runButton["text"] = "RUN"
gatedClock 4:92539904a4ad 466 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 467 def selectTestButtonPressed(self): # TEST/STOP button has been pressed.
gatedClock 4:92539904a4ad 468
gatedClock 4:92539904a4ad 469 if (self.__testRunning == 0): # start test.
gatedClock 4:92539904a4ad 470 self.__testRunning = 1
gatedClock 4:92539904a4ad 471 self.__testButton["text"] = "STOP TEST"
gatedClock 4:92539904a4ad 472 self.t = threading.Thread(target=self.doTest)
gatedClock 4:92539904a4ad 473 self.t.start()
gatedClock 4:92539904a4ad 474
gatedClock 4:92539904a4ad 475
gatedClock 4:92539904a4ad 476 else: # stop test.
gatedClock 4:92539904a4ad 477 self.__testRunning = 0
gatedClock 4:92539904a4ad 478 self.__testButton["text"] = "TEST"
gatedClock 4:92539904a4ad 479 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 480 # BEGIN UTILITY CODE. #===============================
gatedClock 4:92539904a4ad 481 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 482 # guarantee 0x## format.
gatedClock 4:92539904a4ad 483 def formatByteStringForDisplay(self, byteText):
gatedClock 4:92539904a4ad 484
gatedClock 4:92539904a4ad 485 xIndex = byteText.find("x") # eliminate any 0x, 0X prefix.
gatedClock 4:92539904a4ad 486 XIndex = byteText.find("X")
gatedClock 4:92539904a4ad 487
gatedClock 4:92539904a4ad 488 theXindex = 0
gatedClock 4:92539904a4ad 489 if (xIndex >= 0): theXindex = xIndex + 1
gatedClock 4:92539904a4ad 490 if (XIndex >= 0): theXindex = XIndex + 1
gatedClock 4:92539904a4ad 491
gatedClock 4:92539904a4ad 492 buildText = byteText[theXindex:] # any 0x, 0X prefix gone.
gatedClock 4:92539904a4ad 493
gatedClock 4:92539904a4ad 494 # build-out leading 0's.
gatedClock 4:92539904a4ad 495 if (len(buildText) == 0): buildText = "0" + buildText
gatedClock 4:92539904a4ad 496 if (len(buildText) == 1): buildText = "0" + buildText
gatedClock 4:92539904a4ad 497
gatedClock 4:92539904a4ad 498 # truncate leading characters.
gatedClock 4:92539904a4ad 499 if (len(buildText) > 2): buildText = buildText[-2:]
gatedClock 4:92539904a4ad 500
gatedClock 4:92539904a4ad 501 buildText = "0x" + buildText # add leading 0x.
gatedClock 4:92539904a4ad 502
gatedClock 4:92539904a4ad 503 return(buildText) # 0x## format returned.
gatedClock 4:92539904a4ad 504 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 505 # guarantee 0x#### format.
gatedClock 4:92539904a4ad 506 def formatWordStringForDisplay(self, wordText):
gatedClock 4:92539904a4ad 507
gatedClock 4:92539904a4ad 508 xIndex = wordText.find("x") # eliminate any 0x, 0X prefix.
gatedClock 4:92539904a4ad 509 XIndex = wordText.find("X")
gatedClock 4:92539904a4ad 510
gatedClock 4:92539904a4ad 511 theXindex = 0
gatedClock 4:92539904a4ad 512 if (xIndex >= 0): theXindex = xIndex + 1
gatedClock 4:92539904a4ad 513 if (XIndex >= 0): theXindex = XIndex + 1
gatedClock 4:92539904a4ad 514
gatedClock 4:92539904a4ad 515 buildText = wordText[theXindex:] # any 0x, 0X prefix gone.
gatedClock 4:92539904a4ad 516
gatedClock 4:92539904a4ad 517 # build-out leading 0's.
gatedClock 4:92539904a4ad 518 if (len(buildText) == 0): buildText = "0" + buildText
gatedClock 4:92539904a4ad 519 if (len(buildText) == 1): buildText = "0" + buildText
gatedClock 4:92539904a4ad 520 if (len(buildText) == 2): buildText = "0" + buildText
gatedClock 4:92539904a4ad 521 if (len(buildText) == 3): buildText = "0" + buildText
gatedClock 4:92539904a4ad 522
gatedClock 4:92539904a4ad 523
gatedClock 4:92539904a4ad 524 # truncate leading characters.
gatedClock 4:92539904a4ad 525 if (len(buildText) > 4): buildText = buildText[-4:]
gatedClock 4:92539904a4ad 526
gatedClock 4:92539904a4ad 527 buildText = "0x" + buildText # add leading 0x.
gatedClock 4:92539904a4ad 528
gatedClock 4:92539904a4ad 529 return(buildText) # 0x## format returned.
gatedClock 4:92539904a4ad 530 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 531 # guarantee ## format.
gatedClock 4:92539904a4ad 532 def formatByteStringForTransmit(self, byteText):
gatedClock 4:92539904a4ad 533 buildText = self.formatByteStringForDisplay(byteText)
gatedClock 4:92539904a4ad 534 buildText = buildText[-2:]
gatedClock 4:92539904a4ad 535 return(buildText)
gatedClock 4:92539904a4ad 536 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 537 # guarantee #### format.
gatedClock 4:92539904a4ad 538 def formatWordStringForTransmit(self, wordText):
gatedClock 4:92539904a4ad 539 buildText = self.formatWordStringForDisplay(wordText)
gatedClock 4:92539904a4ad 540 buildText = buildText[-4:]
gatedClock 4:92539904a4ad 541 return(buildText)
gatedClock 4:92539904a4ad 542 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 543 def refreshUI(self): # reformat UI text.
gatedClock 4:92539904a4ad 544
gatedClock 4:92539904a4ad 545 # copy-in current form contents.
gatedClock 4:92539904a4ad 546 originalR0Entry = self.__R0Entry.get()
gatedClock 4:92539904a4ad 547 originalR1Entry = self.__R1Entry.get()
gatedClock 4:92539904a4ad 548 originalR2Entry = self.__R2Entry.get()
gatedClock 4:92539904a4ad 549 originalR3Entry = self.__R3Entry.get()
gatedClock 4:92539904a4ad 550 originalPCEntry = self.__PCEntry.get()
gatedClock 4:92539904a4ad 551 originalIREntry = self.__IREntry.get()
gatedClock 4:92539904a4ad 552 originalmmADREntry = self.__mmADREntry.get()
gatedClock 4:92539904a4ad 553 originalmmVALEntry = self.__mmVALEntry.get()
gatedClock 4:92539904a4ad 554
gatedClock 4:92539904a4ad 555 # reformat form contents.
gatedClock 4:92539904a4ad 556 newR0Entry = self.formatByteStringForDisplay(originalR0Entry)
gatedClock 4:92539904a4ad 557 newR1Entry = self.formatByteStringForDisplay(originalR1Entry)
gatedClock 4:92539904a4ad 558 newR2Entry = self.formatByteStringForDisplay(originalR2Entry)
gatedClock 4:92539904a4ad 559 newR3Entry = self.formatByteStringForDisplay(originalR3Entry)
gatedClock 4:92539904a4ad 560 newPCEntry = self.formatByteStringForDisplay(originalPCEntry)
gatedClock 4:92539904a4ad 561 newIREntry = self.formatWordStringForDisplay(originalIREntry)
gatedClock 4:92539904a4ad 562
gatedClock 4:92539904a4ad 563 newmmADREntry = self.formatByteStringForDisplay(originalmmADREntry)
gatedClock 4:92539904a4ad 564 newmmVALEntry = self.formatWordStringForDisplay(originalmmVALEntry)
gatedClock 4:92539904a4ad 565
gatedClock 4:92539904a4ad 566
gatedClock 4:92539904a4ad 567 self.__R0Entry. delete(0,99) # clear form contents.
gatedClock 4:92539904a4ad 568 self.__R1Entry. delete(0,99)
gatedClock 4:92539904a4ad 569 self.__R2Entry. delete(0,99)
gatedClock 4:92539904a4ad 570 self.__R3Entry. delete(0,99)
gatedClock 4:92539904a4ad 571 self.__PCEntry. delete(0,99)
gatedClock 4:92539904a4ad 572 self.__IREntry. delete(0,99)
gatedClock 4:92539904a4ad 573 self.__mmADREntry.delete(0,99)
gatedClock 4:92539904a4ad 574 self.__mmVALEntry.delete(0,99)
gatedClock 4:92539904a4ad 575
gatedClock 4:92539904a4ad 576 # update form contents.
gatedClock 4:92539904a4ad 577 self.__R0Entry. insert(0,newR0Entry)
gatedClock 4:92539904a4ad 578 self.__R1Entry. insert(0,newR1Entry)
gatedClock 4:92539904a4ad 579 self.__R2Entry. insert(0,newR2Entry)
gatedClock 4:92539904a4ad 580 self.__R3Entry. insert(0,newR3Entry)
gatedClock 4:92539904a4ad 581 self.__PCEntry. insert(0,newPCEntry)
gatedClock 4:92539904a4ad 582 self.__IREntry. insert(0,newIREntry)
gatedClock 4:92539904a4ad 583 self.__mmADREntry.insert(0,newmmADREntry)
gatedClock 4:92539904a4ad 584 self.__mmVALEntry.insert(0,newmmVALEntry)
gatedClock 4:92539904a4ad 585 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 586 # build the command string for
gatedClock 4:92539904a4ad 587 # reading a value from a CPU register.
gatedClock 4:92539904a4ad 588 def buildReadRegisterCommand(self,regNum):
gatedClock 4:92539904a4ad 589
gatedClock 4:92539904a4ad 590 commandText = "" # build command for read register.
gatedClock 4:92539904a4ad 591 commandText = commandText + "2" # command-code.
gatedClock 4:92539904a4ad 592 commandText = commandText + regNum # register number.
gatedClock 4:92539904a4ad 593 commandText = commandText + "0000000000000"
gatedClock 4:92539904a4ad 594 return(commandText)
gatedClock 4:92539904a4ad 595 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 596 # build the command-string for
gatedClock 4:92539904a4ad 597 # writing a value to a CPU register.
gatedClock 4:92539904a4ad 598 def buildWriteRegisterCommand(self,regNum,regVal):
gatedClock 4:92539904a4ad 599
gatedClock 4:92539904a4ad 600 commandText = ""
gatedClock 4:92539904a4ad 601 commandText = commandText + "1" # command-code.
gatedClock 4:92539904a4ad 602 commandText = commandText + regNum # register.
gatedClock 4:92539904a4ad 603 commandText = commandText + regVal # content.
gatedClock 4:92539904a4ad 604 commandText = commandText + "00000000000"
gatedClock 4:92539904a4ad 605 return(commandText)
gatedClock 4:92539904a4ad 606 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 607 # build the command-string for
gatedClock 4:92539904a4ad 608 # writing a value to the IR.
gatedClock 4:92539904a4ad 609 def buildWriteIRCommand(self,regVal):
gatedClock 4:92539904a4ad 610
gatedClock 4:92539904a4ad 611 commandText = ""
gatedClock 4:92539904a4ad 612 commandText = commandText + "6" # command-code.
gatedClock 4:92539904a4ad 613 commandText = commandText + "5" # actually a placeholder.
gatedClock 4:92539904a4ad 614 commandText = commandText + regVal # content.
gatedClock 4:92539904a4ad 615 commandText = commandText + "000000000"
gatedClock 4:92539904a4ad 616 return(commandText)
gatedClock 4:92539904a4ad 617 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 618 # build the command-string for
gatedClock 4:92539904a4ad 619 # writing a main-memory value.
gatedClock 4:92539904a4ad 620 def buildWriteMMCommand(self,mmADR,mmVAL):
gatedClock 4:92539904a4ad 621
gatedClock 4:92539904a4ad 622 commandText = ""
gatedClock 4:92539904a4ad 623 commandText = commandText + "3" # command-code.
gatedClock 4:92539904a4ad 624 commandText = commandText + mmADR # address.
gatedClock 4:92539904a4ad 625 commandText = commandText + mmVAL # value.
gatedClock 4:92539904a4ad 626 commandText = commandText + "00000000"
gatedClock 4:92539904a4ad 627 return(commandText)
gatedClock 4:92539904a4ad 628 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 629 def buildReadMMCommand(self,mmADR): # build read-mm command-string.
gatedClock 4:92539904a4ad 630 commandText = "" # build command for read main-memory.
gatedClock 4:92539904a4ad 631 commandText = commandText + "4" # command-code.
gatedClock 4:92539904a4ad 632 commandText = commandText + mmADR
gatedClock 4:92539904a4ad 633 commandText = commandText + "000000000000"
gatedClock 4:92539904a4ad 634 return(commandText)
gatedClock 4:92539904a4ad 635 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 636 # extract reg value payload.
gatedClock 4:92539904a4ad 637 def formatRegValPayload(self,messageVector):
gatedClock 4:92539904a4ad 638 buildHex = "0x" + messageVector[2:4]
gatedClock 4:92539904a4ad 639 return(buildHex)
gatedClock 4:92539904a4ad 640 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 641 def progamMainMemory(self,progFile): # core of loading program to MM.
gatedClock 4:92539904a4ad 642
gatedClock 4:92539904a4ad 643 hFile = open(progFile, 'r')
gatedClock 4:92539904a4ad 644 fileLineList = hFile.readlines() # read file into a list.
gatedClock 4:92539904a4ad 645 hFile.close() # close the file.
gatedClock 4:92539904a4ad 646
gatedClock 4:92539904a4ad 647 # load '0x12 0x3456' memory content
gatedClock 4:92539904a4ad 648 # format, into a list.
gatedClock 4:92539904a4ad 649 for listIndex in range(0, len(fileLineList)):
gatedClock 4:92539904a4ad 650 addressStartIndex = fileLineList[listIndex].find("0x") + 2
gatedClock 4:92539904a4ad 651 dataStartIndex = fileLineList[listIndex].find("0x", addressStartIndex) + 2
gatedClock 4:92539904a4ad 652 progAddress = fileLineList[listIndex][addressStartIndex : addressStartIndex + 2]
gatedClock 4:92539904a4ad 653 progAddress = format(listIndex,'02x')
gatedClock 4:92539904a4ad 654 progData = fileLineList[listIndex][dataStartIndex : dataStartIndex + 4]
gatedClock 4:92539904a4ad 655 commandText = self.buildWriteMMCommand(progAddress,progData)
gatedClock 4:92539904a4ad 656 self.__iUSB.write(commandText)
gatedClock 4:92539904a4ad 657 print(fileLineList[listIndex][0:len(fileLineList[listIndex])-1])
gatedClock 4:92539904a4ad 658 sleep(self.__sleepVal)
gatedClock 4:92539904a4ad 659 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 660 def dumpMainMemory(self,dumpFile): # core of dumping MM to file.
gatedClock 4:92539904a4ad 661
gatedClock 4:92539904a4ad 662 hFile = open(dumpFile, 'w')
gatedClock 4:92539904a4ad 663
gatedClock 4:92539904a4ad 664 for loopIndex in range(0, 256): # traverse all of main-memory.
gatedClock 4:92539904a4ad 665 transmitmmADRText = "{}".format(hex(loopIndex))
gatedClock 4:92539904a4ad 666 transmitmmADRText = transmitmmADRText[2:]
gatedClock 4:92539904a4ad 667 if (len(transmitmmADRText) == 1): transmitmmADRText = "0" + transmitmmADRText
gatedClock 4:92539904a4ad 668 commandText = self.buildReadMMCommand(transmitmmADRText)
gatedClock 4:92539904a4ad 669 self.__iUSB.write(commandText)
gatedClock 4:92539904a4ad 670 fetchTextmmVAL = self.__iUSB.read()
gatedClock 4:92539904a4ad 671 buildHextmmVALString = fetchTextmmVAL[3:7]
gatedClock 4:92539904a4ad 672 buildHexIndex = hex(loopIndex)
gatedClock 4:92539904a4ad 673 buildHexIndex = buildHexIndex[2:]
gatedClock 4:92539904a4ad 674 if (len(buildHexIndex) == 1): buildHexIndex = "0" + buildHexIndex
gatedClock 4:92539904a4ad 675 buildHexIndex = "0x" + buildHexIndex
gatedClock 4:92539904a4ad 676 dumpString = " {} 0x{}\n".format(buildHexIndex,buildHextmmVALString)
gatedClock 4:92539904a4ad 677 hFile.write(dumpString)
gatedClock 4:92539904a4ad 678 print(dumpString[0:len(dumpString)-1])
gatedClock 4:92539904a4ad 679 sleep(self.__sleepVal)
gatedClock 4:92539904a4ad 680
gatedClock 4:92539904a4ad 681 hFile.close() # close the dump file.
gatedClock 4:92539904a4ad 682 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 683 # BEGIN THREADED CODE. #===============================
gatedClock 4:92539904a4ad 684 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 685 def doRun(self): # drives the CPU running.
gatedClock 4:92539904a4ad 686 while(self.__isRunning):
gatedClock 4:92539904a4ad 687 self.selectStepButtonPressed()
gatedClock 4:92539904a4ad 688 self.__masterWidget.update_idletasks()
gatedClock 4:92539904a4ad 689 if (self.__isRunning == 2): sleep(1.0)
gatedClock 4:92539904a4ad 690 #-----------------------------------------------#-------------------------------
gatedClock 4:92539904a4ad 691 # the test consists of loading the add-list program into main memory,
gatedClock 4:92539904a4ad 692 # running it, dumping the result to a file, and comparing that file
gatedClock 4:92539904a4ad 693 # to the expected result.
gatedClock 4:92539904a4ad 694
gatedClock 4:92539904a4ad 695 def doTest(self): # drives the test.
gatedClock 4:92539904a4ad 696 while(self.__testRunning):
gatedClock 4:92539904a4ad 697 # load program into main memory.
gatedClock 4:92539904a4ad 698 self.progamMainMemory("prog_add_list.txt")
gatedClock 4:92539904a4ad 699
gatedClock 4:92539904a4ad 700 # clear instruction register.
gatedClock 4:92539904a4ad 701 self.__iUSB.write(self.buildWriteIRCommand("0000"))
gatedClock 4:92539904a4ad 702
gatedClock 4:92539904a4ad 703 # rewind program counter.
gatedClock 4:92539904a4ad 704 PCVal = self.formatByteStringForTransmit("0")
gatedClock 4:92539904a4ad 705 writePCcommand = self.buildWriteRegisterCommand("4",PCVal)
gatedClock 4:92539904a4ad 706 self.__iUSB.write(writePCcommand)
gatedClock 4:92539904a4ad 707 self.selectRegReadButtonPressed()
gatedClock 4:92539904a4ad 708
gatedClock 4:92539904a4ad 709
gatedClock 4:92539904a4ad 710 for stepIndex in range(0, 196): # step through program.
gatedClock 4:92539904a4ad 711 self.selectStepButtonPressed()
gatedClock 4:92539904a4ad 712
gatedClock 4:92539904a4ad 713 # dump result to file.
gatedClock 4:92539904a4ad 714 self.dumpMainMemory ("dump_test.txt")
gatedClock 4:92539904a4ad 715
gatedClock 4:92539904a4ad 716 # build and run test commands.
gatedClock 4:92539904a4ad 717 self.__testIter = self.__testIter + 1
gatedClock 4:92539904a4ad 718 iterationText = "test iteration {}".format(self.__testIter)
gatedClock 4:92539904a4ad 719 iterationText = "echo '" + iterationText + "' >> testlog.txt"
gatedClock 4:92539904a4ad 720 os.system("echo ============================ >> testlog.txt")
gatedClock 4:92539904a4ad 721 os.system("date >> testlog.txt")
gatedClock 4:92539904a4ad 722 os.system(iterationText)
gatedClock 4:92539904a4ad 723 os.system("echo 'compare dump_test.txt against dump_add_list.txt' >> testlog.txt")
gatedClock 4:92539904a4ad 724 os.system("diff dump_add_list.txt dump_test.txt >> testlog.txt")
gatedClock 4:92539904a4ad 725
gatedClock 4:92539904a4ad 726 self.__masterWidget.update_idletasks()
gatedClock 4:92539904a4ad 727 #===============================================#===============================
gatedClock 4:92539904a4ad 728
gatedClock 4:92539904a4ad 729
gatedClock 4:92539904a4ad 730
gatedClock 4:92539904a4ad 731
gatedClock 4:92539904a4ad 732
gatedClock 4:92539904a4ad 733
gatedClock 4:92539904a4ad 734
gatedClock 4:92539904a4ad 735
gatedClock 4:92539904a4ad 736
gatedClock 4:92539904a4ad 737
gatedClock 4:92539904a4ad 738
gatedClock 4:92539904a4ad 739
gatedClock 4:92539904a4ad 740