-
Notifications
You must be signed in to change notification settings - Fork 3
/
wic64.h
496 lines (383 loc) · 10.8 KB
/
wic64.h
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
!zone wic64 {
;---------------------------------------------------------
; Command constants
;---------------------------------------------------------
WIC64_GET_VERSION_STRING = $00
WIC64_GET_VERSION_NUMBERS = $26
WIC64_SCAN_WIFI_NETWORKS = $0c
WIC64_CONNECT_WITH_SSID_STRING = $02
WIC64_CONNECT_WITH_SSID_INDEX = $0d
WIC64_IS_CONFIGURED = $2f
WIC64_IS_CONNECTED = $2c
WIC64_GET_MAC = $14
WIC64_GET_SSID = $10
WIC64_GET_RSSI = $11
WIC64_GET_IP = $06
WIC64_HTTP_GET = $01
WIC64_HTTP_GET_ENCODED = $0f
WIC64_HTTP_POST_URL = $28
WIC64_HTTP_POST_DATA = $2b
WIC64_TCP_OPEN = $21
WIC64_TCP_AVAILABLE = $30
WIC64_TCP_READ = $22
WIC64_TCP_WRITE = $23
WIC64_TCP_CLOSE = $2e
WIC64_GET_SERVER = $12
WIC64_SET_SERVER = $08
WIC64_GET_TIMEZONE = $17
WIC64_SET_TIMEZONE = $16
WIC64_GET_LOCAL_TIME = $15
WIC64_UPDATE_FIRMWARE = $27
WIC64_REBOOT = $29
WIC64_GET_STATUS_MESSAGE = $2a
WIC64_SET_TRANSFER_TIMEOUT = $2d
WIC64_SET_REMOTE_TIMEOUT = $32
WIC64_IS_HARDWARE = $31
WIC64_FORCE_TIMEOUT = $fc
WIC64_FORCE_ERROR = $fd
WIC64_ECHO = $fe
WIC64_SUCCESS = $00
WIC64_INTERNAL_ERROR = $01
WIC64_CLIENT_ERROR = $02
WIC64_CONNECTION_ERROR = $03
WIC64_NETWORK_ERROR = $04
WIC64_SERVER_ERROR = $05
;---------------------------------------------------------
; Opcode constants
;---------------------------------------------------------
wic64_lda_abs = $ad
wic64_sta_abs_y = $99
wic64_lda_abs_y = $b9
wic64_inc_abs = $ee
wic64_nop = $ea
;---------------------------------------------------------
; Assembly time options
;---------------------------------------------------------
!ifndef wic64_optimize_for_size {
wic64_optimize_for_size = 0
}
!ifndef wic64_include_enter_portal {
wic64_include_enter_portal = 0
}
!ifndef wic64_include_load_and_run {
!if (wic64_include_enter_portal != 0) {
wic64_include_load_and_run = 1
} else {
wic64_include_load_and_run = 0
}
}
;---------------------------------------------------------
; Runtime options
;---------------------------------------------------------
!macro wic64_set_timeout .timeout {
lda #.timeout
sta wic64_timeout
sta wic64_configured_timeout
}
;---------------------------------------------------------
!macro wic64_dont_disable_irqs {
lda #$01
sta wic64_dont_disable_irqs
}
!macro wic64_disable_irqs {
lda #$00
sta wic64_dont_disable_irqs
}
;---------------------------------------------------------
!macro wic64_set_timeout_handler .addr {
lda #<.addr
sta wic64_timeout_handler
lda #>.addr
sta wic64_timeout_handler+1
tsx
stx wic64_timeout_handler_stackpointer
}
!macro wic64_unset_timeout_handler {
lda #$00
sta wic64_timeout_handler
sta wic64_timeout_handler+1
}
;---------------------------------------------------------
!macro wic64_set_error_handler .addr {
lda #<.addr
sta wic64_error_handler
lda #>.addr
sta wic64_error_handler+1
tsx
stx wic64_error_handler_stackpointer
}
!macro wic64_unset_error_handler {
lda #$00
sta wic64_error_handler
sta wic64_error_handler+1
}
;---------------------------------------------------------
!macro wic64_set_store_instruction .addr {
ldy #$02
- lda .addr,y
sta wic64_store_instruction_pages,y
sta wic64_store_instruction_bytes,y
dey
bpl -
lda #wic64_lda_abs
sta wic64_destination_pointer_highbyte_inc
}
!macro wic64_reset_store_instruction {
jsr wic64_reset_store_instruction
}
;---------------------------------------------------------
!macro wic64_set_fetch_instruction .addr {
ldy #$02
- lda .addr,y
sta wic64_fetch_instruction_pages,y
sta wic64_fetch_instruction_bytes,y
dey
bpl -
lda #wic64_lda_abs
sta wic64_source_pointer_highbyte_inc
}
!macro wic64_reset_fetch_instruction {
jsr wic64_reset_fetch_instruction
}
;---------------------------------------------------------
; Transfer functions
;---------------------------------------------------------
!macro wic64_initialize {
jsr wic64_initialize
}
;---------------------------------------------------------
!macro wic64_set_request .request {
lda #<.request
sta wic64_request
lda #>.request
sta wic64_request+1
}
;---------------------------------------------------------
!macro wic64_send_header {
jsr wic64_send_header
}
!macro wic64_send_header .request {
+wic64_set_request .request
jsr wic64_send_header
}
;---------------------------------------------------------
!macro wic64_send {
+wic64_prepare_transfer_of_remaining_bytes
jsr wic64_send
}
!macro wic64_send .source {
+wic64_set_request .source
+wic64_prepare_transfer_of_remaining_bytes
jsr wic64_send
}
!macro wic64_send .source, .size {
+wic64_set_request .source
lda #<.size
sta wic64_bytes_to_transfer
lda #>.size
sta wic64_bytes_to_transfer+1
jsr wic64_send
}
!macro wic64_send .source, ~.size {
+wic64_set_request .source
lda .size
sta wic64_bytes_to_transfer
lda .size+1
sta wic64_bytes_to_transfer+1
jsr wic64_send
}
;---------------------------------------------------------
!macro wic64_receive_header {
jsr wic64_receive_header
}
;---------------------------------------------------------
!macro wic64_set_response .response {
lda #<.response
sta wic64_response
lda #>.response
sta wic64_response+1
}
;---------------------------------------------------------
!macro wic64_receive {
+wic64_prepare_transfer_of_remaining_bytes
jsr wic64_receive
}
!macro wic64_receive .destination {
+wic64_set_response .destination
+wic64_prepare_transfer_of_remaining_bytes
jsr wic64_receive
}
!macro wic64_receive .destination, .size {
+wic64_set_response .destination
lda #<.size
sta wic64_bytes_to_transfer
lda #>.size
sta wic64_bytes_to_transfer+1
jsr wic64_receive
}
!macro wic64_receive .destination, ~.size {
+wic64_set_response .destination
lda .size
sta wic64_bytes_to_transfer
lda .size+1
sta wic64_bytes_to_transfer+1
jsr wic64_receive
}
;---------------------------------------------------------
!macro wic64_finalize {
jsr wic64_finalize
}
;---------------------------------------------------------
!macro wic64_execute .request {
+wic64_set_request .request
lda wic64_store_instruction_pages
cmp #wic64_sta_abs_y
bne +
lda #$00
sta wic64_auto_discard_response
+ jsr wic64_execute
}
!macro wic64_execute .request, .response {
+wic64_set_request .request
+wic64_set_response .response
jsr wic64_execute
}
!macro wic64_execute .request, .response, .timeout {
lda wic64_timeout
sta wic64_configured_timeout
lda #.timeout
sta wic64_timeout
+wic64_execute .request, .response
}
;---------------------------------------------------------
!macro wic64_detect {
jsr wic64_detect
}
;---------------------------------------------------------
!macro wic64_load_and_run .request {
+wic64_load_and_run .request, $02
}
!macro wic64_load_and_run .request, .timeout {
+wic64_set_request .request
lda wic64_timeout
sta wic64_configured_timeout
lda #.timeout
sta wic64_timeout
jsr wic64_load_and_run
}
!if (wic64_include_enter_portal != 0) {
!macro wic64_enter_portal {
jsr wic64_enter_portal
}
}
;---------------------------------------------------------
; Internal macros
;---------------------------------------------------------
;---------------------------------------------------------
; Define macro wic64_wait_for_handshake
;---------------------------------------------------------
;
; If wic64_optimize_for_size is set to a nonzero value,
; a subroutine will be defined using the code defined
; in the macro wic64_wait_for_handshake_code and the
; macro will be defined to simply call this subroutine.
;
; Otherwise the macro will contain the code directly.
;
; Note that optimizing for size will significantly decrease
; transfer speed by about 30% due to the jsr/rts overhead
; added for every byte transferred.
;---------------------------------------------------------
!macro wic64_wait_for_handshake_code {
; wait until a handshake has been received from the ESP,
; e.g. the FLAG2 line on the userport has been asserted,
; with sets bit 4 of $dd0d. Set the carry flag to indicate
; a timeout if the timeout length specified in wic64_timeout
; has been exceeded.
; do a first cheap test for FLAG2 before wasting cycles
; setting up the counters.
lda #$10
bit $dd0d
!if (wic64_optimize_for_size == 0) {
bne .success
} else {
beq +
rts
}
; no fast response, setup timeout delay loop
+ lda wic64_timeout
sta wic64_counters+2
cmp #$01
bne +
lda #$48
+ sta wic64_counters+1
; keep testing for FLAG2 until all counters are down to zero
lda #$10
.wait
bit $dd0d
!if (wic64_optimize_for_size == 0) {
bne .success
} else {
beq +
rts
}
+ dec wic64_counters+0
bne .wait
dec wic64_counters+1
bne .wait
dec wic64_counters+2
bne .wait
.timeout
jmp wic64_handle_timeout
.success
}
!if (wic64_optimize_for_size == 0) {
!macro wic64_wait_for_handshake {
+wic64_wait_for_handshake_code
}
}
;---------------------------------------------------------
; set the source pointer to read request data from
; in wic64_send (self-modifying code)
!macro wic64_set_source_pointer_from .addr {
; only set the pointer if no custom fetch
; operation has been installed
lda wic64_fetch_instruction_pages
cmp #wic64_lda_abs_y
bne .done
lda .addr
sta wic64_source_pointer_pages
lda .addr+1
sta wic64_source_pointer_pages+1
.done
}
;---------------------------------------------------------
; set the destination pointer to write response data to
; in wic64_receive (self-modifying code)
!macro wic64_set_destination_pointer_from .addr {
; only set the pointer if no custom store
; operation has been installed
lda wic64_store_instruction_pages
cmp #wic64_sta_abs_y
bne .done
lda .addr
sta wic64_destination_pointer_pages
lda .addr+1
sta wic64_destination_pointer_pages+1
.done
}
;---------------------------------------------------------
; Make sure the next call to wic64_send or wic64_receive
; will transfer all bytes not sent or received so far.
!macro wic64_prepare_transfer_of_remaining_bytes {
jsr wic64_prepare_transfer_of_remaining_bytes
}
;---------------------------------------------------------
; Substract the number of bytes transferred in the previous
; call to wic64_send or wic64_receive from the number of
; bytes remaining to be transferred in the currend request
; or response
!macro wic64_update_transfer_size_after_transfer {
jsr wic64_update_transfer_size_after_transfer
}
}