Skip to content

Commit

Permalink
i_hate_c
Browse files Browse the repository at this point in the history
  • Loading branch information
amukh1 committed Mar 6, 2024
1 parent 62e92cd commit 4a30e30
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 31 deletions.
Binary file modified crt0.o
Binary file not shown.
65 changes: 40 additions & 25 deletions crt0.s
Original file line number Diff line number Diff line change
@@ -1,38 +1,53 @@
.export _init, _exit
.exportzp sp
.import _main
; ---------------------------------------------------------------------------
; crt0.s
; ---------------------------------------------------------------------------
;
; Startup code for cc65 (Single Board Computer version)

.export __STARTUP__: absolute = 1
.import __RAM_START__, __RAM_SIZE__
.export _init, _exit
.import _main

.import copydata, zerobss, initlib, donelib
.export __STARTUP__ : absolute = 1 ; Mark as startup
.import __RAM_START__, __RAM_SIZE__ ; Linker generated

; .include "zeropage.inc"
.import copydata, zerobss, initlib, donelib

; reserve byte or sp
.segment "ZEROPAGE"
sp: .res 1
.include "zeropage.inc"

; ---------------------------------------------------------------------------
; Place the startup code in a special segment

.segment "STARTUP"

.segment "STARTUP"
; ---------------------------------------------------------------------------
; A little light 6502 housekeeping

_init: ldx #$ff
txs
cld
_init: LDX #$FF ; Initialize stack pointer to $01FF
TXS
CLD ; Clear decimal mode

lda #<(__RAM_START__ + __RAM_SIZE__)
STA sp
lda #>(__RAM_START__ + __RAM_SIZE__)
STA sp+1
; ---------------------------------------------------------------------------
; Set cc65 argument stack pointer

jsr zerobss
jsr copydata
jsr initlib
LDA #<(__RAM_START__ + __RAM_SIZE__)
STA sp
LDA #>(__RAM_START__ + __RAM_SIZE__)
STA sp+1

jsr _main
; ---------------------------------------------------------------------------
; Initialize memory storage

_exit:
jsr donelib
brk
JSR zerobss ; Clear BSS segment
JSR copydata ; Initialize DATA segment
JSR initlib ; Run constructors

; ---------------------------------------------------------------------------
; Call main()

JSR _main

; ---------------------------------------------------------------------------
; Back from main (this is also the _exit entry): force a software break

_exit: JSR donelib ; Run destructors
BRK
8 changes: 4 additions & 4 deletions macro.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cc65 test.c -o test.s
cc65 -t none test.c -o test.s
ca65 test.s -o test.o
# ca65 startup.s -o startup.o
ca65 zeropage.s -o zeropage.o
ca65 crt0.s -o crt0.o
ld65 crt0.o zeropage.o test.o -o test -C test.cfg
# ca65 zeropage.s -o zeropage.o
# ca65 crt0.s -o crt0.o
ld65 zeropage.o test.o sbc.lib -o test -C test.cfg
4 changes: 3 additions & 1 deletion ref.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ https://www.qmtpro.com/~nes/misc/nestest.txt
https://www.pagetable.com/?p=410
https://pegvin.github.io/6502/index.html
https://www.westerndesigncenter.com/wdc/documentation/w65c02s.pdf
vasm6502_std -Fbin -Felf -mid=PC386 test.asm -o out
vasm6502_std -Fbin -Felf -mid=PC386 test.asm -o out
https://cc65.github.io/doc/customizing.html#s1
/usr/share/cc65
Binary file added sbc.lib
Binary file not shown.
13 changes: 12 additions & 1 deletion test.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ MEMORY {

SEGMENTS {
ZEROPAGE: load = ZP, type = zp;
DATA: load = RAM, type = rw;
DATA: load = RAM, type = rw, run = RAM;
CODE: load = ROM, type = ro;
HEAP: load = RAM, type = rw optional = yes;
INIT: load = ROM, type = ro optional = yes;
Expand All @@ -19,6 +19,17 @@ SEGMENTS {
# ONCE: load = ROM, type = ro, optional = yes;
}

FEATURES {
CONDES: segment = STARTUP,
type = constructor,
label = __CONSTRUCTOR_TABLE__,
count = __CONSTRUCTOR_COUNT__;
CONDES: segment = STARTUP,
type = destructor,
label = __DESTRUCTOR_TABLE__,
count = __DESTRUCTOR_COUNT__;
}

SYMBOLS {
__STACKSIZE__: type = weak, value = $0200;
# __RAM_START__: type = weak, value = $0200;
Expand Down
Binary file modified test.o
Binary file not shown.
Binary file modified zeropage.o
Binary file not shown.

0 comments on commit 4a30e30

Please sign in to comment.