;
; File generated by cc65 v 2.13.1
;
	.fopt		compiler,"cc65 v 2.13.1"
	.setcpu		"6502"
	.smart		on
	.autoimport	on
	.case		on
	.debuginfo	on
	.importzp	sp, sreg, regsave, regbank, tmp1, ptr1, ptr2
	.macpack	longbranch
	.dbg		file, "../../../storage/flash/src/atmel_flash.c", 10250, 1267200486
	.dbg		file, "../inc/manifest.h", 151, 1267200480
	.dbg		file, "C:\Programme\cc65\include/stdio.h", 5964, 1262467470
	.dbg		file, "C:\Programme\cc65\include/stddef.h", 2972, 1262467470
	.dbg		file, "C:\Programme\cc65\include/stdarg.h", 2817, 1262467470
	.dbg		file, "../../../storage/ram/inc/mapper.h", 2486, 1267200486
	.dbg		file, "../../../data/inc/types.h", 1544, 1267200498
	.dbg		file, "../../../filesystem/fat/inc/ff.h", 11459, 1267200484
	.dbg		file, "../../../storage/flash/inc/flash.h", 1349, 1267200486
	.dbg		file, "../../../io/inc/gpio.h", 4272, 1267200496
	.import		_printf
	.import		_f_read
	.export		_read_hexbyte
	.export		_flash_erase_block
	.export		_flash_erase
	.export		_flash_file
	.export		_flash_get_cfg_page
	.export		_flash_get_cfg_sector
	.export		_flash_addr2block
	.export		_flash_protect
	.export		_flash_id
	.export		_flash_get_id

.segment	"RODATA"

L0001:
	.byte	$42,$6F,$6F,$74,$20,$61,$72,$65,$61,$20,$6F,$66,$20,$46,$6C,$61
	.byte	$73,$68,$20,$70,$72,$6F,$74,$65,$63,$74,$65,$64,$2E,$0A,$00,$46
	.byte	$6C,$61,$73,$68,$20,$49,$44,$3A,$20,$25,$30,$32,$58,$20,$25,$30
	.byte	$32,$58,$2E,$0A,$00,$43,$68,$65,$63,$6B,$73,$75,$6D,$20,$65,$72
	.byte	$72,$6F,$72,$2E,$0A,$00,$45,$6E,$64,$20,$72,$65,$63,$6F,$72,$64
	.byte	$2E,$0A,$00,$55,$6E,$73,$75,$70,$70,$6F,$72,$74,$65,$64,$20,$73
	.byte	$65,$67,$6D,$65,$6E,$74,$65,$64,$20,$61,$64,$64,$72,$65,$73,$73
	.byte	$2E,$0A,$00,$45,$78,$74,$65,$6E,$64,$65,$64,$20,$61,$64,$64,$72
	.byte	$65,$73,$73,$20,$77,$69,$74,$68,$20,$77,$72,$6F,$6E,$67,$20,$6C
	.byte	$65,$6E,$67,$74,$68,$2E,$0A,$00,$65,$72,$72,$6F,$72,$2E,$00,$74
	.byte	$69,$6D,$65,$6F,$75,$74,$2E,$20,$4D,$41,$50,$50,$45,$52,$3D,$25
	.byte	$30,$34,$78,$2C,$20,$6D,$61,$70,$70,$65,$64,$3D,$25,$30,$34,$78
	.byte	$2C,$20,$64,$61,$74,$61,$3D,$25,$30,$32,$78,$20,$25,$30,$32,$78
	.byte	$0A,$00,$56,$65,$72,$69,$66,$79,$20,$65,$72,$72,$6F,$72,$20,$61
	.byte	$74,$20,$62,$6C,$6F,$63,$6B,$20,$25,$64,$2C,$20,$6F,$66,$66,$73
	.byte	$65,$74,$20,$25,$30,$34,$78,$2E,$20,$52,$65,$61,$64,$20,$25,$30
	.byte	$32,$78,$2C,$20,$73,$68,$6F,$75,$6C,$64,$20,$62,$65,$20,$25,$30
	.byte	$32,$78,$0A,$00,$55,$6E,$73,$75,$70,$70,$6F,$72,$74,$65,$64,$20
	.byte	$72,$65,$63,$6F,$72,$64,$20,$74,$79,$70,$65,$20,$25,$32,$78,$2E
	.byte	$0A,$00,$54,$6F,$6F,$20,$6D,$61,$6E,$79,$20,$65,$72,$72,$6F,$72
	.byte	$73,$2E,$2E,$20,$65,$78,$69,$74,$69,$6E,$67,$2E,$0A,$00

.segment	"BSS"

_flash_id:
	.res	2,$00

; ---------------------------------------------------------------
; unsigned char __near__ read_hexbyte (__near__ struct _FIL*)
; ---------------------------------------------------------------

.segment	"CODE"

.proc	_read_hexbyte: near

.segment	"BSS"

L000D:
	.res	2,$00
L000E:
	.res	2,$00

.segment	"CODE"

;
; BYTE res = 0;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 65
	lda     #$00
	jsr     pusha
;
; f_read(fp, buf, 2, &bytes_read);
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 66
	jsr     decsp8
	ldy     #$0A
	lda     (sp),y
	tax
	dey
	lda     (sp),y
	ldy     #$06
	sta     (sp),y
	iny
	txa
	sta     (sp),y
	lda     #<(L000D)
	ldy     #$04
	sta     (sp),y
	iny
	lda     #>(L000D)
	sta     (sp),y
	lda     #$02
	tay
	sta     (sp),y
	iny
	lda     #$00
	sta     (sp),y
	lda     #<(L000E)
	ldy     #$00
	sta     (sp),y
	iny
	lda     #>(L000E)
	sta     (sp),y
	jsr     _f_read
;
; if(bytes_read == 2) {
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 67
	lda     L000E+1
	jne     L004E
	lda     L000E
	cmp     #$02
	jne     L004E
;
; if((buf[0] >= '0')&&(buf[0] <= '9')) res = (buf[0] & 0x0F) << 4;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 68
	lda     L000D
	sec
	sbc     #$30
	bvs     L001D
	eor     #$80
L001D:	bpl     L0018
	lda     L000D
	sec
	sbc     #$3A
	bvc     L0022
	eor     #$80
L0022:	bpl     L0018
	lda     L000D
	and     #$0F
;
; else if((buf[0] >= 'A')&&(buf[0] <= 'F')) res = (buf[0] - 55) << 4;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 69
	jmp     L0227
L0018:	lda     L000D
	sec
	sbc     #$41
	bvs     L002F
	eor     #$80
L002F:	bpl     L0029
	lda     L000D
	sec
	sbc     #$47
	bvc     L0034
	eor     #$80
L0034:	bpl     L0029
	lda     L000D
	sec
	sbc     #$37
L0227:	asl     a
	asl     a
	asl     a
	asl     a
	ldy     #$00
	sta     (sp),y
;
; if((buf[1] >= '0')&&(buf[1] <= '9')) res |= (buf[1] & 0x0F);
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 70
L0029:	lda     L000D+1
	sec
	sbc     #$30
	bvs     L0041
	eor     #$80
L0041:	bpl     L003C
	lda     L000D+1
	sec
	sbc     #$3A
	bvc     L0046
	eor     #$80
L0046:	bpl     L003C
	lda     L000D+1
	and     #$0F
;
; else if((buf[1] >= 'A')&&(buf[1] <= 'F')) res |= (buf[1] - 55);
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 71
	jmp     L0226
L003C:	lda     L000D+1
	sec
	sbc     #$41
	bvs     L0053
	eor     #$80
L0053:	bpl     L004E
	lda     L000D+1
	sec
	sbc     #$47
	bvc     L0058
	eor     #$80
L0058:	bpl     L004E
	lda     L000D+1
	sec
	sbc     #$37
L0226:	ldy     #$00
	ora     (sp),y
	sta     (sp),y
;
; return res;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 73
L004E:	ldx     #$00
	lda     (sp,x)
;
; }
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 74
	jmp     incsp3
	.dbg	line

.endproc

; ---------------------------------------------------------------
; unsigned char __near__ flash_erase_block (unsigned short)
; ---------------------------------------------------------------

.segment	"CODE"

.proc	_flash_erase_block: near

.segment	"CODE"

;
; if(d < 8)
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 160
	jsr     decsp2
	ldy     #$03
	lda     (sp),y
	tax
	dey
	lda     (sp),y
	cpx     #$00
	bne     L0095
	cmp     #$08
L0095:	bcs     L0093
;
; MAPPER_MAP1 = d + MAP_FLASH;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 161
	iny
	lda     (sp),y
	tax
	dey
	lda     (sp),y
;
; else
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 162
	jmp     L022D
;
; MAPPER_MAP1 = ((d-7) << 3) + MAP_FLASH;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 163
L0093:	iny
	lda     (sp),y
	tax
	dey
	lda     (sp),y
	sec
	sbc     #$07
	bcs     L009F
	dex
L009F:	jsr     shlax3
L022D:	sta     $2300
	txa
	clc
	adc     #$08
	sta     $2300+1
;
; FLASH_CMD1 = 0xAA;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 168
	lda     #$AA
	sta     $4AAA
;
; FLASH_CMD2 = 0x55;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 169
	lda     #$55
	sta     $5554
;
; FLASH_CMD1 = 0x80;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 170
	lda     #$80
	sta     $4AAA
;
; FLASH_CMD1 = 0xAA;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 171
	lda     #$AA
	sta     $4AAA
;
; FLASH_CMD2 = 0x55;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 172
	lda     #$55
	sta     $5554
;
; FLASH_CMD2 = 0x30;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 173
	lda     #$30
	sta     $5554
;
; TIMER = 200;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 176
	lda     #$C8
	sta     $2004
;
; tout  = 2000;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 177
	ldx     #$07
	lda     #$D0
	jsr     stax0sp
;
; while(!(FLASH_CMD1 & 0x80)) {
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 178
L00BD:	lda     $4AAA
	and     #$80
	bne     L00BE
;
; if(!TIMER) {
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 179
	lda     $2004
	bne     L00BD
;
; TIMER = 200;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 180
	lda     #$C8
	sta     $2004
;
; tout--;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 181
	ldy     #$01
	lda     (sp),y
	tax
	dey
	lda     (sp),y
	sec
	sbc     #$01
	bcs     L00CA
	dex
L00CA:	jsr     stax0sp
;
; if(!tout)
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 182
	ldy     #$01
	lda     (sp),y
	tax
	dey
	lda     (sp),y
	jsr     bnegax
	beq     L00BD
;
; return FALSE;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 183
	ldx     #$00
	txa
	jmp     incsp4
;
; return TRUE;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 186
L00BE:	ldx     #$00
	lda     #$01
;
; }
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 187
	jmp     incsp4
	.dbg	line

.endproc

; ---------------------------------------------------------------
; unsigned char __near__ flash_erase (void)
; ---------------------------------------------------------------

.segment	"CODE"

.proc	_flash_erase: near

.segment	"CODE"

;
; MAPPER_MAP1 = MAP_FLASH;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 203
	jsr     decsp2
	ldx     #$08
	lda     #$00
	sta     $2300
	stx     $2300+1
;
; FLASH_CMD1 = 0xAA;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 206
	lda     #$AA
	sta     $4AAA
;
; FLASH_CMD2 = 0x55;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 207
	lda     #$55
	sta     $5554
;
; FLASH_CMD1 = 0x80;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 208
	lda     #$80
	sta     $4AAA
;
; FLASH_CMD1 = 0xAA;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 209
	lda     #$AA
	sta     $4AAA
;
; FLASH_CMD2 = 0x55;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 210
	lda     #$55
	sta     $5554
;
; FLASH_CMD1 = 0x10;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 211
	lda     #$10
	sta     $4AAA
;
; TIMER = 200;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 215
	lda     #$C8
	sta     $2004
;
; tout  = 15000;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 216
	ldx     #$3A
	lda     #$98
	jsr     stax0sp
;
; while(!(FLASH_CMD1 & 0x80)) {
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 217
L00F0:	lda     $4AAA
	and     #$80
	bne     L00F1
;
; if(!TIMER) {
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 218
	lda     $2004
	bne     L00F0
;
; TIMER = 200;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 219
	lda     #$C8
	sta     $2004
;
; tout--;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 220
	ldy     #$01
	lda     (sp),y
	tax
	dey
	lda     (sp),y
	sec
	sbc     #$01
	bcs     L00FD
	dex
L00FD:	jsr     stax0sp
;
; if(!tout)
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 221
	ldy     #$01
	lda     (sp),y
	tax
	dey
	lda     (sp),y
	jsr     bnegax
	beq     L00F0
;
; return FALSE;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 222
	ldx     #$00
	txa
	jmp     incsp2
;
; return TRUE;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 225
L00F1:	ldx     #$00
	lda     #$01
;
; }
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 226
	jmp     incsp2
	.dbg	line

.endproc

; ---------------------------------------------------------------
; unsigned char __near__ flash_file (__near__ struct _FIL*, unsigned char)
; ---------------------------------------------------------------

.segment	"CODE"

.proc	_flash_file: near

.segment	"DATA"

L0136:
	.word	$FFFF

.segment	"BSS"

L0131:
	.res	2,$00
L0133:
	.res	8,$00
L0134:
	.res	1,$00
L0135:
	.res	2,$00
L0139:
	.res	64,$00
L013A:
	.res	1,$00
L013B:
	.res	1,$00
L013C:
	.res	1,$00
L013D:
	.res	4,$00
L013E:
	.res	2,$00
L013F:
	.res	2,$00
L0140:
	.res	2,$00
L0141:
	.res	1,$00
L0142:
	.res	1,$00
L0143:
	.res	2,$00

.segment	"CODE"

;
; errors = 0;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 284
	lda     #$00
	sta     L0142
;
; verify = v;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 285
	tay
	lda     (sp),y
	sta     L0141
;
; paddr = (BYTE *)&addr;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 286
	lda     #<(L013D)
	sta     L013E
	lda     #>(L013D)
	sta     L013E+1
;
; flash = (BYTE *)MAP1_ADDR;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 287
	ldx     #$40
	tya
	sta     L013F
	stx     L013F+1
;
; MAPPER_MAP1 = MAP_FLASH; // 0x800 x 8k = 16M (start of flash)
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 289
	ldx     #$08
	sta     $2300
	stx     $2300+1
;
; flash_get_id();
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 291
	jsr     _flash_get_id
;
; printf("Flash ID: %02X %02X.\n", flash_id[0], flash_id[1]);
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 292
	lda     #<(L0001+31)
	ldx     #>(L0001+31)
	jsr     pushax
	lda     _flash_id
	jsr     pusha0
	lda     _flash_id+1
	jsr     pusha0
	ldy     #$06
L0233:	jsr     _printf
;
; while(errors < 40)
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 294
L0156:	lda     L0142
	cmp     #$28
	jcs     L0157
;
; f_read(fp, buf, 1, &bytes_read);
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 298
L0159:	jsr     decsp8
	ldy     #$0A
	lda     (sp),y
	tax
	dey
	lda     (sp),y
	ldy     #$06
	sta     (sp),y
	iny
	txa
	sta     (sp),y
	lda     #<(L0133)
	ldy     #$04
	sta     (sp),y
	iny
	lda     #>(L0133)
	sta     (sp),y
	lda     #$01
	ldy     #$02
	sta     (sp),y
	iny
	lda     #$00
	sta     (sp),y
	lda     #<(L0131)
	ldy     #$00
	sta     (sp),y
	iny
	lda     #>(L0131)
	sta     (sp),y
	jsr     _f_read
;
; } while(bytes_read && (buf[0] != ':'));
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 299
	lda     L0131
	ora     L0131+1
	beq     L0232
	lda     L0133
	cmp     #$3A
	bne     L0159
;
; if(!bytes_read)
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 300
L0232:	lda     L0131
	ora     L0131+1
	bne     L0167
;
; return TRUE;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 301
	tax
	lda     #$01
	jmp     incsp3
;
; i=0;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 303
L0167:	lda     #$00
	sta     L013A
;
; chk = 0;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 304
	sta     L0134
;
; d = read_hexbyte(fp);
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 307
L016E:	ldy     #$02
	lda     (sp),y
	tax
	dey
	lda     (sp),y
	jsr     pushax
	jsr     _read_hexbyte
	ldx     #$00
	sta     L0135
	stx     L0135+1
;
; chk += d;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 308
	clc
	adc     L0134
	sta     L0134
;
; data[i++] = d;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 309
	lda     L013A
	pha
	clc
	adc     #$01
	sta     L013A
	pla
	clc
	adc     #<(L0139)
	sta     ptr1
	txa
	adc     #>(L0139)
	sta     ptr1+1
	lda     L0135
	ldy     #$00
	sta     (ptr1),y
;
; } while(i < (data[0] + 5));
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 311
	lda     L013A
	jsr     pusha0
	lda     L0139
	clc
	adc     #$05
	bcc     L017C
	ldx     #$01
L017C:	jsr     tosicmp
	bcc     L016E
;
; if(chk) {
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 314
	lda     L0134
	beq     L017D
;
; printf("Checksum error.\n");
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 315
	lda     #<(L0001+53)
	ldx     #>(L0001+53)
	jsr     pushax
	ldy     #$02
	jsr     _printf
;
; switch(data[3]) { // type field
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 317
L017D:	lda     L0139+3
;
; }
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 395
	beq     L019F
	cmp     #$01
	beq     L0186
	cmp     #$02
	beq     L018B
	cmp     #$04
	beq     L018F
	jmp     L0215
;
; printf("End record.\n");
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 319
L0186:	lda     #<(L0001+70)
	ldx     #>(L0001+70)
	jsr     pushax
	ldy     #$02
	jsr     _printf
;
; return TRUE;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 320
	ldx     #$00
	lda     #$01
	jmp     incsp3
;
; printf("Unsupported segmented address.\n");
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 323
L018B:	lda     #<(L0001+83)
	ldx     #>(L0001+83)
	jsr     pushax
	ldy     #$02
;
; break; // ignored
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 324
	jmp     L0233
;
; if(data[0] == 2) {
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 327
L018F:	lda     L0139
	cmp     #$02
	bne     L0190
;
; paddr[3] = data[4];
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 328
	lda     L013E
	sta     ptr1
	lda     L013E+1
	sta     ptr1+1
	lda     L0139+4
	ldy     #$03
	sta     (ptr1),y
;
; paddr[2] = data[5];
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 329
	lda     L013E
	sta     ptr1
	lda     L013E+1
	sta     ptr1+1
	lda     L0139+5
	dey
	sta     (ptr1),y
;
; } else {
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 330
	jmp     L0156
;
; printf("Extended address with wrong length.\n");
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 331
L0190:	lda     #<(L0001+115)
	ldx     #>(L0001+115)
	jsr     pushax
	ldy     #$02
;
; break;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 333
	jmp     L0233
;
; paddr[0] = data[2];
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 336
L019F:	lda     L013E
	sta     ptr1
	lda     L013E+1
	sta     ptr1+1
	lda     L0139+2
	ldy     #$00
	sta     (ptr1),y
;
; paddr[1] = data[1];
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 337
	lda     L013E
	sta     ptr1
	lda     L013E+1
	sta     ptr1+1
	lda     L0139+1
	iny
	sta     (ptr1),y
;
; d = flash_addr2block(addr);
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 339
	lda     L013D+3
	sta     sreg+1
	lda     L013D+2
	sta     sreg
	ldx     L013D+1
	lda     L013D
	jsr     pusheax
	jsr     _flash_addr2block
	sta     L0135
	stx     L0135+1
;
; if(!verify) {
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 341
	lda     L0141
	bne     L01AD
;
; if  (last_erased != d) {
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 343
	lda     L0135
	ldx     L0135+1
	cpx     L0136+1
	bne     L0235
	cmp     L0136
	beq     L01AD
;
; if(!flash_erase_block(d))
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 344
L0235:	lda     L0135
	ldx     L0135+1
	jsr     pushax
	jsr     _flash_erase_block
	tax
;
; return FALSE;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 345
	jeq     L0234
;
; last_erased = d;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 346
	lda     L0135
	sta     L0136
	lda     L0135+1
	sta     L0136+1
;
; d = (WORD)(addr >> 13); // calculate map
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 350
L01AD:	lda     L013D+3
	sta     sreg+1
	lda     L013D+2
	sta     sreg
	lda     L013D+1
	ldx     sreg
	ldy     sreg+1
	sty     sreg
	cpy     #$80
	ldy     #$00
	bcc     L01B8
	dey
L01B8:	sty     sreg+1
	jsr     asreax4
	jsr     asreax1
	sta     L0135
	stx     L0135+1
;
; MAPPER_MAP1 = (d + MAP_FLASH);
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 351
	sta     $2300
	txa
	clc
	adc     #$08
	sta     $2300+1
;
; mapped = (SHORT)(addr & 0x1FFF);
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 352
	lda     L013D+3
	sta     sreg+1
	lda     L013D+2
	sta     sreg
	ldx     L013D+1
	lda     L013D
	jsr     pusheax
	ldx     #$1F
	lda     #$FF
	jsr     tosand0ax
	sta     L0140
	stx     L0140+1
;
; for(i=0;i<data[0];i++) {
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 355
	lda     #$00
L0237:	sta     L013A
	ldx     #$00
	lda     L013A
	cmp     L0139
	jcs     L0156
;
; b = data[4+i];
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 357
	lda     L013A
	clc
	adc     #$04
	bcc     L01CC
	inx
L01CC:	sta     ptr1
	txa
	clc
	adc     #>(L0139)
	sta     ptr1+1
	ldy     #<(L0139)
	lda     (ptr1),y
	sta     L013B
;
; if(!verify) {
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 358
	lda     L0141
	jne     L01CD
;
; FLASH_CMD1 = 0xAA;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 359
	lda     #$AA
	sta     $4AAA
;
; FLASH_CMD2 = 0x55;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 360
	lda     #$55
	sta     $5554
;
; FLASH_CMD1 = 0xA0;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 361
	lda     #$A0
	sta     $4AAA
;
; flash[mapped] = b;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 362
	lda     L013F
	clc
	adc     L0140
	sta     ptr1
	lda     L013F+1
	adc     L0140+1
	sta     ptr1+1
	lda     L013B
	ldy     #$00
	sta     (ptr1),y
;
; b &= 0x80;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 363
	lda     L013B
	and     #$80
	sta     L013B
;
; timeout = 0x7FFF;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 364
	ldx     #$7F
	lda     #$FF
	sta     L0143
	stx     L0143+1
;
; while(--timeout) {
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 365
L01E2:	lda     L0143
	sec
	sbc     #$01
	sta     L0143
	bcs     L01E5
	dec     L0143+1
	ora     L0143+1
L01E5:	beq     L01E3
;
; r = flash[mapped];
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 366
	lda     L013F
	clc
	adc     L0140
	sta     ptr1
	lda     L013F+1
	adc     L0140+1
	sta     ptr1+1
	lda     (ptr1),y
	sta     L013C
;
; if((r & 0x80)==b)
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 367
	and     #$80
	cmp     L013B
;
; break;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 368
	beq     L01E3
;
; if((r & 0x20)==0x20) {
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 369
	lda     L013C
	and     #$20
	cmp     #$20
	bne     L01E2
;
; r = flash[mapped];
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 370
	lda     L013F
	clc
	adc     L0140
	sta     ptr1
	lda     L013F+1
	adc     L0140+1
	sta     ptr1+1
	lda     (ptr1),y
	sta     L013C
;
; if((r & 0x80)==b)
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 371
	and     #$80
	cmp     L013B
;
; break;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 372
	beq     L01E3
;
; printf("error.");
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 373
	lda     #<(L0001+152)
	ldx     #>(L0001+152)
	jsr     pushax
	ldy     #$02
	jsr     _printf
;
; FLASH_CMD1 = 0xF0;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 374
	lda     #$F0
	sta     $4AAA
;
; return FALSE;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 375
	jmp     L0236
;
; if(!timeout) {
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 378
L01E3:	lda     L0143
	ora     L0143+1
	jne     L0208
;
; printf("timeout. MAPPER=%04x, mapped=%04x, data=%02x %02x\n", MAPPER_MAP1, mapped, b, r);
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 379
	lda     #<(L0001+159)
	ldx     #>(L0001+159)
	jsr     pushax
	lda     $2300
	ldx     $2300+1
	jsr     pushax
	lda     L0140
	ldx     L0140+1
	jsr     pushax
	lda     L013B
	jsr     pusha0
	lda     L013C
	jsr     pusha0
	ldy     #$0A
;
; return FALSE;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 380
	jmp     L0238
;
; if(flash[mapped] != b) {
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 383
L01CD:	lda     L013F
	clc
	adc     L0140
	sta     ptr1
	lda     L013F+1
	adc     L0140+1
	sta     ptr1+1
	ldy     #$00
	lda     (ptr1),y
	cmp     L013B
	beq     L0208
;
; printf("Verify error at block %d, offset %04x. Read %02x, should be %02x\n",
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 384
	lda     #<(L0001+210)
	ldx     #>(L0001+210)
	jsr     pushax
;
; d, mapped, flash[mapped], b);
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 385
	lda     L0135
	ldx     L0135+1
	jsr     pushax
	lda     L0140
	ldx     L0140+1
	jsr     pushax
	lda     L013F
	clc
	adc     L0140
	sta     ptr1
	lda     L013F+1
	adc     L0140+1
	sta     ptr1+1
	ldx     #$00
	lda     (ptr1,x)
	jsr     pusha0
	lda     L013B
	jsr     pusha0
	ldy     #$0A
	jsr     _printf
;
; errors++;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 386
	lda     L0142
	clc
	adc     #$01
	sta     L0142
;
; mapped++;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 389
L0208:	lda     L0140
	ldx     L0140+1
	clc
	adc     #$01
	bcc     L0214
	inx
L0214:	sta     L0140
	stx     L0140+1
;
; for(i=0;i<data[0];i++) {
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 355
	lda     L013A
	clc
	adc     #$01
	jmp     L0237
;
; printf("Unsupported record type %2x.\n", data[3]);
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 394
L0215:	lda     #<(L0001+276)
	ldx     #>(L0001+276)
	jsr     pushax
	lda     L0139+3
	jsr     pusha0
	ldy     #$04
;
; }
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 395
	jmp     L0233
;
; printf("Too many errors.. exiting.\n");
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 397
L0157:	lda     #<(L0001+306)
	ldx     #>(L0001+306)
	jsr     pushax
	ldy     #$02
L0238:	jsr     _printf
;
; return FALSE;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 398
L0236:	ldx     #$00
L0234:	txa
;
; }
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 399
	jmp     incsp3
	.dbg	line

.endproc

; ---------------------------------------------------------------
; unsigned short __near__ flash_get_cfg_page (void)
; ---------------------------------------------------------------

.segment	"CODE"

.proc	_flash_get_cfg_page: near

.segment	"CODE"

;
; return MAP_FLASH; // Bottom page!
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 39
	ldx     #$08
	lda     #$00
;
; }
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 40
	rts
	.dbg	line

.endproc

; ---------------------------------------------------------------
; unsigned short __near__ flash_get_cfg_sector (void)
; ---------------------------------------------------------------

.segment	"CODE"

.proc	_flash_get_cfg_sector: near

.segment	"CODE"

;
; return 0;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 44
	ldx     #$00
	txa
;
; }
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 45
	rts
	.dbg	line

.endproc

; ---------------------------------------------------------------
; unsigned short __near__ flash_addr2block (long)
; ---------------------------------------------------------------

.segment	"CODE"

.proc	_flash_addr2block: near

.segment	"CODE"

;
; WORD d = (addr >> 13); // convert to 8K block index first
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 120
	jsr     ldeax0sp
	txa
	ldx     sreg
	ldy     sreg+1
	sty     sreg
	cpy     #$80
	ldy     #$00
	bcc     L008A
	dey
L008A:	sty     sreg+1
	jsr     asreax4
	jsr     asreax1
	jsr     pushax
;
; if(d < 8)
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 128
	ldy     #$01
	lda     (sp),y
	tax
	dey
	lda     (sp),y
	cpx     #$00
	bne     L008D
	cmp     #$08
L008D:	bcs     L008B
;
; return d;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 129
	iny
	lda     (sp),y
	tax
	dey
	lda     (sp),y
	jmp     incsp6
;
; return (d >> 3) + 7;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 130
L008B:	iny
	lda     (sp),y
	tax
	dey
	lda     (sp),y
	jsr     shrax3
	clc
	adc     #$07
	jcc     incsp6
	inx
;
; }
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 131
	jmp     incsp6
	.dbg	line

.endproc

; ---------------------------------------------------------------
; void __near__ flash_protect (void)
; ---------------------------------------------------------------

.segment	"CODE"

.proc	_flash_protect: near

.segment	"CODE"

;
; for(d = MAP_FLASH + 0xE8; d < MAP_FLASH + 0x100; d+= 8) {
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 241
	jsr     decsp2
	ldx     #$08
	lda     #$E8
	jsr     stax0sp
L0103:	ldy     #$01
	lda     (sp),y
	tax
	cpx     #$09
	bcs     L0104
;
; MAPPER_MAP1 = d;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 242
	lda     (sp),y
	sta     $2300+1
	dey
	lda     (sp),y
	sta     $2300
;
; FLASH_CMD1 = 0xAA;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 245
	lda     #$AA
	sta     $4AAA
;
; FLASH_CMD2 = 0x55;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 246
	lda     #$55
	sta     $5554
;
; FLASH_CMD1 = 0x80;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 247
	lda     #$80
	sta     $4AAA
;
; FLASH_CMD1 = 0xAA;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 248
	lda     #$AA
	sta     $4AAA
;
; FLASH_CMD2 = 0x55;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 249
	lda     #$55
	sta     $5554
;
; FLASH_CMD1 = 0x60;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 250
	lda     #$60
	sta     $4AAA
;
; TIMER = 40;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 252
	lda     #$28
	sta     $2004
;
; while(TIMER)
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 253
L012A:	lda     $2004
;
; ;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 254
	bne     L012A
;
; for(d = MAP_FLASH + 0xE8; d < MAP_FLASH + 0x100; d+= 8) {
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 241
	tax
	lda     #$08
	jsr     addeq0sp
	jmp     L0103
;
; printf("Boot area of Flash protected.\n");
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 256
L0104:	lda     #<(L0001)
	ldx     #>(L0001)
	jsr     pushax
	ldy     #$02
	jsr     _printf
;
; }
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 257
	jmp     incsp2
	.dbg	line

.endproc

; ---------------------------------------------------------------
; void __near__ flash_get_id (void)
; ---------------------------------------------------------------

.segment	"CODE"

.proc	_flash_get_id: near

.segment	"CODE"

;
; MAPPER_MAP1 = MAP_FLASH;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 92
	ldx     #$08
	lda     #$00
	sta     $2300
	stx     $2300+1
;
; FLASH_CMD1 = 0xAA;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 94
	lda     #$AA
	sta     $4AAA
;
; FLASH_CMD2 = 0x55;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 95
	lda     #$55
	sta     $5554
;
; FLASH_CMD1 = 0x90;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 96
	lda     #$90
	sta     $4AAA
;
; flash_id[0] = FLASH_MANID;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 97
	lda     $4000
	sta     _flash_id
;
; flash_id[1] = FLASH_DEVID;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 98
	lda     $4002
	sta     _flash_id+1
;
; FLASH_CMD1 = 0xAA;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 99
	lda     #$AA
	sta     $4AAA
;
; FLASH_CMD2 = 0x55;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 100
	lda     #$55
	sta     $5554
;
; FLASH_CMD1 = 0xF0;
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 101
	lda     #$F0
	sta     $4AAA
;
; }
;
	.dbg	line, "../../../storage/flash/src/atmel_flash.c", 102
	rts
	.dbg	line

.endproc

