This commit was generated by cvs2svn to compensate for changes in r2,

which included commits to RCS files with non-trunk default branches.


git-svn-id: svn://svn.cc65.org/cc65/trunk@3 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
uz
2000-05-28 13:40:48 +00:00
parent 579491e8a4
commit 53dd513176
847 changed files with 91345 additions and 0 deletions

28
libsrc/c64/Makefile Normal file
View File

@@ -0,0 +1,28 @@
#
# makefile for CC65 runtime library
#
.SUFFIXES: .o .s .c
%.o: %.c
@echo $<
@$(CC) $(CFLAGS) $<
@$(AS) -o $@ $(AFLAGS) $(*).s
%.o: %.s
@echo $<
@$(AS) -g -o $@ $(AFLAGS) $<
C_OBJS =
S_OBJS = crt0.o read.o write.o kbhit.o conio.o clrscr.o mouse.o\
cputc.o cgetc.o color.o readjoy.o break.o rs232.o
all: $(C_OBJS) $(S_OBJS)
clean:
@rm -f $(C_OBJS:.c=.s)
@rm -f $(C_OBJS)
@rm -f $(S_OBJS)
@rm -f crt0.o

108
libsrc/c64/break.s Normal file
View File

@@ -0,0 +1,108 @@
;
; Ullrich von Bassewitz, 27.09.1998
;
; void set_brk (unsigned Addr);
; void reset_brk (void);
;
.export _set_brk, _reset_brk
.export _brk_a, _brk_x, _brk_y, _brk_sr, _brk_pc
.import _atexit
.include "c64.inc"
.bss
_brk_a: .res 1
_brk_x: .res 1
_brk_y: .res 1
_brk_sr: .res 1
_brk_pc: .res 2
oldvec: .res 2 ; Old vector
.data
uservec: jmp $FFFF ; Patched at runtime
.code
; Set the break vector
.proc _set_brk
sta uservec+1
stx uservec+2 ; Set the user vector
lda oldvec
ora oldvec+1 ; Did we save the vector already?
bne L1 ; Jump if we installed the handler already
lda BRKVec
sta oldvec
lda BRKVec+1
sta oldvec+1 ; Save the old vector
lda #<_reset_brk
ldx #>_reset_brk
jsr _atexit ; Install an exit handler
L1: lda #<brk_handler ; Set the break vector to our routine
sta BRKVec
lda #>brk_handler
sta BRKVec+1
rts
.endproc
; Reset the break vector
.proc _reset_brk
lda oldvec
sta BRKVec
lda oldvec+1
sta BRKVec+1
rts
.endproc
; Break handler, called if a break occurs
.proc brk_handler
pla
sta _brk_y
pla
sta _brk_x
pla
sta _brk_a
pla
and #$EF ; Clear break bit
sta _brk_sr
pla ; PC low
sec
sbc #2 ; Point to start of brk
sta _brk_pc
pla ; PC high
sbc #0
sta _brk_pc+1
jsr uservec ; Call the user's routine
lda _brk_pc+1
pha
lda _brk_pc
pha
lda _brk_sr
pha
ldx _brk_x
ldy _brk_y
lda _brk_a
rti ; Jump back...
.endproc

194
libsrc/c64/c64.inc Normal file
View File

@@ -0,0 +1,194 @@
;
; C64 generic definitions. Stolen from Elite128
;
; ---------------------------------------------------------------------------
; Zero page, Commodore stuff
ST = $90 ; IEC status byte
FNAM_LEN = $B7 ; Length of filename
SECADR = $B9 ; Secondary address
DEVNUM = $BA ; Device number
KEY_COUNT = $C6 ; Number of keys in input buffer
CURS_FLAG = $CC ; 1 = cursor off
CURS_BLINK = $CD ; Blink counter
CURS_CHAR = $CE ; Character under the cursor
CURS_COLOR = $287 ; Color under the cursor
CURS_STATE = $CF ; Cursor blink state
SCREEN_PTR = $D1 ; Pointer to current char in text screen
CURS_X = $D3 ; Cursor column
CURS_Y = $D6 ; Cursor row
CRAM_PTR = $F3 ; Pointer to current char in color RAM
CHARCOLOR = $286
PALFLAG = $2A6 ; $01 = PAL, $00 = NTSC
; ---------------------------------------------------------------------------
; Kernal routines
; Direct entries
CLRSCR = $E544
KBDREAD = $E5B4
NAMED_OPEN = $F3D5
NAMED_CLOSE = $F642
PLOTCHAR = $EA1C ; Char in A, color in X
; ---------------------------------------------------------------------------
; Vector and other locations
IRQVec = $0314
BRKVec = $0316
NMIVec = $0318
; ---------------------------------------------------------------------------
; I/O: VIC
VIC = $D000
VIC_SPR0_X = $D000
VIC_SPR0_Y = $D001
VIC_SPR1_X = $D002
VIC_SPR1_Y = $D003
VIC_SPR2_X = $D004
VIC_SPR2_Y = $D005
VIC_SPR3_X = $D006
VIC_SPR3_Y = $D007
VIC_SPR4_X = $D008
VIC_SPR4_Y = $D009
VIC_SPR5_X = $D00A
VIC_SPR5_Y = $D00B
VIC_SPR6_X = $D00C
VIC_SPR6_Y = $D00D
VIC_SPR7_X = $D00E
VIC_SPR7_Y = $D00F
VIC_SPR_HI_X = $D010
VIC_SPR_ENA = $D015
VIC_SPR_EXP_X = $D017
VIC_SPR_EXP_Y = $D01D
VIC_SPR_MCOLOR = $D01C
VIC_SPR_BG_PRIO = $D01B
VIC_SPR_MCOLOR0 = $D025
VIC_SPR_MCOLOR1 = $D026
VIC_SPR0_COLOR = $D027
VIC_SPR1_COLOR = $D028
VIC_SPR2_COLOR = $D029
VIC_SPR3_COLOR = $D02A
VIC_SPR4_COLOR = $D02B
VIC_SPR5_COLOR = $D02C
VIC_SPR6_COLOR = $D02D
VIC_SPR7_COLOR = $D02E
VIC_CTRL1 = $D011
VIC_CTRL2 = $D016
VIC_HLINE = $D012
VIC_VIDEO_ADR = $D018
VIC_IRR = $D019 ; Interrupt request register
VIC_IMR = $D01A ; Interrupt mask register
VIC_BORDERCOLOR = $D020
VIC_BG_COLOR0 = $D021
VIC_BG_COLOR1 = $D022
VIC_BG_COLOR2 = $D023
VIC_BG_COLOR3 = $D024
; 128 stuff:
VIC_KBD_128 = $D02F ; Extended kbd bits (visible in 64 mode)
VIC_CLK_128 = $D030 ; Clock rate register (visible in 64 mode)
; ---------------------------------------------------------------------------
; I/O: SID
SID = $D400
SID_S1Lo = $D400
SID_S1Hi = $D401
SID_PB1Lo = $D402
SID_PB1Hi = $D403
SID_Ctl1 = $D404
SID_AD1 = $D405
SID_SUR1 = $D406
SID_S2Lo = $D407
SID_S2Hi = $D408
SID_PB2Lo = $D409
SID_PB2Hi = $D40A
SID_Ctl2 = $D40B
SID_AD2 = $D40C
SID_SUR2 = $D40D
SID_S3Lo = $D40E
SID_S3Hi = $D40F
SID_PB3Lo = $D410
SID_PB3Hi = $D411
SID_Ctl3 = $D412
SID_AD3 = $D413
SID_SUR3 = $D414
SID_FltLo = $D415
SID_FltHi = $D416
SID_FltCtl = $D417
SID_Amp = $D418
SID_ADConv1 = $D419
SID_ADConv2 = $D41A
SID_Noise = $D41B
SID_Read3 = $D41C
; ---------------------------------------------------------------------------
; I/O: VDC (128 only)
VDC_INDEX = $D600
VDC_DATA = $D601
; ---------------------------------------------------------------------------
; I/O: CIAs
CIA1 = $DC00
CIA1_PRA = $DC00
CIA1_PRB = $DC01
CIA1_DDRA = $DC02
CIA1_DDRB = $DC03
CIA1_ICR = $DC0D
CIA1_CRA = $DC0E
CIA1_CRB = $DC0F
CIA2 = $DD00
CIA2_PRA = $DD00
CIA2_PRB = $DD01
CIA2_DDRA = $DD02
CIA2_DDRB = $DD03
CIA2_ICR = $DD0D
CIA2_CRA = $DD0E
CIA2_CRB = $DD0F
; ---------------------------------------------------------------------------
; Super CPU
SCPU_VIC_Bank1 = $D075
SCPU_Slow = $D07A
SCPU_Fast = $D07B
SCPU_EnableRegs = $D07E
SCPU_DisableRegs= $D07F
SCPU_Detect = $D0BC
; ---------------------------------------------------------------------------
; Processor Port at $01
LORAM = $01 ; Enable the basic rom
HIRAM = $02 ; Enable the kernal rom
IOEN = $04 ; Enable I/O
CASSDATA = $08 ; Cassette data
CASSPLAY = $10 ; Cassette: Play
CASSMOT = $20 ; Cassette motor on
TP_FAST = $80 ; Switch Rossmoeller TurboProcess to fast mode
RAMONLY = $F8 ; (~(LORAM | HIRAM | IOEN)) & $FF

61
libsrc/c64/cgetc.s Normal file
View File

@@ -0,0 +1,61 @@
;
; Ullrich von Bassewitz, 06.08.1998
;
; char cgetc (void);
;
.export _cgetc
.import cursor
.include "c64.inc"
_cgetc: lda KEY_COUNT ; Get number of characters
bne L3 ; Jump if there are already chars waiting
; Switch on the cursor if needed
lda CURS_FLAG
pha
lda cursor
jsr setcursor
L1: lda KEY_COUNT
beq L1
ldx #0
pla
bne L2
inx
L2: txa
jsr setcursor
L3: jsr KBDREAD ; Read char and return in A
ldx #0
rts
; Switch the cursor on or off
.proc setcursor
tax ; On or off?
bne seton ; Go set it on
lda CURS_FLAG ; Is the cursor currently off?
bne crs9 ; Jump if yes
lda #1
sta CURS_FLAG ; Mark it as off
lda CURS_STATE ; Cursor currently displayed?
beq crs8 ; Jump if no
ldy CURS_X ; Get the character column
lda (SCREEN_PTR),y ; Get character
eor #$80
sta (SCREEN_PTR),y ; Store character back
lda CURS_COLOR
sta (CRAM_PTR),y ; Store color back
crs8: lda #0
sta CURS_STATE ; Cursor not displayed
crs9: rts
seton: lda #0
sta CURS_FLAG
rts
.endproc

14
libsrc/c64/clrscr.s Normal file
View File

@@ -0,0 +1,14 @@
;
; Ullrich von Bassewitz, 06.08.1998
;
; void clrscr (void);
;
.export _clrscr
.include "c64.inc"
_clrscr = CLRSCR

33
libsrc/c64/color.s Normal file
View File

@@ -0,0 +1,33 @@
;
; Ullrich von Bassewitz, 06.08.1998
;
; unsigned char __fastcall__ textcolor (unsigned char color);
; unsigned char __fastcall__ bgcolor (unsigned char color);
; unsigned char __fastcall__ bordercolor (unsigned char color);
;
.export _textcolor, _bgcolor, _bordercolor
.include "c64.inc"
_textcolor:
ldx CHARCOLOR ; get old value
sta CHARCOLOR ; set new value
txa
rts
_bgcolor:
ldx VIC_BG_COLOR0 ; get old value
sta VIC_BG_COLOR0 ; set new value
txa
rts
_bordercolor:
ldx VIC_BG_COLOR0 ; get old value
sta VIC_BORDERCOLOR ; set new value
txa
rts

23
libsrc/c64/conio.s Normal file
View File

@@ -0,0 +1,23 @@
;
; Ullrich von Bassewitz, 06.08.1998
;
; Low level stuff for screen output/console input
;
.export initconio
.exportzp CURS_X, CURS_Y
.import xsize, ysize
.include "../cbm/cbm.inc"
.include "c64.inc"
.code
initconio:
jsr SCREEN
stx xsize
sty ysize
rts

105
libsrc/c64/cputc.s Normal file
View File

@@ -0,0 +1,105 @@
;
; Ullrich von Bassewitz, 06.08.1998
;
; void cputcxy (unsigned char x, unsigned char y, char c);
; void cputc (char c);
;
.export _cputcxy, _cputc, cputdirect, putchar
.export advance, newline, plot
.import popa, _gotoxy
.import xsize, revers
.include "c64.inc"
.include "../cbm/cbm.inc"
_cputcxy:
pha ; Save C
jsr popa ; Get Y
jsr _gotoxy ; Set cursor, drop x
pla ; Restore C
; Plot a character - also used as internal function
_cputc: cmp #$0D ; CR?
bne L1
lda #0
sta CURS_X
beq plot ; Recalculate pointers
L1: cmp #$0A ; LF?
bne L2
ldy CURS_Y
iny
bne newline ; Recalculate pointers
; Printable char of some sort
L2: cmp #' '
bcc cputdirect ; Other control char
tay
bmi L10
cmp #$60
bcc L3
and #$DF
bne cputdirect ; Branch always
L3: and #$3F
cputdirect:
jsr putchar ; Write the character to the screen
; Advance cursor position
advance:
iny
cpy xsize
bne L9
ldy #0 ; new line
newline:
clc
lda xsize
adc SCREEN_PTR
sta SCREEN_PTR
bcc L4
inc SCREEN_PTR+1
L4: clc
lda xsize
adc CRAM_PTR
sta CRAM_PTR
bcc L5
inc CRAM_PTR+1
L5: inc CURS_Y
L9: sty CURS_X
rts
; Handle character if high bit set
L10: and #$7F
cmp #$7E ; PI?
bne L11
lda #$5E ; Load screen code for PI
bne cputdirect
L11: ora #$40
bne cputdirect
; Set cursor position, calculate RAM pointers
plot: ldy CURS_X
ldx CURS_Y
clc
jmp PLOT ; Set the new cursor
; Write one character to the screen without doing anything else, return X
; position in Y
putchar:
ora revers ; Set revers bit
ldy CURS_X
sta (SCREEN_PTR),y ; Set char
lda CHARCOLOR
sta (CRAM_PTR),y ; Set color
rts

132
libsrc/c64/crt0.s Normal file
View File

@@ -0,0 +1,132 @@
;
; Startup code for cc65 (C64 version)
;
; This must be the *first* file on the linker command line
;
.export _exit
.import __hinit, initconio, zerobss, push0, doatexit
.import _main
.include "c64.inc"
.include "../cbm/cbm.inc"
; ------------------------------------------------------------------------
; Define and export the ZP variables for the C64 runtime
.exportzp sp, sreg, regsave
.exportzp ptr1, ptr2, ptr3, ptr4
.exportzp tmp1, tmp2, tmp3, tmp4
.exportzp regbank, zpspace
sp = $02 ; stack pointer
sreg = $04 ; secondary register/high 16 bit for longs
regsave = $06 ; slot to save/restore (E)AX into
ptr1 = $0A ;
ptr2 = $0C
ptr3 = $0E
ptr4 = $10
tmp1 = $12
tmp2 = $13
tmp3 = $14
tmp4 = $15
regbank = $16 ; 6 byte register bank
zpspace = $1A ; Zero page space allocated
; ------------------------------------------------------------------------
; BASIC header with a SYS call
.org $7FF
.word Head ; Load address
Head: .word @Next
.word 1000 ; Line number
.byte $9E,"2061" ; SYS 2061
.byte $00 ; End of BASIC line
@Next: .word 0 ; BASIC end marker
.reloc
; ------------------------------------------------------------------------
; Actual code
ldy #zpspace-1
L1: lda sp,y
sta zpsave,y ; Save the zero page locations we need
dey
bpl L1
; Close open files
jsr CLRCH
; Switch to second charset
lda #14
jsr BSOUT
; Clear the BSS data
jsr zerobss
; Save system stuff and setup the stack
tsx
stx spsave ; Save the system stack ptr
lda $01
sta mmusave ; Save the memory configuration
lda $01
and #$F8
ora #$06 ; Enable kernal+I/O, disable basic
sta $01
lda #<$D000
sta sp
lda #>$D000
sta sp+1 ; Set argument stack ptr
; Initialize the heap
jsr __hinit
; Initialize conio stuff
jsr initconio
; Pass an empty command line
jsr push0 ; argc
jsr push0 ; argv
ldy #4 ; Argument size
jsr _main ; call the users code
; fall thru to exit...
_exit: jsr doatexit ; call exit functions
ldx spsave
txs ; Restore stack pointer
lda mmusave
sta $01 ; Restore memory configuration
; Copy back the zero page stuff
ldy #zpspace-1
L2: lda zpsave,y
sta sp,y
dey
bpl L2
; Reset changed vectors, back to basic
jmp RESTOR
.data
zpsave: .res zpspace
.bss
spsave: .res 1
mmusave:.res 1

20
libsrc/c64/kbhit.s Normal file
View File

@@ -0,0 +1,20 @@
;
; Ullrich von Bassewitz, 06.08.1998
;
; int kbhit (void);
;
.export _kbhit
.import return0, return1
.include "c64.inc"
_kbhit:
lda KEY_COUNT ; Get number of characters
bne L1
jmp return0
L1: jmp return1

384
libsrc/c64/mouse.s Normal file
View File

@@ -0,0 +1,384 @@
;
; Ullrich von Bassewitz, 24.04.1999
;
; Routines for the 1351 proportional mouse. Parts of the code are from
; the Commodore 1351 mouse users guide.
;
.export _mouse_init, _mouse_done
.export _mouse_hide, _mouse_show
.export _mouse_box, _mouse_info
.export _mouse_move
.import popa, popsreg, addysp1
.importzp sp, sreg
.include "c64.inc"
.code
; --------------------------------------------------------------------------
;
; void __fastcall__ mouse_init (unsigned char port, unsigned char sprite);
;
_mouse_init:
tax ; Save sprite number
jsr popa ; Get the port number
ldy OldIRQ+1 ; Already initialized?
bne Done ; Jump if yes
stx MouseSprite ; Remember the sprite number
sta MousePort ; Remember the port number
; Initialize variables
ldx #0
stx XPos
stx XPos+1
stx YPos
stx YPos+1
stx OldPotX
stx OldPotY
stx XMin
stx XMin+1
stx YMin
stx YMin+1
stx YMax+1
inx ; X = 1
stx Visible ; Mouse *not* visible
stx XMax+1 ; >320
ldx #<320
stx XMax
ldx #200
stx YMax
; Remember the old IRQ vector
lda IRQVec
sta OldIRQ
lda IRQVec+1
sta OldIRQ+1
; Set our own IRQ vector
lda #<MouseIRQ
ldx #>MouseIRQ
bne SetIRQ
; --------------------------------------------------------------------------
;
; void mouse_done (void);
;
_mouse_done:
lda OldIRQ ; Initialized?
ldx OldIRQ+1
beq Done ; Jump if no
ldy #0
sty OldIRQ+1 ; Reset the initialized flag
SetIRQ: sei ; Disable interrupts
sta IRQVec ; Set the new/old vector
stx IRQVec+1
cli ; Enable interrupts
Done: rts
; --------------------------------------------------------------------------
;
; void mouse_hide (void);
;
_mouse_hide:
lda Visible ; Get the flag
bne @L1 ; Jump if already invisible
ldx MouseSprite ; Sprite defined?
beq @L1 ; Jump if no
lda BitMask-1,x ; Get bit mask
eor #$FF ; We must clear the bit
sei ; Disable interrupts
and VIC_SPR_ENA
sta VIC_SPR_ENA ; Disable sprite
cli ; Enable interrupts
@L1: inc Visible ; Set the flag to invisible
rts
; --------------------------------------------------------------------------
;
; void mouse_show (void);
;
_mouse_show:
dec Visible ; Get the flag
bne @L1 ; Jump if still invisible
ldx MouseSprite ; Sprite defined?
beq @L1 ; Jump if no
lda BitMask-1,x ; Get bit mask
sei ; Disable interrupts
ora VIC_SPR_ENA
sta VIC_SPR_ENA ; Enable sprite
cli ; Enable interrupts
@L1: rts
; --------------------------------------------------------------------------
;
; void __fastcall__ mouse_box (int minx, int miny, int maxx, int maxy);
;
_mouse_box:
sei ; Disable interrupts
sta YMax
stx YMax+1 ; maxy
ldy #0
lda (sp),y
sta XMax
iny
lda (sp),y
sta XMax+1 ; maxx
iny
lda (sp),y
sta YMin
iny
lda (sp),y
sta YMin+1 ; miny
iny
lda (sp),y
sta XMin
iny
lda (sp),y
sta XMin+1 ; minx
cli ; Enable interrupts
jmp addysp1 ; Drop params, return
; --------------------------------------------------------------------------
;
; void mouse_info (...);
;
_mouse_info:
rts
; --------------------------------------------------------------------------
;
; void __fastcall__ mouse_move (int x, int y);
;
_mouse_move:
jsr popsreg ; Get X
sei ; Disable interrupts
sta YPos
stx YPos+1
lda sreg
ldx sreg+1
sta XPos
stx XPos+1 ; Set new position
lda Visible ; Mouse visible?
bne @L9 ; Jump if no
lda MouseSprite ; Sprite defined?
beq @L9
jsr MoveSprite ; Move the sprite to the mouse pos
@L9: cli ; Enable interrupts
rts
; --------------------------------------------------------------------------
;
; Mouse interrupt handler
;
MouseIRQ:
cld
lda SID_ADConv1 ; Get mouse X movement
ldy OldPotX
jsr MoveCheck ; Calculate movement vector
sty OldPotX
; Calculate the new X coordinate (--> a/y)
clc
adc XPos
tay ; Remember low byte
txa
adc XPos+1
; Limit the X coordinate to the bounding box
cpy XMin+1
bne @L1
cmp XMin
@L1: bpl @L2
ldy XMin
lda XMin+1
jmp @L4
@L2: cpy XMax+1
bne @L3
cmp XMax
beq @L4
@L3: bmi @L4
ldy XMax
lda XMax+1
@L4: sty XPos
sta XPos+1
; Calculate the Y movement vector
lda SID_ADConv2 ; Get mouse Y movement
ldy OldPotY
jsr MoveCheck ; Calculate movement
sty OldPotY
; Calculate the new Y coordinate (--> a/y)
clc
adc YPos
tay ; Remember low byte
txa
adc YPos+1
; Limit the Y coordinate to the bounding box
cpy YMin+1
bne @L5
cmp YMin
@L5: bpl @L6
ldy YMin
lda YMin+1
jmp @L8
@L6: cpy YMax+1
bne @L7
cmp YMax
beq @L8
@L7: bmi @L8
ldy YMax
lda YMax+1
@L8: sty YPos
sta YPos+1
; Jump to the next IRQ handler
jmp (OldIRQ)
; --------------------------------------------------------------------------
;
; Move check routine, called for both coordinates.
;
; Entry: y = old value of pot register
; a = current value of pot register
; Exit: y = value to use for old value
; x/a = delta value for position
;
MoveCheck:
sty OldValue
sta NewValue
ldx #$00
sec ; a = mod64 (new - old)
sbc OldValue
and #%01111111
cmp #%01000000 ; if (a > 0)
bcs @L1 ;
lsr a ; a /= 2;
beq @L2 ; if (a != 0)
ldy NewValue ; y = NewValue
rts ; return
@L1: ora #%11000000 ; else or in high order bits
cmp #$FF ; if (a != -1)
beq @L2
sec
ror a ; a /= 2
ldx #$FF ; high byte = -1
ldy NewValue
rts
@L2: lda #0
rts
; --------------------------------------------------------------------------
;
; Move the mouse sprite to the current mouse position. Must be called
; with interrupts off.
;
MoveSprite:
lda Visible ; Mouse pointer visible?
bne @L9 ; Jump if no
ldx MouseSprite ; Sprite defined?
beq @L9 ; Jump if no
ldy BitMask-1,x ; Get high bit mask
txa
asl a ; Index*2
tax
; Set the X position
lda XPos+1 ; Negative?
bmi @L2 ; Jump if yes
beq @L1
tya ; Load high position bit
@L1: ora VIC_SPR_HI_X ; Set high bit
sta VIC_SPR_HI_X
lda XPos
sta VIC_SPR0_X,x ; Set low byte
; Set the Y position
@L2: ldy YPos+1 ; Negative or too large?
bne @L9 ; Jump if yes
lda YPos
sta VIC_SPR0_Y,x ; Set Y position
; Done
@L9: rts
; --------------------------------------------------------------------------
; Data
.bss
OldIRQ: .res 2 ; Old IRQ vector
MousePort: .res 1 ; Port used for the mouse
MouseSprite: .res 1 ; Number of sprite to control
OldValue: .res 1 ; Temp for MoveCheck routine
NewValue: .res 1 ; Temp for MoveCheck routine
Visible: .res 1 ; Is the mouse visible?
OldPotX: .res 1 ; Old hw counter values
OldPotY: .res 1
XPos: .res 2 ; Current mouse position, X
YPos: .res 2 ; Current mouse position, Y
XMin: .res 2 ; X1 value of bounding box
YMin: .res 2 ; Y1 value of bounding box
XMax: .res 2 ; X2 value of bounding box
YMax: .res 2 ; Y2 value of bounding box
.data
BitMask: .byte $01, $02, $04, $08, $10, $20, $40, $80

48
libsrc/c64/read.s Normal file
View File

@@ -0,0 +1,48 @@
;
; Ullrich von Bassewitz, 30.05.1998
;
; int read (int fd, void* buf, int count);
;
; THIS IS A HACK!
;
.export _read
.import popax
.importzp ptr1, ptr2, ptr3
.include "../cbm/cbm.inc"
_read: jsr popax ; get count
sta ptr2
stx ptr2+1 ; save it for later
jsr popax ; get buf
sta ptr1
stx ptr1+1
jsr popax ; get fd and discard it
lda #0
sta ptr3
sta ptr3+1 ; set count
L1: lda ptr2
ora ptr2+1 ; count zero?
beq L9
jsr BASIN
ldy #0
sta (ptr1),y ; save char
inc ptr1
bne L2
inc ptr1+1
L2: inc ptr3 ; increment count
bne L3
inc ptr3+1
L3: cmp #$0D ; CR?
bne L1
; Done, return the count
L9: lda ptr3
ldx ptr3+1
rts

41
libsrc/c64/readjoy.s Normal file
View File

@@ -0,0 +1,41 @@
;
; Ullrich von Bassewitz, 23.09.1998
;
; unsigned readjoy (unsigned char joy);
;
.export _readjoy
.include "c64.inc"
.proc _readjoy
tax ; Joystick number into X
bne joy2
; Read joystick 1
joy1: lda #$7F
sei
sta CIA1_PRA
lda CIA1_PRB
cli
and #$1F
eor #$1F
rts
; Read joystick 2
joy2: ldx #0
lda #$E0
ldy #$FF
sta CIA1_DDRA
lda CIA1_PRA
sty CIA1_DDRA
and #$1F
eor #$1F
rts
.endproc

699
libsrc/c64/rs232.s Normal file
View File

@@ -0,0 +1,699 @@
;
; SwiftLink/Turbo-232 v0.90 device driver, by Craig Bruce, 14-Apr-1998.
;
; This software is Public Domain. It is in Buddy assembler format.
;
; This device driver uses the SwiftLink RS-232 Serial Cartridge, available from
; Creative Micro Designs, Inc, and also supports the extensions of the Turbo232
; Serial Cartridge. Both devices are based on the 6551 ACIA chip. It also
; supports the "hacked" SwiftLink with a 1.8432 MHz crystal.
;
; The code assumes that the kernal + I/O are in context. On the C128, call
; it from Bank 15. On the C64, don't flip out the Kernal unless a suitable
; NMI catcher is put into the RAM under then Kernal. For the SuperCPU, the
; interrupt handling assumes that the 65816 is in 6502-emulation mode.
;
;--------------------------------------------------------------------------
;
; Adapted for the use with the cc65 runtime library by
; Ullrich von Bassewitz (uz@musoftware.de) 02-May-1999.
;
; All external functions are C callable, the return value is an error code.
;
.importzp ptr1, ptr2, tmp1, tmp2
.import popa, popax
.export _rs232_init, _rs232_params, _rs232_done, _rs232_get
.export _rs232_put, _rs232_pause, _rs232_unpause, _rs232_status
useC64 = 1
.if useC64
NmiExit = $febc ;exit address for nmi
.else
NmiExit = $ff33 ;exit address for nmi
.endif
ACIA = $DE00
;----------------------------------------------------------------------------
;
; Global variables
;
.bss
DropCnt: .res 4 ; Number of bytes lost from rx buffer full
Initialized: .res 1 ; Flag indicating driver is initialized
Stopped: .res 1 ; Flow-stopped flag
RtsOff: .res 1 ;
Errors: .res 1 ; Number of bytes received in error, low byte
Turbo232: .res 1 ; Flag indicating turbo-232
HackedFlag: .res 1 ; Flag indicating hacked-crystal swiftlink
CpuSpeed: .res 1 ; In MHz
RecvHead: .res 1 ; Head of receive buffer
RecvTail: .res 1 ; Tail of receive buffer
RecvFreeCnt: .res 1 ; Number of bytes in receive buffer
SendHead: .res 1 ; Head of send buffer
SendTail: .res 1 ; Tail of send buffer
SendFreeCnt: .res 1 ; Number of bytes free in send buffer
BaudCode: .res 1 ; Current baud in effect
; Send and receive buffers: 256 bytes each
RecvBuf: .res 256
SendBuf: .res 256
.data
NmiContinue: .byte $4c ; JMP instruction for NMI save -- continue
NmiSave: .res 2 ; normal NMI handler
; Switftlink register offsets
RegData = 0 ; Data register
RegStatus = 1 ; Status register
RegCommand = 2 ; Command register
RegControl = 3 ; Control register
RegClock = 7 ; Turbo232 external baud-rate generator
; Error codes. Beware: The codes must match the codes in the C header file
ErrNotInitialized = $01
ErrBaudTooFast = $02
ErrBaudNotAvail = $03
ErrNoData = $04
ErrOverflow = $05
.code
;----------------------------------------------------------------------------
;
; unsigned char __fastcall__ rs232_init (char hacked);
; /* Initialize the serial port, install the interrupt handler. The parameter
; * must be true (non zero) for a hacked swiftlink and false (zero) otherwise.
; */
;
_rs232_init:
bit Initialized ;** shut down if started
bpl @L1
pha
jsr _rs232_done
pla
;** set hacked-crystal
@L1: sta HackedFlag
;** check for turbo-232
lda #$00
sta ACIA+RegControl
tax
lda ACIA+RegClock
beq @L3
dex
@L3: stx Turbo232
;** get C128/C64 cpu speed
.if useC64
lda #1
sta CpuSpeed
.else
lda $d030
and #$01
clc
adc #1
sta CpuSpeed
.endif
;** check for super-cpu at 20 MHz
bit $d0bc
bmi @L4
bit $d0b8
bvs @L4
lda #20
sta CpuSpeed
;** initialize buffers & control
@L4: lda #0
sta RecvHead
sta SendHead
sta RecvTail
sta SendTail
sta Errors
sta Stopped
lda #255
sta RecvFreeCnt
sta SendFreeCnt
;** set up nmi's
lda $318
ldy $319
sta NmiSave+0
sty NmiSave+1
lda #<NmiHandler
ldy #>NmiHandler
sta $318
sty $319
;** set default to 2400-8N1, enable interrupts
lda ACIA+RegData
lda ACIA+RegStatus
lda #$18
bit HackedFlag
bpl @L5
lda #$1a
@L5: sta ACIA+RegControl
lda #$01
sta RtsOff
ora #$08
sta ACIA+RegCommand
lda #$06
sta BaudCode
;** return
lda #$ff
sta Initialized
lda #$00
tax
rts
;----------------------------------------------------------------------------
;
; unsigned char __fastcall__ rs232_params (unsigned char params, unsigned char parity);
; /* Set the port parameters. Use a combination of the #defined values above. */
;
; Set communication parameters.
;
; baud rates stops word | parity
; --------------------- ----- ----- | ---------
; $00=50 $08=9600 $00=1 $00=8 | $00=none
; $01=110 $09=19200 $80=2 $20=7 | $20=odd
; $02=134.5 $0a=38400 $40=6 | $60=even
; $03=300 $0b=57600 $60=5 | $A0=mark
; $04=600 $0c=115200 | $E0=space
; $05=1200 $0d=230400
; $06=2400 $0e=future
; $07=4800 $0f=future
;
_rs232_params:
jsr CheckInitialized ;** check initialized
bcc @L1
rts
; Save new parity
@L1: and #%11100000
ora #%00000001
sta tmp2
; Check cpu speed against baud rate
jsr popa
sta tmp1
and #$0f
cmp #$0c
bcc @L3
ldx CpuSpeed
cpx #1+1
bcc @L2
cmp #$0c
beq @L3
cpx #4
bcs @L3
@L2: lda #ErrBaudTooFast
bne @L9
; Set baud/parameters
@L3: lda tmp1
and #$0f
tax
lda NormBauds,x
bit HackedFlag
bpl @L4
lda HackBauds,x
@L4: cmp #$ff
bne @L5
lda #ErrBaudNotAvail
bne @L9
@L5: tax
and #$30
beq @L6
bit Turbo232
bmi @L6
lda #ErrBaudNotAvail
bne @L9
@L6: lda tmp1
and #$0f
sta BaudCode
lda tmp1
and #%11100000
ora #%00010000
sta tmp1
txa
and #$0f
ora tmp1
sta ACIA+RegControl
txa
and #%00110000
beq @L7
lsr
lsr
lsr
lsr
eor #%00000011
sta ACIA+RegClock
; Set new parity
@L7: lda tmp2
sta RtsOff
ora #%00001000
sta ACIA+RegCommand
lda #0
@L9: ldx #0
rts
.rodata
NormBauds:
.byte $ff,$ff,$ff,$05,$06,$07,$08,$0a,$0c,$0e,$0f,$10,$20,$30,$ff,$ff
HackBauds:
.byte $01,$03,$04,$06,$07,$08,$0a,$0c,$0e,$0f,$ff,$ff,$00,$ff,$ff,$ff
;in: 0 1 2 3 4 5 6 7 8 9 a b c d e f
;baud50 110 134 3 6 12 24 48 96 19 38 57 115 230 exp exp
;out masks: $0F=Baud, val$FF=err
; $30=t232ExtBaud: $00=none, $10=57.6, $20=115.2, $30=230.4
.code
;----------------------------------------------------------------------------
;
; unsigned char __fastcall__ rs232_done (void);
; /* Close the port, deinstall the interrupt hander. You MUST call this function
; * before terminating the program, otherwise the machine may crash later. If
; * in doubt, install an exit handler using atexit(). The function will do
; * nothing, if it was already called.
; */
;
_rs232_done:
bit Initialized ;** check initialized
bpl @L9
; Stop interrupts, drop DTR
lda RtsOff
and #%11100010
ora #%00000010
sta ACIA+RegCommand
; Restore NMI vector
lda NmiSave+0
ldy NmiSave+1
sta $318
sty $319
; Flag uninitialized
@L9: lda #$00
sta Initialized
tax
rts
;----------------------------------------------------------------------------
;
; unsigned char __fastcall__ rs232_get (char* B);
; /* Get a character from the serial port. If no characters are available, the
; * function will return RS_ERR_NO_DATA, so this is not a fatal error.
; */
;
_rs232_get:
jsr CheckInitialized ; Check if initialized
bcc @L1
rts
; Check for bytes to send
@L1: sta ptr1
stx ptr1+1 ; Store pointer to received char
ldx SendFreeCnt
cpx #$ff
beq @L2
lda #$00
jsr TryToSend
; Check for buffer empty
@L2: lda RecvFreeCnt
cmp #$ff
bne @L3
lda #ErrNoData
ldx #0
rts
; Check for flow stopped & enough free: release flow control
@L3: ldx Stopped
beq @L4
cmp #63
bcc @L4
lda #$00
sta Stopped
lda RtsOff
ora #%00001000
sta ACIA+RegCommand
; Get byte from buffer
@L4: ldx RecvHead
lda RecvBuf,x
inc RecvHead
inc RecvFreeCnt
ldx #$00
sta (ptr1,x)
txa ; Return code = 0
rts
;----------------------------------------------------------------------------
;
; unsigned char __fastcall__ rs232_put (char B);
; /* Send a character via the serial port. There is a transmit buffer, but
; * transmitting is not done via interrupt. The function returns
; * RS_ERR_OVERFLOW if there is no space left in the transmit buffer.
; */
;
_rs232_put:
jsr CheckInitialized ; Check initialized
bcc @L1
rts
; Try to send
@L1: ldx SendFreeCnt
cpx #$ff
beq @L2
pha
lda #$00
jsr TryToSend
pla
; Put byte into send buffer & send
@L2: ldx SendFreeCnt
bne @L3
lda #ErrOverflow
ldx #$00
rts
@L3: ldx SendTail
sta SendBuf,x
inc SendTail
dec SendFreeCnt
lda #$ff
jsr TryToSend
lda #$00
tax
rts
;----------------------------------------------------------------------------
;
; unsigned char __fastcall__ rs232_pause (void);
; /* Assert flow control and disable interrupts. */
;
_rs232_pause:
; Check initialized
jsr CheckInitialized
bcc @L1
rts
; Assert flow control
@L1: lda RtsOff
sta Stopped
sta ACIA+RegCommand
; Delay for flow stop to be received
ldx BaudCode
lda PauseTimes,x
jsr DelayMs
; Stop rx interrupts
lda RtsOff
ora #$02
sta ACIA+RegCommand
lda #0
tax
rts
.rodata
; Delay times: 32 byte-receive times in milliseconds, or 100 max.
; Formula = 320,000 / baud
PauseTimes:
.byte 100,100,100,100,100,100,100,067,034,017,009,006,003,002,001,001
;in: 0 1 2 3 4 5 6 7 8 9 a b c d e f
;baud50 110 134 3 6 12 24 48 96 19 38 57 115 230 exp exp
.code
;----------------------------------------------------------------------------
;
; unsigned char __fastcall__ rs232_unpause (void);
; /* Re-enable interrupts and release flow control */
;
_rs232_unpause:
; Check initialized
jsr CheckInitialized
bcc @L1
rts
; Re-enable rx interrupts & release flow control
@L1: lda #$00
sta Stopped
lda RtsOff
ora #%00001000
sta ACIA+RegCommand
; Poll for stalled char & exit
jsr PollReceive
lda #0
tax
rts
;----------------------------------------------------------------------------
;
; unsigned char __fastcall__ rs232_status (unsigned char* status,
; unsigned char* errors);
; /* Return the serial port status. */
;
_rs232_status:
sta ptr2
stx ptr2+1
jsr popax
sta ptr1
stx ptr1+1
jsr CheckInitialized
bcs @L9
; Get status
lda ACIA+RegStatus
ldy #0
sta (ptr1),y
jsr PollReceive ; bug-recovery hack
lda Errors
sta (ptr2),y
tya
tax
@L9: rts
;----------------------------------------------------------------------------
;
; NMI handler
; C128 NMI overhead=76 cycles: int=7, maxLatency=6, ROMenter=33, ROMexit=30
; C64 NMI overhead=76 cycles: int=7, maxLatency=6, ROMenter=34, ROMexit=29
;
; timing: normal=76+43+9=128 cycles, assertFlow=76+52+9=137 cycles
;
; C128 @ 115.2k: 177 cycles avail (fast)
; C64 @ 57.6k: 177 cycles avail, worstAvail=177-43? = 134
; SCPU @ 230.4k: 868 cycles avail: for a joke!
;
NmiHandler:
.if useC64
pha
lda ACIA+RegStatus ;(4) ;status ;check for byte received
and #$08 ;(2)
beq @L9 ;(2*)
cld
txa
pha
tya
pha
.else
lda ACIA+RegStatus ;(4) ;status ;check for byte received
and #$08 ;(2)
beq NmiNorm ;(2*)
.endif
lda ACIA+RegStatus ;(4) opt ;status ;check for receive errors
and #$07 ;(2) opt
beq @L1 ;(3*)opt
inc Errors ;(5^)opt
@L1: lda ACIA+RegData ;(4) ;data ;get byte and put into receive buffer
ldy RecvTail ;(4)
ldx RecvFreeCnt ;(4)
beq @L3 ;(2*)
sta RecvBuf,y ;(5)
inc RecvTail ;(6)
dec RecvFreeCnt ;(6)
cpx #33 ;(2) ;check for buffer space low
bcc @L2 ;(2*)
jmp NmiExit ;(3)
; Assert flow control
@L2: lda RtsOff ;(3) ;assert flow control if buffer space too low
sta ACIA+RegCommand ;(4) ;command
sta Stopped ;(3)
jmp NmiExit ;(3)
; Drop this char
@L3: inc DropCnt+0 ;not time-critical
bne @L4
inc DropCnt+1
bne @L4
inc DropCnt+2
bne @L4
inc DropCnt+3
@L4: jmp NmiExit
@L9:
.if useC64
pla
.endif
jmp NmiContinue
;----------------------------------------------------------------------------
;
; CheckInitialized - internal check if initialized
; Set carry and an error code if not initialized, clear carry and do not
; change any registers if initialized.
;
CheckInitialized:
bit Initialized
bmi @L1
lda #ErrNotInitialized
ldx #0
sec
rts
@L1: clc
rts
;----------------------------------------------------------------------------
; Try to send a byte. Internal routine. A = TryHard
TryToSend:
sta tmp1 ; Remember tryHard flag
@L0: lda SendFreeCnt
cmp #$ff
beq @L3 ; Bail out
; Check for flow stopped
@L1: lda Stopped
bne @L3 ; Bail out
;** check that swiftlink is ready to send
@L2: lda ACIA+RegStatus
and #$10
bne @L4
bit tmp1 ;keep trying if must try hard
bmi @L0
@L3: rts
;** send byte and try again
@L4: ldx SendHead
lda SendBuf,x
sta ACIA+RegData
inc SendHead
inc SendFreeCnt
jmp @L0
;----------------------------------------------------------------------------
;
; PollReceive - poll for rx char
; This function is useful in odd cases where the 6551 has a character in
; it but it fails to raise an NMI. It might be edge-triggering conditions?
; Actually, I'm not entirely sure that this condition can still arrise, but
; calling this function does no harm.
;
PollReceive:
lda #$08
and ACIA+RegStatus
beq @L9
and ACIA+RegStatus
beq @L9
lda ACIA+RegData
ldx RecvFreeCnt
beq @L9
ldx RecvTail
sta RecvBuf,x
inc RecvTail
dec RecvFreeCnt
@L9: rts
;----------------------------------------------------------------------------
;
; DelayMs : delay for given number of milliseconds
; This implementation isn't very rigerous; it merely delays for the
; approximate number of clock cycles for the processor speed.
; Algorithm:
; repeat for number of milliseconds:
; repeat for number of MHz of cpu speed:
; delay for 1017 clock cycles
;
DelayMs: ;( .A=milliseconds )
@L1: ldy CpuSpeed
@L2: ldx #203 ;(2)
@L3: dex ;(2)
bne @L3 ;(3) // 1017 cycles
dey
bne @L2
sec
sbc #1
bne @L1
rts
.end

47
libsrc/c64/write.s Normal file
View File

@@ -0,0 +1,47 @@
;
; Ullrich von Bassewitz, 30.05.1998
;
; int write (int fd, const void* buf, int count);
;
; THIS IS A HACK!
;
.export _write
.import popax
.importzp ptr1, ptr2, ptr3
.include "../cbm/cbm.inc"
_write: jsr popax ; get count
sta ptr2
stx ptr2+1 ; save it for later
sta ptr3
stx ptr3+1 ; save for function result
jsr popax ; get buf
sta ptr1
stx ptr1+1
jsr popax ; get fd and discard it
L1: lda ptr2
ora ptr2+1 ; count zero?
beq L9
ldy #0
lda (ptr1),y
jsr BSOUT
inc ptr1
bne L2
inc ptr1+1
L2: lda ptr2
bne L3
dec ptr2
dec ptr2+1
jmp L1
L3: dec ptr2
jmp L1
; No error, return count
L9: lda ptr3
ldx ptr3+1
rts