]> git.sur5r.net Git - cc65/blob - libsrc/lynx/upload.s
Oliver added a comment to the Apple2 linker config.
[cc65] / libsrc / lynx / upload.s
1 ; ***
2 ; CC65 Lynx Library
3 ;
4 ; Originally by Bastian Schick
5 ; http://www.geocities.com/SiliconValley/Byte/4242/lynx/
6 ;
7 ; Ported to cc65 (http://www.cc65.org) by
8 ; Shawn Jefferson, June 2004
9 ;
10 ; ***
11 ;
12 ; void install_uploader(unsigned char divider);
13 ;
14 ; Installs an interrupt handler that hooks the comlynx rx/tx interrupts to
15 ; allow upload of code to the lynx from another computer via the comlynx
16 ; cable.  divider values are in lynx.h
17 ;
18 ; Loader is installed under the mikey chip at $FE00.
19 ;
20
21         .include        "lynx.inc"
22         .export         _lynx_install_uploader
23         .import         _install_irq
24         .import         popa, pusha
25
26
27 ; ------------------------------------------------------------------------
28
29 .segment        "EXTZP": zeropage
30
31 load_len:       .res    2
32 load_ptr:       .res    2
33 load_ptr2:      .res    2
34
35
36         .code
37
38 ; ------------------------------------------------------------------------
39 ; The following code will get moved to $FE00 when installing the uploader.
40
41 .proc   Loader
42
43 .org    $fe00
44
45 run:    ldy     #4
46 loop0:  jsr     read_byte
47         sta     load_len-1,y
48         dey
49         bne     loop0       ; get destination and length
50         tax                 ; lowbyte of length
51
52         lda     load_ptr
53         sta     load_ptr2
54         lda     load_ptr+1
55         sta     load_ptr2+1
56
57 loop1:  inx
58         bne     cont1
59         inc     load_len+1
60         bne     cont1
61         jmp     (load_ptr)
62
63 cont1:  jsr     read_byte
64         sta     (load_ptr2),y
65         sta     PALETTE         ; feedback ;-)
66         iny
67         bne     loop1
68         inc     load_ptr2+1
69         bra     loop1
70
71 read_byte:
72         bit     SERCTL
73         bvc     read_byte
74         lda     SERDAT
75         rts
76
77 .reloc
78
79 .endproc
80
81
82 ; ------------------------------------------------------------------------
83
84 .code
85
86 _lynx_install_uploader:
87
88         ldx     #.sizeof(Loader)-1      ; put Loader in the right place
89 loop:   lda     Loader,x                ; x is length of loader
90         sta     Loader::run,x
91         dex
92         bpl     loop
93
94 ;
95 ; install serial-irq  vector
96 ;
97         lda     #4
98         jsr     pusha
99         lda     #<UpLoader
100         ldx     #>UpLoader
101         jsr     _install_irq    ; set vector
102 ;
103 ; set baudrate
104 ;
105         jsr     popa            ; get divider
106         sta     $fd10
107         lda     #%00011000      ; Baudrate = 1MHz/16/(divider+1)
108         sta     $fd11
109 ;
110 ; set ComLynx parameters
111 ;
112         lda     #%00011101      ; even par
113         sta     SERCTL          ; set 8E1
114 ;
115 ; clear Rx-buffer
116 ;
117 clear:  bit     SERCTL
118         bvc     ok0
119         ldx     SERDAT
120         bra     clear
121 ;
122 ; enable Rx-interrupt
123 ;
124 ok0:    ora     #$40
125         sta     SERCTL
126         rts
127
128
129 ; ------------------------------------------------------------------------
130 ; Rx-IRQ-handler
131
132 UpLoader:
133         lda     SERDAT          ; wait for the start sequence
134         bit     flag            ; already seen $81 ?
135         bpl     again           ; >= 0 => no
136         cmp     #$50            ; "P" ?
137         bne     again           ; not correct, so clear flag
138         jmp     Loader::run
139
140 again:  stz     flag
141         cmp     #$81
142         bne     exit
143         sta     flag
144 ;
145 ; last action : clear interrupt
146 ;
147 exit:   lda     #$10
148         sta     INTRST
149         rts
150
151
152 .data
153
154 flag:           .byte   0
155