Fix stupid ethernet bug
[fpgaboy.git] / ethernet.asm
CommitLineData
43282fcf
JW
1ETH_STATUS EQU $FF68
2ETH_DATA EQU $FF69
3
4PUTC: MACRO
5 ld a, \1
6 call putc
7 ENDM
8
9PUTS: MACRO
10 ld hl, \1
11 call puts
12 ENDM
13
14PUTSI: MACRO
15 push hl
16 call putsi
17 db \1, 0
18 pop hl
19 ENDM
20
21PUTSIL: MACRO
22 push hl
23 call putsi
24 db \1, 13, 10, 0
25 pop hl
26 ENDM
27
28PUTHEX: MACRO
29 ld a, \1
30 call puthex
31 ENDM
32
33 SECTION "config", HOME[$0]
34myMAC: db $12, $34, $56, $78, $9A, $00
35myIP: db $0A, $00, $00, $02
36myMask: db $FF, $00, $00, $00
37myGW: db $0A, $00, $00, $01
38
39 SECTION "boot", HOME[$100]
40 jr start
41 nop
42 nop
43
44 db $CE,$ED,$66,$66,$CC,$0D,$00,$0B,$03,$73,$00,$83 ;<<< no $00,$c3,$50,$01
45 db $00,$0C,$00,$0D,$00,$08,$11,$1F,$88,$89,$00,$0E,$DC,$CC,$6E,$E6
46 db $DD,$DD,$D9,$99,$BB,$BB,$67,$63,$6E,$0E,$EC,$CC,$DD,$DC,$99,$9F
47 db $BB,$B9,$33,$3E ; Standard Nintendo DO NOT CHANGE...
48
49 db "GB Ethernet " ; Cart name 16bytes
50 db $00,$00,$00 ; Not used
51 db $00 ; Cart type ROM Only
52 db $00 ; ROM Size 32k
53 db $00 ; RAM Size 0k
54 db $cf,$08 ; Maker ID Matthew Johnson
55 db $01 ; Version =1
56 db $87 ; Complement check (Important)
57 db $ff,$ff ; Cheksum, fix this if you are going to
58
59start:
60 ld sp, $D800
a6035e0c 61 call zerobss
43282fcf 62 PUTSIL "Ethernet test ROM"
a6035e0c 63.lp: xor a ; Reset state machines
43282fcf 64 ld [ETH_STATUS], a
a6035e0c 65 ld a, [ETH_STATUS] ; Load current status
43282fcf
JW
66 ld [$FF51], a
67 and $02 ; New packet?
68 call nz, getpacket
69 jr .lp
70
a6035e0c
JW
71zerobss:
72 ld hl, bssstart
73 ld bc, bssend - bssstart
74.lp: ld a, b
75 or c
76 ret z
77 dec bc
78 xor a
79 ld [hli], a
80 ld a, h
81 jr .lp
82
43282fcf
JW
83getpacket:
84 ld a, [ETH_DATA]
85 ld b, a
86 ld a, [ETH_DATA]
87 ld c, a
88 push bc
a6035e0c 89 ld hl, packet
43282fcf
JW
90.cplp: ld a, b
91 or c
92 jr z, .done
93 dec bc
94 ld a, [ETH_DATA]
95 ld [hli], a
96 jr .cplp
97.done: pop bc
98 PUTSI "getpacket: Got "
99 PUTHEX b
100 PUTHEX c
101 PUTSIL " byte packet from hardware."
102 ld hl, $D000
103 call ether_input
104 ret
105
106ether_input:
107 push hl
108 PUTSI "ether_input: Packet to "
109 PUTHEX [hli]
110 PUTC $3A
111 PUTHEX [hli]
112 PUTC $3A
113 PUTHEX [hli]
114 PUTC $3A
115 PUTHEX [hli]
116 PUTC $3A
117 PUTHEX [hli]
118 PUTC $3A
119 PUTHEX [hli]
120 PUTSI " from "
121 PUTHEX [hli]
122 PUTC $3A
123 PUTHEX [hli]
124 PUTC $3A
125 PUTHEX [hli]
126 PUTC $3A
127 PUTHEX [hli]
128 PUTC $3A
129 PUTHEX [hli]
130 PUTC $3A
131 PUTHEX [hli]
132 PUTSI " protocol "
133 PUTHEX [hli]
134 PUTHEX [hli]
135 PUTSIL "."
136 pop hl
137
138 ; Verify the destination MAC.
139 push hl
140 ld de, myMAC
141 ld c, $06
142.maclp: ld a, [de]
143 cp [hl]
144 jr nz, .notus
145 inc de
146 inc hl
147 dec c
148 jr nz, .maclp
a6035e0c
JW
149 ; Done? Then it was to us!
150 jr .us
151.notus: ; Was it to broadcast?
43282fcf 152 pop hl
a6035e0c
JW
153 push hl
154 ld c, $06
155.bclp: ld a, [hli]
156 cp $FF
157 jr nz, .notbc
158 dec c
159 jr nz, .bclp
160
161.us: PUTSIL "ether_input: It's to us!"
162 ; Which protocol does it belong to?
163 pop hl
164 ld b, $00
165 ld c, 12
166 add hl, bc ; Skip source and dest MAC.
167 ld a, [hli]
168 cp $08
169 jr nz, .unknownproto
170 ld a, [hli]
171 cp $06
172 jr z, .arp
173 cp $00
174 jp z, .ip
175 jr .unknownproto
176
177.notbc: pop hl
178 PUTSIL "ether_input: Not addressed to us and not broadcast."
179 ret
43282fcf 180
a6035e0c
JW
181.unknownproto:
182 PUTSIL "ether_input: Unknown protocol; dropping"
43282fcf 183 ret
a6035e0c
JW
184
185.arp:
186 PUTSIL "ether_input: ARP packet"
187 call arp_input
188 ret
189.ip:
190 PUTSIL "ether_input: IP packet"
191 call ip_input
43282fcf
JW
192 ret
193
a6035e0c
JW
194arp_input:
195 ret
196
197ip_input:
198 ret
199
200 SECTION "ethdata", BSS
201bssstart DS 0
202packet DS 1552
203bssend DS 0
204
205
43282fcf 206; SERIAL ROUTINES ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
a6035e0c 207 SECTION "serial", HOME
43282fcf
JW
208
209putc:
210 push af
211.waitport:
212 ld a,[$FF53]
213 and $01
214 jr nz,.waitport
215 pop af
216 ld [$FF52],a
217 ret
218
219puts:
220 ld a, [hli]
221 cp $00
222 ret z
223 call putc
224 jr puts
225
226EX_SP_HL: MACRO
227 push de
228 di
229 add sp, 2
230 pop de
231 push hl
232 ld l, e
233 ld h, d
234 add sp, -2
235 ei
236 pop de
237 ENDM
238
239putsi:
240 pop hl
241 push af
242 push bc
243 push de
244.lp: ld a, [hli]
245 or a
246 jr z, .done
247 call putc
248 jr .lp
249.done: pop de
250 pop bc
251 pop af
252 push hl
253 ret
254
255puthex: ; Put two hex nibbles to the serial console.
256 push bc
257 push hl
258 push af
259 swap a
260 and $0F
261 ld hl,hex
262 ld b,0
263 ld c,a
264 add hl,bc
265 ld a, [hl]
266 call putc
267 pop af
268 and $0F
269 ld hl,hex
270 ld c,a
271 add hl,bc
272 ld a, [hl]
273 call putc
274 pop hl
275 pop bc
276 ret
277hex: db "0123456789ABCDEF"
This page took 0.040124 seconds and 4 git commands to generate.