forked from parallaxinc/spin-standard-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcom.parallel-8bit.spin
297 lines (254 loc) · 10.5 KB
/
com.parallel-8bit.spin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
{
---------------------------------------------------------------------------------------------------
Filename: com.parallel-8bit.spin
Description: 8-bit parallel I/O engine for LCDs
Author: Jesse Burt
Started: Oct 13, 2021
Updated: Feb 13, 2024
Copyright (c) 2024 - See end of file for terms of use.
---------------------------------------------------------------------------------------------------
}
CON
' 8-bit Parallel engine commands
IDLE = 0
CMD = 1
DATA = 2
BLKDAT = 3
BLKWD_MSBF = 4
BYTEX = 5
WORDX = 6
VAR
long _cog
long _io_cmd, _ptr_buff, _xfer_cnt
long _DATA, _CS, _DC, _WR, _RD
PUB null{}
' This is not a top-level object
PUB init(D_BASEPIN, CS_PIN, DC_PIN, WR_PIN, RD_PIN=-1): status
' Initialize engine
' D_BASEPIN: first of 8 I/O pin block
' CS_PIN: CS / CSX (chip select)
' DC_PIN: DC / DCX / RS (data/command or register select)
' WR_PIN: WR / WRX (write clock)
' RD_PIN: RD / RDX (read clock; not currently used - keep this pin high)
deinit{} ' stop engine, if started
_DATA := D_BASEPIN ' 8 pins starting at D_BASEPIN
_CS := |< CS_PIN ' create masks for control pins
_DC := |< DC_PIN
_WR := |< WR_PIN
_cog := cognew(@entry, @_io_cmd)+1 ' start engine
return _cog
PUB deinit{}
' Deinitialize engine
' Stop running cog and clear variables
if (_cog)
cogstop(_cog-1)
longfill(@_cog, 0, 8)
PUB wrbyte_cmd(c)
' Write command (8-bits)
_xfer_cnt := 1 ' set data size
_ptr_buff := c ' command byte
_io_cmd := CMD ' signal command to engine
repeat until (_io_cmd == IDLE) ' wait for engine to finish
PUB wrbyte_dat(d)
' Write data (8-bits)
_xfer_cnt := 1
_ptr_buff := d
_io_cmd := DATA
repeat until (_io_cmd == IDLE)
PUB wrblock_dat(ptr_buff, nr_bytes)
' Write block of data
' ptr_buff: pointer to buffer of data
' nr_bytes: number of bytes to write to display
longmove(@_ptr_buff, @ptr_buff, 2) ' copy params to PASM engine
_io_cmd := BLKDAT ' params
repeat until (_io_cmd == IDLE)
PUB wrblkword_msbf(ptr_buff, nr_words)
' Write block of words (MSByte-first)
' ptr_buff: pointer to buffer of data
' nr_words: number of words to write to display
longmove(@_ptr_buff, @ptr_buff, 2)
_io_cmd := BLKWD_MSBF
repeat until (_io_cmd == IDLE)
PUB wrwordx_dat(dw, nr_words)
' Repeatedly write word dw, nr_words times
longmove(@_ptr_buff, @dw, 2)
_io_cmd := WORDX
repeat until (_io_cmd == IDLE)
DAT
entry
org 0
initio
' Initialize:
' * set pointers to calling spin code
' * set I/O pin directions and states
mov iolink, par
mov ptrbuff, iolink ' get spin buffer ptr
add ptrbuff, #4
mov ptr_xcnt, iolink ' get ptr to spin nr_bytes
add ptr_xcnt, #8
mov tmp0, iolink
add tmp0, #12
rdlong DBASE, tmp0 ' get data basepin and
mov DATMASK, #$FF ' create mask
shl DATMASK, DBASE
add tmp0, #4
rdlong CS, tmp0
add tmp0, #4
rdlong DC, tmp0
add tmp0, #4
rdlong WRC, tmp0
or outa, CS ' CS high
or dira, CS
andn outa, WRC ' WRC low
or dira, WRC
or dira, DATMASK ' D7..0 output
or dira, DC ' DC output
andn outa, CS ' CS low
cmdloop
' Wait for command
rdlong tmp0, par wz
if_z jmp #cmdloop
cmp tmp0, #CMD wz
if_e jmp #wr_cmd
cmp tmp0, #DATA wz
if_e jmp #wr_dat
cmp tmp0, #BLKDAT wz
if_e jmp #wrblk_data
cmp tmp0, #BLKWD_MSBF wz
if_e jmp #wrblkwd_msbf
cmp tmp0, #BYTEX wz
if_e jmp #wrwordx_data
cmp tmp0, #WORDX wz
if_e jmp #wrwordx_data
cmdexit
' End of command, or no/invalid command
wrlong clrcmd, iolink ' signal command is clear
jmp #cmdloop
wr_cmd
' Write command
andn outa, DC ' DC low: command
rdbyte cmdbyte, ptrbuff ' get cmd from hub
shl cmdbyte, DBASE
andn outa, DATMASK ' D7..0 low
or outa, cmdbyte ' write cmd to D7..D0
or outa, WRC ' clock out byte
andn outa, WRC
jmp #cmdexit ' return
wr_dat
' Write byte of data
or outa, DC ' DC high: data
rdbyte dbyte, ptrbuff ' get data from hub
shl dbyte, DBASE
andn outa, DATMASK ' D7..0 low
or outa, dbyte ' write data to D7..D0
or outa, WRC ' clock out byte
andn outa, WRC
jmp #cmdexit ' return
wrblk_data
' Write block of bytes
or outa, DC ' DC high: data
rdlong ptr_data, ptrbuff ' get pointer to data
rdlong xcnt, ptr_xcnt ' and number of bytes
:dbyteloop
rdbyte dbyte, ptr_data ' get next byte of data
shl dbyte, DBASE
andn outa, DATMASK ' D7..0 low
or outa, dbyte ' write byte to D7..0
or outa, WRC ' clock out byte
andn outa, WRC
add ptr_data, #1 ' advance ptr to next byte
djnz xcnt, #:dbyteloop ' loop if more bytes
jmp #cmdexit ' return
wrblkwd_msbf
' Write block of words, MSByte-first
or outa, DC ' DC high: data
rdlong ptr_data, ptrbuff ' get pointer to data
rdlong xcnt, ptr_xcnt ' and number of words
:dwordloop
rdword dword, ptr_data ' get next word of data
mov byte1, dword
shr byte1, #8
shl byte1, DBASE
mov byte0, dword
and byte0, #$FF
shl byte0, DBASE
andn outa, DATMASK ' D7..0 low
or outa, byte1 ' write MSB
or outa, WRC ' clock it out
andn outa, WRC
andn outa, DATMASK ' D7..0 low
or outa, byte0 ' write LSB
or outa, WRC ' clock it out
andn outa, WRC
add ptr_data, #2 ' advance ptr to next word
djnz xcnt, #:dwordloop ' loop if more bytes
jmp #cmdexit ' return
wrbytex_data
' Repeatedly write the same byte of data, xcnt times
or outa, DC ' DC high: data
rdlong dbyte, ptrbuff ' get pointer to word of data
shl dbyte, DBASE
rdlong xcnt, ptr_xcnt ' and number of words
:repdloop
andn outa, DATMASK ' D7..0 low
or outa, dbyte ' write MSB
or outa, WRC ' clock it out
andn outa, WRC
djnz xcnt, #:repdloop ' loop if more words
jmp #cmdexit ' return
wrwordx_data
' Repeatedly write the same word of data, xcnt times
or outa, DC ' DC high: data
rdlong dword, ptrbuff ' get pointer to word of data
rdlong xcnt, ptr_xcnt ' and number of words
mov byte1, dword ' isolate MSB of word
shr byte1, #8
shl byte1, DBASE
mov byte0, dword ' and LSB
and byte0, #$FF
shl byte0, DBASE
:repdloop
andn outa, DATMASK ' D7..0 low
or outa, byte1 ' write MSB
or outa, WRC ' clock it out
andn outa, WRC
andn outa, DATMASK ' D7..0 low
or outa, byte0 ' write LSB
or outa, WRC ' clock it out
andn outa, WRC
djnz xcnt, #:repdloop ' loop if more words
jmp #cmdexit ' return
DATMASK long 0
DBASE long 0
CS long 0
WRC long 0
DC long 0
RD long 0
iolink long 0
tmp0 long 0
ptr_data long 0
dbyte long 0
dword long 0
cmdbyte long 0
clrcmd long IDLE
ptrbuff long 0
ptr_xcnt long 0
xcnt long 0
byte1 long 0
byte0 long 0
DAT
{
Copyright 2024 Jesse Burt
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
}