/
low.asm
1154 lines (1129 loc) · 24 KB
/
low.asm
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;Low-level USB routines
include "settings.inc"
include "equates.inc"
SEGMENT Main
GLOBALS ON
EXTERN WaitTimerBms,putAtoHL_ExtraRAMPage,getAfromHL_ExtraRAMPage,DispHexA,WaitTimer20ms,WaitTimer40ms
EXTERN WaitTimer100ms,SendControlDataIncoming,HandleUSBActivity,SendControlDataOutgoing,ClearControlBuffer
EXTERN IPutC
IsInterruptMode:
;Returns Z set if using interrupts, or NZ if using "no interrupt" mode.
ld a,(USBFlags)
and 1
ret
InitializeUSB_Peripheral:
;Initializes the USB controller in peripheral mode.
;Returns carry set if problems
;Hold the USB controller in reset
xor a
out (4Ch),a
;Disable 48MHz crystal, power down Toshiba PHY,
; and disable USB suspend interrupt
ld a,2
out (54h),a
;Set D- switches to default
ld a,20h
out (4Ah),a
;Enable the various clocks we need
call SetupClocks
;Release USB controller reset and enable protocol interrupts
ld a,1
out (5Bh),a
ld a,8
out (4Ch),a
;Wait until IP clock is enabled
ld de,DEFAULT_TIMEOUT
$$: call DecrementCounter
;TODO: This needs to go to an error routine instead
scf
ret z
in a,(4Ch)
res 6,a ;don't care about charge pump setting
cp 1Ah
jr nz,$B
;Enable all pipes for input and output
ld a,0FFh
out (87h),a
xor a
out (92h),a ;not sure what this is or if it's necessary
ld a,0Eh
out (89h),a
;Set the protocol interrupt mask, fires when:
; Bus is suspended
; Bus reset occurs
ld a,5
out (8Bh),a
;I don't know for sure, but I think this tells the controller to drive VBus.
in a,(81h)
set 0,a
out (81h),a
;Enable the bus suspend interrupt
in a,(54h)
set 0,a
out (54h),a
WaitForFrameCounterStart:
ld b,FRAME_COUNTER_TIMEOUT / DEFAULT_TIMEOUT
frameCounterLoop:
ld de,DEFAULT_TIMEOUT
$$: call DecrementCounter
jr z,counterExpired
in a,(8Ch)
or a
jr z,$B
or a
ret
counterExpired:
djnz frameCounterLoop
;TODO: This needs to go to an error routine instead
scf
ret
SetupClocks:
IFDEF TI84PCSE
call SetGPIOPin6
ENDIF
;Set up internal charge pump
xor a
out (4Bh),a
in a,(3Ah)
bit 3,a
jr z,$F
;We have an external charge pump, so disable the internal one
ld a,20h
out (4Bh),a
$$: ;Power down Toshiba PHY, disable USB suspend interrupt,
; and stop master 48MHz disable signal
xor a
out (54h),a
;Wait 10ms for the hardware to deal with that
ld b,1
call WaitTimerBms
;Enable 48MHz clock and take Toshiba PHY out of power down mode
in a,(3Ah)
bit 3,a
jr z,$F
;We have an external charge pump, don't enable the internal one
ld a,44h
out (54h),a
$$: ;But then we enable it anyway? This might be an OS bug carried over to here.
ld a,0C4h
out (54h),a
ret
SetGPIOPin6:
;No idea what this does.
;Set GPIO output pin 6
in a,(39h)
or 40h
out (39h),a
in a,(3Ah)
or 40h
out (3Ah),a
jr WaitTimer100ms
InitializeUSB_Host:
;Initializes the USB controller in host mode.
;Returns carry set if problems
IFDEF TI84PCSE
call SetGPIOPin6
ENDIF
;Get out if controller is not reset or suspended, and IP clock is enabled
in a,(4Ch)
and 1Ah
ret z
;Hold the controller in reset
xor a
out (4Ch),a
;Enable USB interrupts
ld a,1
out (5Bh),a
xor a
in a,(4Ch)
;Disable 48MHz crystal, power down Toshiba PHY,
; and disable USB suspend interrupt
ld a,2
out (54h),a
;Set D- switches to default
ld a,20h
out (4Ah),a
;Enable the various clocks we need
call SetupClocks
iusbhStart:
ld de,DEFAULT_TIMEOUT
$$: call DecrementCounter
;TODO: This needs to go to an error routine instead
scf
ret z
in a,(4Ch)
bit 0,a
jr nz,iusbhDone ;bus is suspended, apparently we should get out now
;Is IP clock enabled? (this is 1Ah/5Ah ignoring controller reset bit)
res 6,a
cp 12h
jr nz,$B
;Yes, continue
in a,(4Dh)
bit 5,a
jr z,$F
;We're the B device; request host control from the other device
call RequestHostControl
ret c
;Make sure we get B cable unplug and A cable plug-in events? OTG screwery...
ld a,90h
out (57h),a
jr iusbhContinue
$$: ;We're the A device; drive VBus (I think)
call EnableHostMode
iusbhContinue:
;Enable all pipes for input/output
ld a,0FFh
out (87h),a
xor a
out (92h),a ;not sure what this is
ld a,0Eh
out (89h),a
;Enable miscellaneous interrupts:
; 0: Bus suspended
; 5: Something unplugged
; 7: Insufficient power
ld a,0A1h
out (8Bh),a
ld de,DEFAULT_TIMEOUT
$$: call DecrementCounter
;TODO: This needs to go to an error routine instead
scf
ret z
in a,(81h)
bit 6,a
jr z,$B
;Is host bit set?
in a,(8Fh)
bit 2,a
jr z,iusbhStart ;no, try again
;Wait for frame counter to start
call WaitForFrameCounterStart
ret c
;Finally, we are host (which can happen regardless of which cable is attached).
xor a
out (5Bh),a
call ResetAddress
ld a,1
out (5Bh),a
iusbhDone:
or a
ret
EnableHostModeFromBDevice:
;Initiates an SRP request.
in a,(3Ah)
bit 3,a
jr nz,ehmOld
call WaitForVBusLow
ret c
;Set external discharge timeout
ld a,27h
out (50h),a
;Clear GPIO pins 0 and 1, set GPIO pin 2
in a,(3Ah)
and 0FCh
or 4
out (3Ah),a
;Enable GPIO pins 0-2 for output
in a,(39h)
or 7
out (39h),a
;Disable SRP timeout interrupt
;Enable external discharge and start SRP request
in a,(4Fh)
and 0BFh
or 88h
out (4Fh),a
call WaitTimer20ms
;Disable external discharge and stop SRP request
in a,(4Fh)
and 37h
out (4Fh),a
;Clear GPIO pins 0-2
in a,(3Ah)
and 0F8h
out (3Ah),a
;Enable GPIO pins 0-2 for output
in a,(39h)
or 7
out (39h),a
;Release controller reset
ld a,8
out (4Ch),a
;Drive VBus (I think)
ld a,1
out (8Fh),a
ld b,2
ehmfbdLoop:
ld de,DEFAULT_TIMEOUT
$$: call DecrementCounter
jr z,$F
in a,(4Dh)
bit 6,a
jr z,$B
;Disable GPIO pins 0-2 for output
in a,(39h)
and 0F8h
out (39h),a
or a
ret
$$: djnz ehmfbdLoop
scf
ret
EnableHostMode:
;Enables host mode (sets the "host bit") while B device is connected.
in a,(3Ah)
bit 3,a
jr nz,ehmNew ;use new method
ehmOld:;Release controller reset
ld a,8
out (4Ch),a
;I'm not sure what bit 5 of port 81h indicates...
ld de,DEFAULT_TIMEOUT
$$: call DecrementCounter
;TODO: This needs to go to an error routine instead
scf
ret z
in a,(81h)
bit 5,a
jr nz,$B
;Drive VBus (I think)
ld a,1
out (8Fh),a
jr WaitTimer20ms
ehmNew:
call WaitForVBusLow
ret c
;Clear GPIO pins 0 and 1
in a,(3Ah)
and 0FDh
out (3Ah),a
;Enable output on GPIO pin 1
in a,(39h)
or 2
out (39h),a
;Release controller reset
ld a,8
out (4Ch),a
;Drive VBus (I think)
ld a,1
out (8Fh),a
call WaitForVBusHigh
ret c
;Disable output on GPIO pins 0 and 1
in a,(39h)
and 0FDh
out (39h),a
ret
RequestHostControl:
;Requests host control from the connected A device.
call EnableHostModeFromBDevice
;TODO: This needs to go to an error routine instead
ret c
;Disable all USB interrupts
xor a
out (5Bh),a
out (8Bh),a
ld b,50
call WaitTimerBms
ld a,3
out (8Fh),a
;Enable all known miscellaneous interrupts
ld a,0F7h
out (8Bh),a
;Wait for new device to connect
ld de,DEFAULT_TIMEOUT
$$: call DecrementCounter
;TODO: This needs to go to an error routine instead
scf
ret z
in a,(86h)
bit 4,a
jr z,$B
call WaitTimer20ms
in a,(81h)
and 0F7h
out (81h),a
call ResetAddress
in a,(8Fh)
xor a
ret
ResetAddress:
ld a,8
out (81h),a
call WaitTimer20ms
xor a
out (81h),a
call WaitTimer40ms
xor a
out (80h),a
ret
GetDeviceDescriptor:
;Get device descriptor.
;Inputs: B: page to store data
; HL: address to store data
; DE: number of bytes to retrieve
call ClearControlBuffer
ld hl,0680h
ld (controlBuffer),hl
ld a,01h
ld (controlBuffer+3),a
ld (controlBuffer+6),de
;Fall through to SendControlRequest
SendControlRequest:
;Issues control request with potential incoming/outgoing data.
;Inputs: controlBuffer: SETUP data
; B: page containing data to send/receive, if any
; HL: address of data to send/receive, if any
; DE: number of bytes to send or expected to receive
;Returns carry flag set if problems
ld a,(controlBuffer)
bit 7,a
jr nz,SendControlRequest_DeviceToHost
push de
ld de,(controlBuffer+6)
ld a,d
or e
pop de
jr z,SendControlRequest_NoData
;Fall through to SendControlRequest_HostToDevice
SendControlRequest_HostToDevice:
;Issues control request with potential host->device data sent.
;Inputs: B: page containing data to send, if any
; HL: address of data to send, if any
;Returns carry flag set if problems
ld de,(controlBuffer+6)
call SendControlDataOutgoing
ld de,DEFAULT_TIMEOUT
scrhtdWaitLoop:
call DecrementCounter
scf
ret z
call IsInterruptMode
jr nz,$F
ei
halt
jr scrhtdContinue
$$: push de
call HandleUSBActivity
pop de
scrhtdContinue:
ld hl,USBFlags
bit sendingControlData,(hl)
jr nz,scrhtdWaitLoop
or a
ret
SendControlRequest_DeviceToHost:
SendControlRequest_NoData:
;Issues control request with potential device->host data received.
;Inputs: B: page to receive incoming data
; HL: address to receive incoming data
;Outputs: TODO: Bytes actually received
;Returns carry flag set if problems
ld de,(controlBuffer+6)
call SendControlDataIncoming
ld de,DEFAULT_TIMEOUT
scrdthWaitLoop:
call DecrementCounter
scf
ret z
call IsInterruptMode
jr nz,$F
ei
halt
jr scrdthContinue
$$: push de
call HandleUSBActivity
pop de
scrdthContinue:
ld hl,USBFlags
bit receivingControlDataHost,(hl)
jr nz,scrdthWaitLoop
or a
ret
WaitForVBusLow:
;Wait for VBus to go low
ld de,DEFAULT_TIMEOUT
$$: call DecrementCounter
;TODO: This needs to go to an error routine instead
scf
ret z
in a,(4Dh)
bit 6,a
jr nz,$B
or a
ret
WaitForVBusHigh:
;Wait for VBus to go high
ld de,DEFAULT_TIMEOUT
$$: call DecrementCounter
;TODO: This needs to go to an error routine instead
scf
ret z
in a,(4Dh)
bit 6,a
jr z,$B
or a
ret
StallControlPipe:
;This is for peripheral mode only. 60h has a different meaning in host mode.
xor a
out (8Eh),a
ld a,60h
out (91h),a
ret
FinishControlRequest:
;Yes, you need the dummy reads. Apparently.
xor a
out (8Eh),a
in a,(91h)
ld a,48h
out (91h),a
in a,(91h)
ret
;------------------------------------------------------------------------------
SetupOutPipe:
;Sets up outgoing pipe
;Inputs: A is max packet size / 8
out (90h),a
ld a,48h
out (91h),a
xor a
out (92h),a
ret
SetupInPipe:
;Sets up incoming pipe
;Inputs: A is max packet size / 8
out (93h),a
ld a,90h
out (94h),a
xor a
out (95h),a
ret
SendInterruptData:
;Sends B bytes at HL to interrupt pipe C
;Returns carry if problems
LOG IntData,b
push hl
; ld hl,USBflag
; bit driverConfigured,(hl)
pop hl
scf
ret z
set indicOnly,(iy+indicFlags)
ld a,c
; ld (asm_ram),a
ld c,0A0h
add a,c
ld c,a
push bc
ld de,0FFh
$$: call DecrementCounter
scf
pop bc
jr z,sendInterruptDataDone
push bc
in a,(8Eh)
ld c,a
; ld a,(asm_ram)
out (8Eh),a
in a,(91h)
ld b,a
ld a,c
out (8Eh),a
ld a,b
and 1
jr nz,$B
pop bc
$$: ld a,(hl)
out (c),a
inc hl
djnz $B
in a,(8Eh)
ld c,a
; ld a,(asm_ram)
out (8Eh),a
ld a,1
out (91h),a
ld a,c
out (8Eh),a
xor a
sendInterruptDataDone:
res indicOnly,(iy+indicFlags)
ret
DecrementCounter:
dec de
ld a,d
or e
ret
scfRet:
scf
ret
SendBulkData:
;Sends DE bytes at HL to bulk pipe 01
;Duplicates OS functionality
ld c,01h
SendBulkDataC:
set 0,(iy+41h)
LOG BulkData,d
LOG BulkData,e
res 5,(iy+41h)
ld a,0
ld (9C7Dh),a
ld (9C81h),de
ld (9C7Eh),hl
in a,(8Fh)
bit 2,a
jr z,$F
; ret
$$: ld a,c
out (8Eh),a
ld a,20h
or c
out (98h),a
xor a
in a,(98h)
ld a,8
out (90h),a
xor a
in a,(90h)
ld a,0FFh
out (87h),a
xor a
out (92h),a
in a,(87h)
ld a,0A1h
out (8Bh),a
in a,(8Bh)
; in a,(86h)
jr startSendData
sendDataLoop:
xor a
out (5Bh),a
set 0,(iy+41h)
ld b,40h
ld a,c
add a,0A0h
ld c,a
$$: call wasteTime
; bit useExtraRAMPages,(iy+periph8xFlags)
jr z,useRAM1
; call getAfromHL_ExtraRAMPage
jr continueSendLoop1
useRAM1:
ld a,(hl)
continueSendLoop1:
out (c),a
inc hl
dec de
djnz $B
ld a,c
sub 0A0h
ld c,a
out (8Eh),a
ld a,1
out (91h),a
; call DebugStart ;*** TESTING
waitLoop1:
res 0,(iy+43h)
bit 0,(iy+43h)
jr z,$F
in a,(91h)
and 0D9h
or 8
out (91h),a
scf
ret
$$: call isSendError
jr nz,scfRet
push bc
in a,(82h)
$$: srl a
dec c
jr nz,$B
and 1
pop bc
jr z,waitLoop1
; call DebugStop ;*** TESTING
in a,(91h)
bit 5,a
jr nz,scfRet
ld a,c
out (8Eh),a
ld a,8
out (91h),a
startSendData:
ld a,d
or a
jr nz,sendDataLoop
ld a,e
cp 40h
jr nc,sendDataLoop
push af
xor a
out (5Bh),a
set 0,(iy+41h)
pop af
or a
jr z,skipWrite
ld b,a
ld a,c
add a,0A0h
ld c,a
$$: call wasteTime
; bit useExtraRAMPages,(iy+periph8xFlags)
jr z,useRAM2
; call getAfromHL_ExtraRAMPage
jr continueSendLoop2
useRAM2:
ld a,(hl)
continueSendLoop2:
out (c),a
inc hl
djnz $B
ld a,c
sub 0A0h
ld c,a
skipWrite:
ld a,c
out (8Eh),a
ld a,1
out (91h),a
; call DebugStart ;*** TESTING
waitLoop2:
res 0,(iy+43h)
bit 0,(iy+43h)
jr z,$F
in a,(91h)
and 0D9h
or 8
out (91h),a
scf
ret
$$: call isSendError
jr nz,scfRet
push bc
in a,(82h)
$$: srl a
dec c
jr nz,$B
and 1
pop bc
jr z,waitLoop2
; call DebugStop ;*** TESTING
in a,(91h)
bit 5,a
jr nz,scfRet
ld a,c
out (8Eh),a
ld a,8
out (91h),a
ld a,1
out (5Bh),a
res 0,(iy+41h)
; ei
ret
isSendError:
in a,(86h)
bit 5,a
ret nz
bit 7,a
ret
wasteTime:
nop
inc hl
dec hl
push af
push af
pop af
pop af
inc hl
dec hl
nop
call scfRet
ccf
call scfRet
ccf
ret
ReceiveLargeData:
;Receives BC bytes to HL on bulk pipe 02
;Duplicates OS functionality
ld d,2
ReceiveLargeDataD:
res 2,(iy+40h)
res 5,(iy+43h)
push bc
push af
ld (iMathPtr5),hl
push de
receiveLargeLoop:
; set 2,(iy+12h)
res 3,(iy+8)
push bc
push hl
ld hl,40h
or a
sbc hl,bc
pop hl
ld b,c
jr nc,$F
ld b,40h
$$: pop hl
pop af
push af
push hl
ld d,a
ld hl,(iMathPtr5)
call ReceiveDataD
ld (iMathPtr5),hl
pop hl
; call initCrystalTimer
or a
ld b,0
sbc hl,bc
jr z,$F
ld b,h
ld c,l
jr receiveLargeLoop
$$: pop de
pop bc
pop bc
ret
ReceiveData:
;Receives B bytes to HL on bulk pipe 02, max size 40h
;Duplicates OS functionality
ld d,2
ReceiveDataD:
ld a,b
or a
ret z
ld a,40h
cp b
ret c
jr nz,$F
nop
$$: ld a,b
ld (9C80h),a
; ld a,(bytesRemaining)
or a
jr z,$F
cp b
jr nc,$F
ld b,a
ld (9C80h),a
$$: in a,(8Fh)
bit 2,a
jr z,$F
ret
$$: ld a,d
out (8Eh),a
in a,(94h)
bit 6,a
jr z,$F
and 0DFh
out (94h),a
pop af
scf
ret
$$: ;ld a,(bytesRemaining)
or a
jr nz,$F
in a,(96h)
$$: push af
ld c,0
receiveLoop:
ld a,d
add a,0A0h
push bc
ld c,a
in a,(c)
pop bc
; bit 2,(iy+40h)
; jr nz,receiveToZone1
;receiveToZone1:
; bit useExtraRAMPages,(iy+periph8xFlags)
jr z,$F
; call putAtoHL_ExtraRAMPage
jr continueReceiveLoop
$$: ld (hl),a
continueReceiveLoop:
inc hl
inc c
djnz receiveLoop
ld a,d
out (8Eh),a
pop af
sub c
; ld (bytesRemaining),a
ret nz
xor a
; ld (bytesRemaining),a
in a,(94h)
and 0FEh
out (94h),a
res 5,(iy+41h)
; ld a,0A1h
ld a,21h
out (8Bh),a
ld a,1
; out (5Bh),a
res 0,(iy+41h)
; ei
ret
initCrystalTimer:
;Sets up/Resets crystal timer that OS uses for USB stuff
; di
xor a
out (33h),a
ld a,43h
out (33h),a
ld a,3
out (34h),a
ld a,(90A8h)
ld (90AAh),a
ld a,(90A9h)
ld (90ABh),a
or a
ld a,0FFh
jr nz,$F
ld a,(90A8h)
$$: out (35h),a
res 0,(iy+43h)
; ei
ret
initUSBStuff:
;Initializes memory and ports that OS uses for USB stuff
;Duplicates OS functionality
set 1,(iy+43h)
res 0,(iy+42h)
ld hl,8449h
ld (hl),74h
res 0,(iy+42h)
ld de,0FAh
ld (90A0h),de
ld (9094h),de
ld a,14h
ld (90A8h),a
xor a
ld (90A9h),a
ld a,0
ld (90A5h),a
ret
sendUSBData_BCbytesFromHL:
;Sends BC bytes from HL out bulk pipe 01
;Duplicates OS functionality
push hl
push af
push de
push bc
push bc
pop de
call SendBulkData
; res useExtraRAMPages,(iy+periph8xFlags)
jr c,sendFailed
xor a
jr zeroToDEret
sendFailed:
ld de,0FFFFh
or 1
jr saveDEret
zeroToDEret:
ld de,0
saveDEret:
ld (90A6h),de
pop bc
pop de
pop hl
ld a,h
pop hl
ret
receiveAndWriteUSBData:
;Receives BC bytes to HL from bulk pipe 02
;Duplicates OS functionality
set 2,(iy+40h)
jr $F
receiveAndWriteUSBData_resFlag:
res 2,(iy+40h)
$$: res 5,(iy+43h)
receiveAndWriteUSBData_sub:
push bc
push af
ld (iMathPtr5),hl
bit 7,h
jr nz,receiveUSBDataAndWriteLoop
xor a
ld (pagedCount),a
receiveUSBDataAndWriteLoop:
bit 5,(iy+41h)
jr nz,doTheRealReceiveAndWrite
bit 0,(iy+43h)
jr nz,returnProblemsBCis3
bit 5,(iy+43h)
jr z,doNotCheckForKeyLinkActivity
res 2,(iy+12h)
set 3,(iy+8)
; B_CALL HandleLinkActivity
ld a,(apdTimer)
cp 1
jr z,returnProblemsBCis3
doNotCheckForKeyLinkActivity:
bit 5,(iy+41h)
jr nz,doTheRealReceiveAndWrite
push hl
push bc
ld hl,error_handler2
call APP_PUSH_ERRORH
; call checkForErrBreak
call APP_POP_ERRORH