Laser Lock - Memoy Data Controller.SXB '------------------------------------------------------------------------------------- 'Overview: '------------------------------------------------------------------------------------- 'The Memory Data Controller is responsible for the data bus and main read/write ' control of the M48Z02 NVRAM chip. This controller also sends data to the main ' controller when requested. This controller watches the "ready" status line from the ' Memory Address Controller, to ensure the address value is properly set on the ' address bus before performing any read/write operation. Note that the limited ' number of IO pins is the only reason the memory controller functionality is split ' into two Sx28 chips. This could be replaced with a single SX48 TQFP ' microcontroller. '------------------------------------------------------------------------------------- 'Notes '------------------------------------------------------------------------------------- 'Inputs/Outputs: '-------------- ' Serial IO with Laser Lock main controller ' Data bus IO with M48Z02 NVRAM '------------------------------------------------------------------------------------- 'Speed selection and device initialization '------------------------------------------------------------------------------------- DEVICE SX28, OSC4MHZ, TURBO, STACKX, OPTIONX '-using an SX28 device, with internal clock of 4 MHz '-TURBO refers to the operation mode, where one clock cycle per 'instruction. '-STACKX, OPTIONX required for SX28 only (apparently - P32 of Practical SXBasic ' programming guide) 'when programming SX48, TURBO, STACKX, and OPTIONX are assumed 'this is used for timing-specific parts of the code, like PAUSE 'this is also used by the SX-KEY, as it generates a clock for debugging 'purposes FREQ 4_000_000 'NOTE: the underscore can be used to separate number values to improve readability ' eg binary valu %0100_0010 versus %01000010 '------------------------------------------------------------------------------------- 'Pin Assignment '------------------------------------------------------------------------------------- DataBus PIN RB Input 'the 8-bit data bus to the M48Z02 WE PIN RC.0 Output 'M48Z02 write enable (active low) CE PIN RC.1 Output 'M48Z02 chip enable (active low) OE PIN RC.2 Output 'M48Z02 output enable (active low) AddrReady Pin RC.3 Input 'Memory Address controller ready ' signal (active high) AddrAck Pin RC.4 Input 'Memory Address controller attention ' acknowledge (active low) 'chip select- flagged low when the main controller is attempting to communicate CS PIN RA.2 Input 'master in, slave out - default input to allow bussed use of this line Miso PIN RA.1 Input Mosi PIN RA.0 Input 'Master out, slave in - SPI input for this device 'attention acknowledge - default input to allow bussed use of this line by other ' controllers AttnAck PIN RA.3 Input Debug Pin RC.7 Output '------------------------------------------------------------------------------------- 'Variable Declaration '------------------------------------------------------------------------------------- 'TODO: is this needed? datavalue var byte 'data value to be written to, or read from, the M48Z02 'running indices for loops i var byte j var byte 'packet data packet var byte(10) 'scratch pad variables for calculations scratch var byte(3) '------------------------------------------------------------------------------------- 'Subroutine Definitions '------------------------------------------------------------------------------------- ASyncPacketRead sub 0 'slave asynchronous serial packet read ASyncPacketWrite sub 0 'slave asynchronous serial packet write '------------------------------------------------------------------------------------- 'Main Program '------------------------------------------------------------------------------------- 'The PROGRAM directive specifies the label where the program begins. 'In this case, "Start" is the label specified. This also specifies 'where to put the internally generated SX initialization code. 'NOTE: to manually remove the initialization code, look into 'the NOSTARTUP option for the PROGRAM directive PROGRAM Start 'the Start label Start: 'TODO: POST the NVRAM? 'set all outputs appropriately high WE high CE high OE 'wait for main controller to POST pause 500 'startup config: set the position to an intermediate spot 'main loop: '---------- MainLoop: 'wait for next instruction from main controller ASyncPacketRead 'respond to packet data if packet(1) = $01 then 'command $01: read data from specified address 'wait for address controller to signal that it is ready do while AddrReady = 0 loop 'prepare for M48Z02 read Input DataBus low CE 'activate chip enable (/E) low OE 'acivate output enable (/G) pauseus 1 datavalue = DataBus high OE 'disable output enable high CE 'disable chip enable 'now transfer info to a packet and get ready to write it packet(0) = 2 packet(1) = datavalue ASyncPacketWrite elseif packet(1) = $02 then 'command $02: write data to specified address do while AddrReady = 0 loop 'prepare for M48Z02 write DataBus = packet(4) Output DataBus low CE 'activate chip enable (/E) low WE 'activate write enable (/W) 'TODO: is this needed? pauseus 1 high WE 'disable write enable high CE 'disable chip enable Input DataBus endif GOTO MainLoop '------------------------------------------------------------------------------------- 'Subroutines '------------------------------------------------------------------------------------- 'this performs asynchronous packet read operation sub ASyncPacketRead 'wait for CS to go low do while CS = 1 loop 'wait for AddrAck to go low, ensuring the memory address controller is ready as well do while AddrAck = 1 loop 'take control of attention acknowledge line and set it low AttnAck = 0 output AttnAck 'read packet size header byte serin Mosi, N57600, packet(0) 'read the rest of the packet for i = 1 to packet(0) if i < packet(0) then serin Mosi, N57600, packet(i) endif next 'wait for CS to go high again do while CS = 0 loop 'release attention acknowledge line (pullup resistor will set it high) input AttnAck endsub 'this performs asynchronous packet write operation sub ASyncPacketWrite Miso = 0 output Miso AttnAck = 0 output AttnAck do while CS = 1 loop pauseus 5 for i = 0 to packet(0) if i < packet(0) then serout Miso, N57600, packet(i) pauseus 5 endif next 'release the Miso line so other devices can use it input Miso 'reset attention acknowledge to an input, which is tied to high, indicating packet ' transfer complete input AttnAck 'wait for main controller to acknowledge packet transfer complete do while CS = 0 loop endsub