repo_id
stringlengths
5
115
size
int64
590
5.01M
file_path
stringlengths
4
212
content
stringlengths
590
5.01M
wagiminator/C64-Collection
3,472
C64_xu1541/software/tools/cc65-2.13.2/libsrc/cbm510/cbm510-stdjoy.s
; ; Standard joystick driver for the Commodore 510 (aka P500). May be used ; multiple times when linked to the statically application. ; ; Ullrich von Bassewitz, 2003-02-16 ; .include "zeropage.inc" .include "extzp.inc" .include "joy-kernel.inc" .include "joy-error.inc" .include "cbm510.inc" .include "extzp.inc" .macpack generic ; ------------------------------------------------------------------------ ; Header. Includes jump table .segment "JUMPTABLE" ; Driver signature .byte $6A, $6F, $79 ; "joy" .byte JOY_API_VERSION ; Driver API version number ; Button state masks (8 values) .byte $01 ; JOY_UP .byte $02 ; JOY_DOWN .byte $04 ; JOY_LEFT .byte $08 ; JOY_RIGHT .byte $10 ; JOY_FIRE .byte $00 ; JOY_FIRE2 unavailable .byte $00 ; Future expansion .byte $00 ; Future expansion ; Jump table. .addr INSTALL .addr UNINSTALL .addr COUNT .addr READ .addr 0 ; IRQ entry unused ; ------------------------------------------------------------------------ ; Constants JOY_COUNT = 2 ; Number of joysticks we support ; ------------------------------------------------------------------------ ; Data. .code ; ------------------------------------------------------------------------ ; INSTALL routine. Is called after the driver is loaded into memory. If ; possible, check if the hardware is present and determine the amount of ; memory available. ; Must return an JOY_ERR_xx code in a/x. ; INSTALL: lda #<JOY_ERR_OK ldx #>JOY_ERR_OK ; rts ; Run into UNINSTALL instead ; ------------------------------------------------------------------------ ; UNINSTALL routine. Is called before the driver is removed from memory. ; Can do cleanup or whatever. Must not return anything. ; UNINSTALL: rts ; ------------------------------------------------------------------------ ; COUNT: Return the total number of available joysticks in a/x. ; COUNT: lda #<JOY_COUNT ldx #>JOY_COUNT rts ; ------------------------------------------------------------------------ ; READ: Read a particular joystick passed in A. ; READ: ldx #$0F ; Switch to the system bank stx IndReg tax ; Save joystick number ; Get the direction bits ldy #CIA::PRB lda (cia2),y ; Read joystick inputs sta tmp1 ; Get the fire bits ldy #CIA::PRA lda (cia2),y ; Make the result value cpx #$00 ; Joystick 0? bne @L1 ; Jump if no ; Joystick 1, fire is in bit 6, direction in bit 0-3 asl a jmp @L2 ; Joystick 2, fire is in bit 7, direction in bit 5-7 @L1: ldx #$00 ; High byte of return value lsr tmp1 lsr tmp1 lsr tmp1 lsr tmp1 ; Mask the relavant bits, get the fire bit @L2: asl a ; Fire bit into carry lda tmp1 and #$0F bcc @L3 ora #$10 @L3: eor #$1F ; All bits are inverted ; Switch back to the execution bank and return the joystick mask in a/x ldy ExecReg sty IndReg rts
wagiminator/C64-Collection
3,366
C64_xu1541/software/tools/cc65-2.13.2/libsrc/cbm510/cgetc.s
; ; Ullrich von Bassewitz, 16.09.2001 ; ; char cgetc (void); ; .export _cgetc .condes k_blncur, 2 .import cursor .include "cbm510.inc" .include "extzp.inc" ; ------------------------------------------------------------------------ .proc _cgetc lda keyidx ; Characters waiting? bne L3 ; Jump if so ; Switch on the cursor if needed lda CURS_FLAG pha lda cursor jsr setcursor L1: lda keyidx beq L1 ldx #0 pla bne L2 inx L2: txa jsr setcursor ; Read the character from the keyboard buffer L3: ldx #$00 ; Get index ldy keybuf ; Get first character in the buffer sei L4: lda keybuf+1,x ; Move up the remaining chars sta keybuf,x inx cpx keyidx bne L4 dec keyidx cli ldx #$00 ; High byte tya ; First char from buffer rts .endproc ; ------------------------------------------------------------------------ ; .proc setcursor ldy #$00 ; tax ; On or off? bne @L9 ; Go set it on lda CURS_FLAG ; Is the cursor currently off? bne @L8 ; Jump if yes lda #1 sta CURS_FLAG ; Mark it as off lda CURS_STATE ; Cursor currently displayed? sty CURS_STATE ; Cursor will be cleared later beq @L8 ; Jump if no ; Switch to the system bank, load Y with the cursor X coordinate lda #$0F sta IndReg ; Access system bank ldy CURS_X ; Reset the current cursor lda CURS_COLOR sta (CRAM_PTR),y ; Store cursor color lda ExecReg sta IndReg ; Switch to our segment lda (SCREEN_PTR),y eor #$80 ; Toggle reverse flag sta (SCREEN_PTR),y ; Done @L8: rts @L9: sty CURS_FLAG ; Cursor on (Y = 0) rts .endproc ; ------------------------------------------------------------------------ ; Blink the cursor in the interrupt. A blinking cursor is only available if ; we use the cgetc() function, so we will export this IRQ handler only in ; case the module is included into a program. .proc k_blncur lda CURS_FLAG ; Is the cursor on? bne curend ; Jump if not dec CURS_BLINK bne curend ; Re-initialize the blink counter lda #20 ; Initial value sta CURS_BLINK ; Load Y with the cursor X coordinate ldy CURS_X ; Check if the cursor state was on or off before lda CURS_COLOR ; Load color behind cursor lsr CURS_STATE ; Cursor currently displayed? bcs curset ; Jump if yes ; Cursor was off before, switch it on inc CURS_STATE ; Mark as displayed lda (CRAM_PTR),y ; Get color behind cursor... sta CURS_COLOR ; ...and remember it lda CHARCOLOR ; Use character color ; Set the cursor with color in A curset: sta (CRAM_PTR),y ; Store cursor color lda ExecReg sta IndReg ; Switch to our segment lda (SCREEN_PTR),y eor #$80 ; Toggle reverse flag sta (SCREEN_PTR),y ; Switch back to the system bank lda #$0F sta IndReg curend: rts .endproc
wagiminator/C64-Collection
3,048
C64_xu1541/software/tools/cc65-2.13.2/libsrc/joystick/joy_load.s
; ; Ullrich von Bassewitz, 2006-06-05 ; ; unsigned char __fastcall__ joy_load_driver (const char* driver); ; /* Load and install a joystick driver. Return an error code. */ .include "joy-kernel.inc" .include "joy-error.inc" .include "modload.inc" .include "fcntl.inc" .import pushax .import pusha0 .import incsp2 .import _open .import _read .import _close ;---------------------------------------------------------------------------- ; Variables .data ctrl: .addr _read .res 2 ; CALLERDATA .res 2 ; MODULE .res 2 ; MODULE_SIZE .res 2 ; MODULE_ID ;---------------------------------------------------------------------------- ; Code .code .proc _joy_load_driver ; Save name on the C stack. We will need it later as parameter passed to open() jsr pushax ; Check if we do already have a driver loaded. If so, remove it. lda _joy_drv ora _joy_drv+1 beq @L1 jsr _joy_uninstall ; Open the file. The name parameter is already on stack and will get removed ; by open(). ; ctrl.callerdata = open (name, O_RDONLY); @L1: lda #<O_RDONLY jsr pusha0 ldy #4 ; Argument size jsr _open sta ctrl + MOD_CTRL::CALLERDATA stx ctrl + MOD_CTRL::CALLERDATA+1 ; if (ctrl.callerdata >= 0) { txa bmi @L3 ; /* Load the module */ ; Res = mod_load (&ctrl); lda #<ctrl ldx #>ctrl jsr _mod_load pha ; /* Close the input file */ ; close (ctrl.callerdata); lda ctrl + MOD_CTRL::CALLERDATA ldx ctrl + MOD_CTRL::CALLERDATA+1 jsr _close ; /* Check the return code */ ; if (Res == MLOAD_OK) { pla bne @L3 ; Check the driver signature, install the driver. c is already on stack and ; will get removed by joy_install(). ; Res = joy_install (ctrl.module); lda ctrl + MOD_CTRL::MODULE ldx ctrl + MOD_CTRL::MODULE+1 jsr _joy_install ; If joy_install was successful, we're done tax beq @L2 ; The driver didn't install correctly. Remove it from memory and return the ; error code. pha ; Save the error code lda _joy_drv ldx _joy_drv+1 jsr _mod_free ; Free the driver memory jsr _joy_clear_ptr ; Clear joy_drv pla ; Restore the error code ldx #0 ; We must return an int @L2: rts ; Done ; Open or mod_load failed. Return an error code. @L3: lda #<JOY_ERR_CANNOT_LOAD ldx #>JOY_ERR_CANNOT_LOAD rts .endproc
wagiminator/C64-Collection
3,298
C64_xu1541/software/tools/cc65-2.13.2/libsrc/joystick/joy-kernel.s
; ; Ullrich von Bassewitz, 2002-12-20 ; ; Common functions of the joystick API. ; .importzp ptr1 .interruptor joy_irq ; Export as IRQ handler .include "joy-kernel.inc" .include "joy-error.inc" ;---------------------------------------------------------------------------- ; Variables .bss _joy_drv: .res 2 ; Pointer to driver _joy_masks: .res .sizeof(JOY_HDR::MASKS) ; Jump table for the driver functions. .data joy_vectors: joy_install: jmp $0000 joy_uninstall: jmp $0000 joy_count: jmp $0000 joy_read: jmp $0000 joy_irq: .byte $60, $00, $00 ; RTS plus two dummy bytes ; Driver header signature .rodata joy_sig: .byte $6A, $6F, $79, JOY_API_VERSION ; "joy", version .code ;---------------------------------------------------------------------------- ; unsigned char __fastcall__ joy_install (void* driver); ; /* Install the driver once it is loaded */ _joy_install: sta _joy_drv sta ptr1 stx _joy_drv+1 stx ptr1+1 ; Check the driver signature ldy #.sizeof(joy_sig)-1 @L0: lda (ptr1),y cmp joy_sig,y bne inv_drv dey bpl @L0 ; Copy the mask array ldy #JOY_HDR::MASKS + .sizeof(JOY_HDR::MASKS) - 1 ldx #.sizeof(JOY_HDR::MASKS)-1 @L1: lda (ptr1),y sta _joy_masks,x dey dex bpl @L1 ; Copy the jump vectors ldy #JOY_HDR::JUMPTAB ldx #0 @L2: inx ; Skip the JMP opcode jsr copy ; Copy one byte jsr copy ; Copy one byte cpy #(JOY_HDR::JUMPTAB + .sizeof(JOY_HDR::JUMPTAB)) bne @L2 jsr joy_install ; Call driver install routine tay ; Test error code bne @L3 ; Bail out if install had errors ; Install the IRQ vector if the driver needs it. A/X contains the error code ; from joy_install, so don't use it. ldy joy_irq+2 ; Check high byte of IRQ vector beq @L3 ; Jump if vector invalid ldy #$4C ; JMP opcode sty joy_irq ; Activate IRQ routine @L3: rts ; Driver signature invalid inv_drv: lda #JOY_ERR_INV_DRIVER ldx #0 rts ; Copy one byte from the jump vectors copy: lda (ptr1),y iny set: sta joy_vectors,x inx rts ;---------------------------------------------------------------------------- ; unsigned char __fastcall__ joy_uninstall (void); ; /* Uninstall the currently loaded driver. Note: This call does not free ; * allocated memory. ; */ _joy_uninstall: lda #$60 ; RTS opcode sta joy_irq ; Disable IRQ entry point jsr joy_uninstall ; Call the driver routine _joy_clear_ptr: ; External entry point lda #0 sta _joy_drv sta _joy_drv+1 ; Clear the driver pointer tax ; Return zero rts
wagiminator/C64-Collection
1,480
C64_xu1541/software/tools/cc65-2.13.2/libsrc/dbg/asmtab.s
; ; Ullrich von Bassewitz, 07.08.1998 ; ; Tables needed for the line assembler/disassembler. ; .export OffsetTab .export AdrFlagTab .export SymbolTab1, SymbolTab2 .export MnemoTab1, MnemoTab2 ; ------------------------------------------------------------------------- ; Assembler tables .rodata OffsetTab: .byte $40,$02,$45,$03,$D0,$08,$40,$09 .byte $30,$22,$45,$33,$D0,$08,$40,$09 .byte $40,$02,$45,$33,$D0,$08,$40,$09 .byte $40,$02,$45,$B3,$D0,$08,$40,$09 .byte $00,$22,$44,$33,$D0,$8C,$44,$00 .byte $11,$22,$44,$33,$D0,$8C,$44,$9A .byte $10,$22,$44,$33,$D0,$08,$40,$09 .byte $10,$22,$44,$33,$D0,$08,$40,$09 .byte $62,$13,$78,$A9 AdrFlagTab: .byte $00,$21,$81,$82,$00,$00,$59,$4D .byte $91,$92,$86,$4A,$85,$9D SymbolTab1: .byte $2C,$29,$2C,$23,$28,$24 SymbolTab2: .byte $59,$00,$58,$24,$24,$00 MnemoTab1: .byte $1C,$8A,$1C,$23,$5D,$8B,$1B,$A1 .byte $9D,$8A,$1D,$23,$9D,$8B,$1D,$A1 .byte $00,$29,$19,$AE,$69,$A8,$19,$23 .byte $24,$53,$1B,$23,$24,$53,$19,$A1 .byte $00,$1A,$5B,$5B,$A5,$69,$24,$24 .byte $AE,$AE,$A8,$AD,$29,$00,$7C,$00 .byte $15,$9C,$6D,$9C,$A5,$69,$29,$53 .byte $84,$13,$34,$11,$A5,$69,$23,$A0 MnemoTab2: .byte $D8,$62,$5A,$48,$26,$62,$94,$88 .byte $54,$44,$C8,$54,$68,$44,$E8,$94 .byte $00,$B4,$08,$84,$74,$B4,$28,$6E .byte $74,$F4,$CC,$4A,$72,$F2,$A4,$8A .byte $00,$AA,$A2,$A2,$74,$74,$74,$72 .byte $44,$68,$B2,$32,$B2,$00,$22,$00 .byte $1A,$1A,$26,$26,$72,$72,$88,$C8 .byte $C4,$CA,$26,$48,$44,$44,$A2,$C8
wagiminator/C64-Collection
5,679
C64_xu1541/software/tools/cc65-2.13.2/libsrc/dbg/dbgdasm.s
; ; Ullrich von Bassewitz, 07.08.1998 ; ; unsigned DbgDisAsm (char* buf, unsigned addr); ; unsigned DbgDisAsm (unsigned addr); ; ; ; Part of this code is taken from the Plus/4 machine language monitor ; (TEDMon). ; .import popax .import __hextab, OffsetTab, AdrFlagTab .import SymbolTab1, SymbolTab2, MnemoTab1, MnemoTab2 ; ------------------------------------------------------------------------- ; Equates for better readability .importzp sreg, tmp1, tmp2, tmp3, tmp4, ptr1, ptr2, ptr3 BufIndex = tmp1 ; Index into output buffer OperandLen = tmp2 ; Length of operand BufLen = tmp3 ; Length of output buffer AdrFlagBuf = tmp4 ; Flag for addressing mode YSave = sreg ; Temp storage XSave = sreg+1 ; Dito BufPtr = ptr1 ; Pointer to output buffer MemPtr = ptr2 ; Pointer to memory to disassemble MnemoBuf = ptr3 ; Buffer for decoding mnemonic ; ------------------------------------------------------------------------- ; Main entries .export _DbgDisAsm, _DbgDisAsmLen .proc _DbgDisAsm sta BufLen ; Save the buffer length jsr popax ; Get the buffer pointer sta BufPtr stx BufPtr+1 jsr popax ; Get the address sta MemPtr stx MemPtr+1 lda #0 sta BufIndex ; Initialize index into buffer jsr DisAssLine ; Disassemble one line into the buffer lda BufLen ; Get requested length sec sbc BufIndex beq L2 tax ; Count into X ldy BufIndex lda #$20 ; Get a space L1: sta (BufPtr),y iny dex bne L1 L2: lda #0 ; Add C string terminator sta (BufPtr),y beq disassret .endproc _DbgDisAsmLen: sta MemPtr ; Save address stx MemPtr+1 ldy #$00 lda (MemPtr),y ; Get the opcode from memory... jsr AnalyzeOPCode ; ...and analyze it disassret: ldx OperandLen ; Get length of operand inx ; Adjust for opcode byte txa ldx #$00 ; Clear high byte rts ; ------------------------------------------------------------------------- ; Helper functions Put3Spaces: jsr PutSpace Put2Spaces: jsr PutSpace PutSpace: lda #$20 PutChar: sty YSave ; Save Y ldy BufIndex ; Get current line pointer cpy BufLen ; Be sure not to overflow the buffer bcs PC9 sta (BufPtr),y ; store character iny ; bump index sty BufIndex PC9: ldy YSave ; get old value rts ; Print the 16 bit hex value in X/Y PutHex16: txa jsr PutHex8 tya ; Print 8 bit value in A, save X and Y PutHex8: stx XSave sty YSave ldy BufIndex pha lsr a lsr a lsr a lsr a tax lda __hextab,x sta (BufPtr),y iny pla and #$0F tax lda __hextab,x sta (BufPtr),y iny sty BufIndex ldy YSave ldx XSave rts ; ------------------------------------------------------------------------- ; Disassemble one line DisAssLine: ldy MemPtr ldx MemPtr+1 jsr PutHex16 ; Print the address jsr Put2Spaces ; Add some space ldy #$00 lda (MemPtr),y ; Get the opcode from memory... jsr AnalyzeOPCode ; ...and analyze it pha ; Save mnemonic ldx OperandLen ; Number of bytes ; Print the bytes that make up the instruction inx L2083: dex bpl L208C ; Print the instruction bytes jsr Put3Spaces ; If none left, print spaces instead jmp L2094 L208C: lda (MemPtr),y ; Get a byte from memory jsr PutHex8 ; ...and print it jsr PutSpace ; Add some space L2094: iny ; Next one... cpy #$03 ; Maximum is three bcc L2083 ; jsr Put2Spaces ; Add some space after bytes ; Print the assembler mnemonic pla ; Get mnemonic code ldx #$03 jsr PutMnemo ; Print the mnemonic ldx #$06 ; Print the operand L20A4: cpx #$03 bne L20BA ldy OperandLen beq L20BA L20AC: lda AdrFlagBuf cmp #$E8 ; Branch? lda (MemPtr),y ; Get branch offset bcs GetBranchAdr ; If branch: Calculate address jsr PutHex8 ; Otherwise print 8bit value dey bne L20AC L20BA: asl AdrFlagBuf bcc L20CC lda SymbolTab1-1,x jsr PutChar lda SymbolTab2-1,x beq L20CC jsr PutChar L20CC: dex bne L20A4 rts ; If the instruction is a branch, calculate the absolute address of the ; branch target and print it. GetBranchAdr: jsr L20DD clc adc #$01 bne L20D9 inx ; Bump high byte L20D9: tay jmp PutHex16 ; Output address L20DD: ldx MemPtr+1 tay bpl L20E3 dex L20E3: adc MemPtr bcc L20E8 inx ; Bump high byte L20E8: rts ; ------------------------------------------------------------------------- ; Subroutine to analyze an opcode byte in A. Will return a byte that ; encodes the mnemonic, and will set the number of bytes needed for this ; instruction in OperandLen AnalyzeOPCode: tay lsr a bcc L20F8 lsr a bcs L2107 cmp #$22 beq L2107 and #$07 ora #$80 L20F8: lsr a tax lda OffsetTab,x bcs L2103 lsr a lsr a lsr a lsr a L2103: and #$0F bne L210B L2107: ldy #$80 lda #$00 L210B: tax lda AdrFlagTab,x sta AdrFlagBuf and #$03 sta OperandLen tya and #$8F tax tya ldy #$03 cpx #$8A beq L212B L2120: lsr a bcc L212B lsr a L2124: lsr a ora #$20 dey bne L2124 iny L212B: dey bne L2120 rts ; ------------------------------------------------------------------------- ; Print the mnemonic with code in A (that code was returned by ; AnalyzeOpcode). PutMnemo: tay lda MnemoTab1,y sta MnemoBuf lda MnemoTab2,y sta MnemoBuf+1 L213A: lda #$00 ldy #$05 ; 3*5 bits in two bytes L213E: asl MnemoBuf+1 rol MnemoBuf rol a dey bne L213E adc #$3F jsr PutChar dex bne L213A jmp PutSpace
wagiminator/C64-Collection
1,254
C64_xu1541/software/tools/cc65-2.13.2/libsrc/dbg/dbgdump.s
; ; Ullrich von Bassewitz, 11.08.1998 ; ; char* DbgMemDump (unsigend Addr, char* Buf, unsigned char Length); ; .export _DbgMemDump .import addysp1 .import __hextab .importzp sp, tmp2, tmp3, tmp4, ptr3, ptr4 _DbgMemDump: ldy #0 lda (sp),y ; Get length sta tmp4 iny lda (sp),y ; Get the string buffer sta ptr3 iny lda (sp),y sta ptr3+1 iny lda (sp),y ; Get the address sta ptr4 iny lda (sp),y sta ptr4+1 jsr addysp1 ; Drop the parameters lda #0 sta tmp2 ; String index sta tmp3 ; Byte index ; Print the address lda ptr4+1 ; Hi address byte jsr dump ; Print address lda ptr4 ; Lo address byte jsr dump jsr putspace ; Add a space dump1: dec tmp4 ; Bytes left? bmi dump9 ; Jump if no jsr putspace ; Add a space ldy tmp3 inc tmp3 lda (ptr4),y jsr dump jmp dump1 dump9: lda #0 ldy tmp2 sta (ptr3),y ; Add string terminator lda ptr3 ldx ptr3+1 ; We assume this is not zero rts ; Dump one hex byte dump: pha lsr a lsr a lsr a lsr a tax lda __hextab,x jsr putc pla and #$0F tax lda __hextab,x putc: ldy tmp2 inc tmp2 sta (ptr3),y rts putspace: lda #$20 bne putc
wagiminator/C64-Collection
4,050
C64_xu1541/software/tools/cc65-2.13.2/libsrc/dbg/dbgsupp.s
; ; Ullrich von Bassewitz, 08.08.1998 ; ; Support routines for the debugger ; .export _DbgInit .export _DbgSP, _DbgCS, _DbgHI .import popax, return0, _DbgEntry, _set_brk, _end_brk .import _DbgBreaks .import _brk_pc .import __ZP_START__ ; Linker generated .include "zeropage.inc" ; C callable function, will install the debugger _DbgInit: lda #<DbgBreak ldx #>DbgBreak jmp _set_brk ; Entry for the break vector. DbgBreak: pla sta retsav pla sta retsav+1 cli tsx ; Stack pointer stx _DbgSP jsr DbgSwapZP ; Swap stuff lda #<DbgStack ; Set new stack sta sp lda #>DbgStack sta sp+1 jsr ResetDbgBreaks ; Reset temporary breakpoints jsr _DbgEntry ; Call C code jsr SetDbgBreaks ; Set temporary breakpoints jsr DbgSwapZP ; Swap stuff back lda retsav+1 pha lda retsav pha rts ; Stack used when in debugger mode .bss .res 256 DbgStack: ; Swap space for the the C temporaries CTemp: _DbgCS: .res 2 ; sp _DbgHI: .res 2 ; sreg .res (zpspace-4) ; Other stuff _DbgSP: .res 1 retsav: .res 2 ; Save buffer for return address .code ; Swap the C temporaries DbgSwapZP: ldy #zpspace-1 Swap1: ldx CTemp,y lda <__ZP_START__,y sta CTemp,y txa sta sp,y dey bpl Swap1 rts ; ---------------------------------------------------------------------------- ; Utility functions ; Set/reset the breakpoints. We must do that here since the breakpoints ; may be in the runtime stuff, causing the C part to fail before it has ; reset the breakpoints. See declaration of struct breakpoint in the C ; source MaxBreaks = 48 ; 4*12 ResetDbgBreaks: ldy #0 ldx #0 L4: lda _DbgBreaks+3,x ; Get bk_use beq L6 ; Jump if not set bpl L5 ; Jump if user breakpoint lda #0 sta _DbgBreaks+3,x ; Clear if temp breakpoint L5: lda _DbgBreaks+1,x ; PC hi sta ptr1+1 lda _DbgBreaks,x ; PC lo sta ptr1 lda _DbgBreaks+2,x ; Old OPC sta (ptr1),y ; Reset the breakpoint L6: inx inx inx inx cpx #MaxBreaks ; Done? bne L4 rts SetDbgBreaks: ldx #0 ldy #0 L7: lda _DbgBreaks+3,x ; Get bk_use beq L8 ; Jump if not set lda _DbgBreaks+1,x ; PC hi sta ptr1+1 lda _DbgBreaks,x ; PC lo sta ptr1 lda (ptr1),y ; Get the breakpoint OPC... sta _DbgBreaks+2,x ; ...and save it lda #$00 ; Load BRK opcode sta (ptr1),y L8: inx inx inx inx cpx #MaxBreaks ; Done? bne L7 rts ; Get a free breakpoint slot or return 0 .export _DbgGetBreakSlot _DbgGetBreakSlot: ldx #0 L10: lda _DbgBreaks+3,x ; Get bk_use beq L11 ; Jump if not set inx inx inx inx cpx #MaxBreaks ; Done? bne L10 jmp return0 ; No free slot L11: stx tmp1 lda #<_DbgBreaks ldx #>_DbgBreaks clc adc tmp1 bcc L12 inx L12: ldy #1 ; Force != 0 rts ; Check if a given address has a user breakpoint set, if found, return the ; slot, otherwise return 0. .export _DbgIsBreak _DbgIsBreak: jsr popax ; Get address sta ptr1 stx ptr1+1 ldx #0 L20: lda _DbgBreaks+3,x ; Get bk_use beq L21 ; Jump if not set bmi L21 ; Jump if temp breakpoint lda _DbgBreaks,x ; Low byte of address cmp ptr1 bne L21 lda _DbgBreaks+1,x ; High byte of address cmp ptr1+1 beq L22 L21: inx inx inx inx cpx #MaxBreaks ; Done? bne L20 jmp return0 ; Not found L22: stx tmp1 lda #<_DbgBreaks ldx #>_DbgBreaks clc adc tmp1 bcc L23 inx L23: ldy #1 ; Force != 0 rts
wagiminator/C64-Collection
3,085
C64_xu1541/software/tools/cc65-2.13.2/libsrc/conio/vcscanf.s
; ; int fastcall vcscanf(const char* format, va_list ap); ; ; 2005-01-02, Greg King ; .export _vcscanf .import _cgetc, _cputc .import popax, pushax, swapstk .include "../common/_scanf.inc" ; static bool pushed; ; static char back; ; .bss pushed: .res 1 back: .res 1 .code ; /* Call-back functions: ; ** (Note: These prototypes must NOT be declared with fastcall! They don't ; ** use (getfunc)'s and (ungetfunc)'s last parameter. Leaving it out of these ; ** prototypes makes more efficient code.) ; */ ; ---------------------------------------------------------------------------- ; /* Read a character from the console, and return it to an internal function */ ; static int get(void) { ; static char C; ; ; if (pushed) { ; pushed = false; ; return (int)back; ; } ; cputc(C = cgetc()); /* echo a typed character */ ; return (int)C; ; } ; get: ldx pushed beq L1 ; Return the old, pushed-back character (instead of getting a new one). ; dex ; ldx #>0 stx pushed lda back rts ; Directly read the keyboard. ; L1: jsr _cgetc ; Echo the character to the screen. ; pha jsr _cputc pla ldx #>0 rts ; ---------------------------------------------------------------------------- ; static int unget(int c) { ; pushed = true; ; return back = c; ; } ; unget: ldx #1 stx pushed jsr popax ; get the first argument sta back rts ; ---------------------------------------------------------------------------- ; int fastcall vcscanf(const char* format, va_list ap) { ; /* Initiate the data structure. ; ** Don't initiate the member that these conio functions don't use. ; */ ; static const struct scanfdata d = { ; ( getfunc) get, ; (ungetfunc)unget ; }; ; ; /* conio is very interactive. So, don't use any pushed-back character. ; ** Start fresh, each time that this function is called. ; */ ; pushed = false; ; ; /* Call the internal function, and return the result. */ ; return _scanf(&d, format, ap); ; } ; ; Beware: Because ap is a fastcall parameter, we must not destroy .XA. ; .proc _vcscanf ; ---------------------------------------------------------------------------- ; Static, constant scanfdata structure for the _vcscanf routine. ; .rodata d: .addr get ; SCANFDATA::GET .addr unget ; SCANFDATA::UNGET ; .addr 0 ; SCANFDATA::DATA (not used) .code pha ; Save low byte of ap txa pha ; Save high byte of ap ldx #0 stx pushed ; Put &d on the stack in front of the format pointer. lda #<d ldx #>d jsr swapstk ; Swap .XA with top-of-stack jsr pushax ; Put format pointer back on stack ; Restore ap, and jump to _scanf which will clean up the stack. pla tax pla jmp __scanf .endproc
wagiminator/C64-Collection
1,183
C64_xu1541/software/tools/cc65-2.13.2/libsrc/conio/cscanf.s
; ; int cscanf(const char* format, ...); ; ; 2000-12-01, Ullrich von Bassewitz ; 2005-01-01, Greg King ; .export _cscanf .import pushax, addysp, _vcscanf .macpack generic .include "zeropage.inc" ; ---------------------------------------------------------------------------- ; Code ; _cscanf: sty ArgSize ; Number of argument bytes passed in .Y dey ; subtract size of format pointer dey tya ; Now, calculate the va_list pointer -- which points to format. ldx sp+1 add sp bcc @L1 inx @L1: sta ptr1 stx ptr1+1 ; Push a copy of the format pointer onto the stack. ldy #1 lda (ptr1),y tax dey lda (ptr1),y jsr pushax ; Load va_list [last and __fastcall__ argument for vcscanf()]. lda ptr1 ldx ptr1+1 ; Call vcscanf(). jsr _vcscanf ; Clean up the stack. We will return what we got from vcscanf(). ldy ArgSize jmp addysp ; ---------------------------------------------------------------------------- ; Data ; .bss ArgSize: .res 1 ; Number of argument bytes
wagiminator/C64-Collection
2,959
C64_xu1541/software/tools/cc65-2.13.2/libsrc/conio/vcprintf.s
; ; int vcprintf (const char* Format, va_list ap); ; ; Ullrich von Bassewitz, 2.12.2000 ; .export _vcprintf .import pushax, popax .import __printf, _cputc .importzp sp, ptr1, ptr2, ptr3, tmp1 .macpack generic .data ; ---------------------------------------------------------------------------- ; ; Static data for the _vsprintf routine ; outdesc: ; Static outdesc structure .word 0 ; ccount .word out ; Output function pointer .word 0 ; ptr .word 0 ; uns .code ; ---------------------------------------------------------------------------- ; Callback routine used for the actual output. ; ; static void out (struct outdesc* d, const char* buf, unsigned count) ; /* Routine used for writing */ ; { ; /* Fast screen output */ ; d->ccount += count; ; while (count) { ; cputc (*buf); ; ++buf; ; --count; ; } ; } ; ; We're using ptr1 and tmp1, since we know that the cputc routine will not use ; them (they're also used in cputs, so they must be safe). out: jsr popax ; count sta ptr2 eor #$FF sta outdesc+6 txa sta ptr2+1 eor #$FF sta outdesc+7 jsr popax ; buf sta ptr1 stx ptr1+1 jsr popax ; d sta ptr3 stx ptr3+1 ; Sum up the total count of characters ldy #0 ; ccount in struct outdesc sty tmp1 ; Initialize tmp1 while we have zero available lda (ptr3),y add ptr2 sta (ptr3),y iny lda (ptr3),y adc ptr2+1 sta (ptr3),y ; Loop outputting characters @L1: inc outdesc+6 beq @L4 @L2: ldy tmp1 lda (ptr1),y iny bne @L3 inc ptr1+1 @L3: sty tmp1 jsr _cputc jmp @L1 @L4: inc outdesc+7 bne @L2 rts ; ---------------------------------------------------------------------------- ; vcprintf - formatted console i/o ; ; int vcprintf (const char* format, va_list ap) ; { ; struct outdesc d; ; ; /* Setup descriptor */ ; d.fout = out; ; ; /* Do formatting and output */ ; _printf (&d, format, ap); ; ; /* Return bytes written */ ; return d.ccount; ; } ; ; It is intentional that this function does not have __fastcall__ calling ; conventions - we need the space on the stack anyway, so there's nothing ; gained by using __fastcall__. _vcprintf: sta ptr1 ; Save ap stx ptr1+1 ; Setup the outdesc structure lda #0 sta outdesc sta outdesc+1 ; Clear ccount ; Get the format parameter and push it again ldy #1 lda (sp),y tax dey lda (sp),y jsr pushax ; Replace the passed format parameter on the stack by &d - this creates ; exactly the stack frame _printf expects. Parameters will get dropped ; by _printf. ldy #2 ; Low byte of d lda #<outdesc sta (sp),y iny lda #>outdesc sta (sp),y ; Restore ap and call _printf lda ptr1 ldx ptr1+1 jsr __printf ; Return the number of bytes written. lda outdesc ; ccount ldx outdesc+1 rts
wagiminator/C64-Collection
13,898
C64_xu1541/software/tools/cc65-2.13.2/libsrc/c128/c128-swlink.s
; ; Serial driver for the C128 using a Swiftlink or Turbo-232 cartridge. ; ; Ullrich von Bassewitz, 2003-04-18 ; ; The driver is based on the cc65 rs232 module, which in turn is based on ; Craig Bruce device driver for the Switftlink/Turbo-232. ; ; 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. ; .include "zeropage.inc" .include "ser-kernel.inc" .include "ser-error.inc" .include "c128.inc" ; ------------------------------------------------------------------------ ; Header. Includes jump table .segment "JUMPTABLE" ; Driver signature .byte $73, $65, $72 ; "ser" .byte SER_API_VERSION ; Serial API version number ; Jump table. .word INSTALL .word UNINSTALL .word OPEN .word CLOSE .word GET .word PUT .word STATUS .word IOCTL .word IRQ ;---------------------------------------------------------------------------- ; I/O definitions ACIA = $DE00 ACIA_DATA = ACIA+0 ; Data register ACIA_STATUS = ACIA+1 ; Status register ACIA_CMD = ACIA+2 ; Command register ACIA_CTRL = ACIA+3 ; Control register ;---------------------------------------------------------------------------- ; ; Global variables ; ; We reuse the RS232 zero page variables for the driver, since the ROM ; routines cannot be used together with this driver (may also use $A0F ; and following in case of problems). RecvHead := $A7 ; Head of receive buffer RecvTail := $A8 ; Tail of receive buffer RecvFreeCnt := $A9 ; Number of bytes in receive buffer SendHead := $AA ; Head of send buffer SendTail := $AB ; Tail of send buffer SendFreeCnt := $B4 ; Number of bytes free in send buffer Stopped := $B5 ; Flow-stopped flag RtsOff := $B6 ; ; Send and receive buffers: 256 bytes each RecvBuf := $0C00 ; Use the ROM buffers SendBuf := $0D00 .rodata ; Tables used to translate RS232 params into register values BaudTable: ; bit7 = 1 means setting is invalid .byte $FF ; SER_BAUD_45_5 .byte $FF ; SER_BAUD_50 .byte $FF ; SER_BAUD_75 .byte $FF ; SER_BAUD_110 .byte $FF ; SER_BAUD_134_5 .byte $02 ; SER_BAUD_150 .byte $05 ; SER_BAUD_300 .byte $06 ; SER_BAUD_600 .byte $07 ; SER_BAUD_1200 .byte $FF ; SER_BAUD_1800 .byte $08 ; SER_BAUD_2400 .byte $09 ; SER_BAUD_3600 .byte $0A ; SER_BAUD_4800 .byte $0B ; SER_BAUD_7200 .byte $0C ; SER_BAUD_9600 .byte $0E ; SER_BAUD_19200 .byte $0F ; SER_BAUD_38400 .byte $FF ; SER_BAUD_57600 .byte $FF ; SER_BAUD_115200 .byte $FF ; SER_BAUD_230400 BitTable: .byte $60 ; SER_BITS_5 .byte $40 ; SER_BITS_6 .byte $20 ; SER_BITS_7 .byte $00 ; SER_BITS_8 StopTable: .byte $00 ; SER_STOP_1 .byte $80 ; SER_STOP_2 ParityTable: .byte $00 ; SER_PAR_NONE .byte $20 ; SER_PAR_ODD .byte $60 ; SER_PAR_EVEN .byte $A0 ; SER_PAR_MARK .byte $E0 ; SER_PAR_SPACE .code ;---------------------------------------------------------------------------- ; Interrupt stub that is copied into low RAM. The startup code uses a special ; memory configuration with just kernal and I/O enabled (anything else is RAM). ; The NMI handler in ROM will switch back to a configuration where just the ; low 16K RAM are accessible. So we have to copy a smal piece of code into ; low RAM that enables the cc65 configuration and then jumps to the real NMI ; handler. NmiStubOrig := * .org $1150 ; BASIC graphics area .proc NmiStub lda #MMU_CFG_CC65 ; Bank 0 with kernal ROM... sta MMU_CR ; ...enable jsr NmiHandler ; Call the actual NMI handler lda #$00 ; Get ROM config... sta MMU_CR ; ...and enable it Vector := *+1 .byte $4C ; Jump to the saved IRQ vector .endproc .reloc ;---------------------------------------------------------------------------- ; INSTALL routine. Is called after the driver is loaded into memory. If ; possible, check if the hardware is present. ; Must return an SER_ERR_xx code in a/x. INSTALL: ; Deactivate DTR and disable 6551 interrupts lda #%00001010 sta ACIA_CMD ; Copy the NMI stub into low memory ldy #.sizeof (NmiStub)-1 @L1: lda NmiStubOrig,y sta NmiStub,y dey bpl @L1 ; Set up the nmi vector lda NMIVec ldy NMIVec+1 sta NmiStub::Vector+0 sty NmiStub::Vector+1 lda #<NmiStub ldy #>NmiStub SetNMI: sta NMIVec sty NMIVec+1 ; Done, return an error code lda #<SER_ERR_OK tax ; A is zero rts ;---------------------------------------------------------------------------- ; UNINSTALL routine. Is called before the driver is removed from memory. ; Must return an SER_ERR_xx code in a/x. UNINSTALL: ; Stop interrupts, drop DTR lda #%00001010 sta ACIA_CMD ; Restore NMI vector and return OK lda NmiStub::Vector+0 ldy NmiStub::Vector+1 jmp SetNMI ;---------------------------------------------------------------------------- ; PARAMS routine. A pointer to a ser_params structure is passed in ptr1. ; Must return an SER_ERR_xx code in a/x. OPEN: ; Check if the handshake setting is valid ldy #SER_PARAMS::HANDSHAKE ; Handshake lda (ptr1),y cmp #SER_HS_HW ; This is all we support bne InvParam ; Initialize buffers jsr InitBuffers ; Set the value for the control register, which contains stop bits, word ; length and the baud rate. ldy #SER_PARAMS::BAUDRATE lda (ptr1),y ; Baudrate index tay lda BaudTable,y ; Get 6551 value bmi InvBaud ; Branch if rate not supported sta tmp1 ldy #SER_PARAMS::DATABITS ; Databits lda (ptr1),y tay lda BitTable,y ora tmp1 sta tmp1 ldy #SER_PARAMS::STOPBITS ; Stopbits lda (ptr1),y tay lda StopTable,y ora tmp1 ora #%00010000 ; Receiver clock source = baudrate sta ACIA_CTRL ; Set the value for the command register. We remember the base value in ; RtsOff, since we will have to manipulate ACIA_CMD often. ldy #SER_PARAMS::PARITY ; Parity lda (ptr1),y tay lda ParityTable,y ora #%00000001 ; DTR active sta RtsOff ora #%00001000 ; Enable receive interrupts sta ACIA_CMD ; Done lda #<SER_ERR_OK tax ; A is zero rts ; Invalid parameter InvParam: lda #<SER_ERR_INIT_FAILED ldx #>SER_ERR_INIT_FAILED rts ; Baud rate not available InvBaud: lda #<SER_ERR_BAUD_UNAVAIL ldx #>SER_ERR_BAUD_UNAVAIL rts ;---------------------------------------------------------------------------- ; CLOSE: Close the port, disable interrupts and flush the buffer. Called ; without parameters. Must return an error code in a/x. ; CLOSE: ; Stop interrupts, drop DTR lda #%00001010 sta ACIA_CMD ; Initalize buffers. Returns zero in a jsr InitBuffers ; Return OK lda #<SER_ERR_OK tax ; A is zero rts ;---------------------------------------------------------------------------- ; GET: Will fetch a character from the receive buffer and store it into the ; variable pointer to by ptr1. If no data is available, SER_ERR_NO_DATA is ; return. ; GET: ldx SendFreeCnt ; Send data if necessary inx ; X == $FF? beq @L1 lda #$00 jsr TryToSend ; Check for buffer empty @L1: lda RecvFreeCnt ; (25) cmp #$ff bne @L2 lda #<SER_ERR_NO_DATA ldx #>SER_ERR_NO_DATA rts ; Check for flow stopped & enough free: release flow control @L2: ldx Stopped ; (34) beq @L3 cmp #63 bcc @L3 lda #$00 sta Stopped lda RtsOff ora #%00001000 sta ACIA_CMD ; Get byte from buffer @L3: ldx RecvHead ; (41) lda RecvBuf,x inc RecvHead inc RecvFreeCnt ldx #$00 ; (59) sta (ptr1,x) txa ; Return code = 0 rts ;---------------------------------------------------------------------------- ; PUT: Output character in A. ; Must return an error code in a/x. ; PUT: ; Try to send ldx SendFreeCnt inx ; X = $ff? beq @L2 pha lda #$00 jsr TryToSend pla ; Put byte into send buffer & send @L2: ldx SendFreeCnt bne @L3 lda #<SER_ERR_OVERFLOW ; X is already zero rts @L3: ldx SendTail sta SendBuf,x inc SendTail dec SendFreeCnt lda #$ff jsr TryToSend lda #<SER_ERR_OK tax rts ;---------------------------------------------------------------------------- ; STATUS: Return the status in the variable pointed to by ptr1. ; Must return an error code in a/x. ; STATUS: lda ACIA_STATUS ldx #0 sta (ptr1,x) txa ; SER_ERR_OK rts ;---------------------------------------------------------------------------- ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl ; specific data in ptr1, and the ioctl code in A. ; Must return an error code in a/x. ; IOCTL: lda #<SER_ERR_INV_IOCTL ; We don't support ioclts for now ldx #>SER_ERR_INV_IOCTL rts ;---------------------------------------------------------------------------- ; IRQ: Not used on the C128 ; IRQ = $0000 ;---------------------------------------------------------------------------- ; ; 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! ; ; Note: Because of the C128 banking, a small stub has to go into low memory, ; since the ROM NMI entry point switches to a configuration, where only the ; low 16K of RAM are visible. The entry code switches into the standard cc65 ; configuration (I/O + 16K kernal) and then jumps here. Registers are already ; saved by the ROM code. NmiHandler: lda ACIA_STATUS ;(4) ;status ;check for byte received and #$08 ;(2) beq @L9 ;(2*) cld lda ACIA_DATA ;(4) data ;get byte and put into receive buffer ldy RecvTail ;(4) ldx RecvFreeCnt ;(4) beq @L9 ;(2*) Jump if no space in receive buffer sta RecvBuf,y ;(5) inc RecvTail ;(6) dec RecvFreeCnt ;(6) cpx #33 ;(2) check for buffer space low bcc @L2 ;(2*) rts ; Assert flow control @L2: lda RtsOff ;(3) assert flow control if buffer space too low sta ACIA_CMD ;(4) command sta Stopped ;(3) @L9: rts ;---------------------------------------------------------------------------- ; Try to send a byte. Internal routine. A = TryHard .proc 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_STATUS 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_DATA inc SendHead inc SendFreeCnt jmp @L0 .endproc ;---------------------------------------------------------------------------- ; Initialize buffers InitBuffers: ldx #0 stx Stopped stx RecvHead stx RecvTail stx SendHead stx SendTail dex ; X = 255 stx RecvFreeCnt stx SendFreeCnt rts
wagiminator/C64-Collection
6,564
C64_xu1541/software/tools/cc65-2.13.2/libsrc/c128/c128-ramcart.s
; ; Extended memory driver for the RamCart 64/128KB cartridge. Driver works ; without problems when statically linked. ; Code is based on GEORAM code by Ullrich von Bassewitz. ; Maciej 'YTM/Elysium' Witkowiak <ytm@elysium.pl> ; 06,22.12.2002 ; .include "zeropage.inc" .include "em-kernel.inc" .include "em-error.inc" .macpack generic ; ------------------------------------------------------------------------ ; Header. Includes jump table .segment "JUMPTABLE" ; Driver signature .byte $65, $6d, $64 ; "emd" .byte EMD_API_VERSION ; EM API version number ; Jump table. .word INSTALL .word UNINSTALL .word PAGECOUNT .word MAP .word USE .word COMMIT .word COPYFROM .word COPYTO ; ------------------------------------------------------------------------ ; Constants RAMC_WINDOW = $DF00 ; Address of RamCart window RAMC_PAGE_LO = $DE00 ; Page register low RAMC_PAGE_HI = $DE01 ; Page register high (only for RC128) ; ------------------------------------------------------------------------ ; Data. .bss pagecount: .res 2 ; Number of pages available .code ; ------------------------------------------------------------------------ ; INSTALL routine. Is called after the driver is loaded into memory. If ; possible, check if the hardware is present and determine the amount of ; memory available. ; Must return an EM_ERR_xx code in a/x. ; INSTALL: ldx RAMC_WINDOW cpx RAMC_WINDOW bne @notpresent lda #0 sta RAMC_PAGE_LO sta RAMC_PAGE_HI ldx RAMC_WINDOW cpx RAMC_WINDOW bne @notpresent lda #2 sta RAMC_WINDOW cmp RAMC_WINDOW beq @cont cpx RAMC_WINDOW beq @readonly @cont: ldy #1 sty RAMC_PAGE_HI sty RAMC_WINDOW dey sty RAMC_PAGE_HI iny cpy RAMC_WINDOW beq @rc64 ; we're on rc128 ldx #>512 bne @setsize @rc64: ldx #>256 @setsize: lda #0 sta pagecount stx pagecount+1 lda #<EM_ERR_OK ldx #>EM_ERR_OK rts @notpresent: @readonly: lda #<EM_ERR_NO_DEVICE ldx #>EM_ERR_NO_DEVICE ; rts ; Run into UNINSTALL instead ; ------------------------------------------------------------------------ ; UNINSTALL routine. Is called before the driver is removed from memory. ; Can do cleanup or whatever. Must not return anything. ; UNINSTALL: rts ; ------------------------------------------------------------------------ ; PAGECOUNT: Return the total number of available pages in a/x. ; PAGECOUNT: lda pagecount ldx pagecount+1 rts ; ------------------------------------------------------------------------ ; USE: Tell the driver that the window is now associated with a given page. ; The RamCart cartridge does not copy but actually map the window, so USE is ; identical to MAP. USE = MAP ; ------------------------------------------------------------------------ ; MAP: Map the page in a/x into memory and return a pointer to the page in ; a/x. The contents of the currently mapped page (if any) may be discarded ; by the driver. ; MAP: sta RAMC_PAGE_LO stx RAMC_PAGE_HI lda #<RAMC_WINDOW ldx #>RAMC_WINDOW ; Use the RTS from COMMIT below to save a precious byte of storage ; ------------------------------------------------------------------------ ; COMMIT: Commit changes in the memory window to extended storage. COMMIT: rts ; ------------------------------------------------------------------------ ; COPYFROM: Copy from extended into linear memory. A pointer to a structure ; describing the request is passed in a/x. ; The function must not return anything. ; COPYFROM: jsr setup ; Setup is: ; ; - ptr1 contains the struct pointer ; - ptr2 contains the linear memory buffer ; - ptr3 contains -(count-1) ; - tmp1 contains the low page register value ; - tmp2 contains the high page register value ; - X contains the page offset ; - Y contains zero jmp @L5 @L1: lda RAMC_WINDOW,x sta (ptr2),y iny bne @L2 inc ptr2+1 @L2: inx beq @L4 ; Bump count and repeat @L3: inc ptr3 bne @L1 inc ptr3+1 bne @L1 rts ; Bump page register @L4: inc tmp1 bne @L5 inc tmp2 @L5: lda tmp1 sta RAMC_PAGE_LO lda tmp2 sta RAMC_PAGE_HI jmp @L3 ; ------------------------------------------------------------------------ ; COPYTO: Copy from linear into extended memory. A pointer to a structure ; describing the request is passed in a/x. ; The function must not return anything. ; COPYTO: jsr setup ; Setup is: ; ; - ptr1 contains the struct pointer ; - ptr2 contains the linear memory buffer ; - ptr3 contains -(count-1) ; - tmp1 contains the low page register value ; - tmp2 contains the high page register value ; - X contains the page offset ; - Y contains zero jmp @L5 @L1: lda (ptr2),y sta RAMC_WINDOW,x iny bne @L2 inc ptr2+1 @L2: inx beq @L4 ; Bump count and repeat @L3: inc ptr3 bne @L1 inc ptr3+1 bne @L1 rts ; Bump page register @L4: inc tmp1 bne @L5 inc tmp2 @L5: lda tmp1 sta RAMC_PAGE_LO lda tmp2 sta RAMC_PAGE_HI jmp @L3 ; ------------------------------------------------------------------------ ; Helper function for COPYFROM and COPYTO: Store the pointer to the request ; structure and prepare data for the copy setup: sta ptr1 stx ptr1+1 ; Save passed pointer ; Get the page number from the struct and adjust it so that it may be used ; with the hardware. That is: lower 6 bits in tmp1, high bits in tmp2. ldy #EM_COPY::PAGE+1 lda (ptr1),y sta tmp2 dey lda (ptr1),y sta tmp1 ; Get the buffer pointer into ptr2 ldy #EM_COPY::BUF lda (ptr1),y sta ptr2 iny lda (ptr1),y sta ptr2+1 ; Get the count, calculate -(count-1) and store it into ptr3 ldy #EM_COPY::COUNT lda (ptr1),y eor #$FF sta ptr3 iny lda (ptr1),y eor #$FF sta ptr3+1 ; Get the page offset into X and clear Y ldy #EM_COPY::OFFS lda (ptr1),y tax ldy #$00 ; Done rts
wagiminator/C64-Collection
8,471
C64_xu1541/software/tools/cc65-2.13.2/libsrc/c128/c128-potmouse.s
; ; Driver for a potentiometer "mouse" e.g. Koala Pad ; ; Ullrich von Bassewitz, 2004-03-29, 2009-09-26 ; Stefan Haubenthal, 2006-08-20 ; .include "zeropage.inc" .include "mouse-kernel.inc" .include "c128.inc" .macpack generic ; ------------------------------------------------------------------------ ; Header. Includes jump table .segment "JUMPTABLE" HEADER: ; Driver signature .byte $6d, $6f, $75 ; "mou" .byte MOUSE_API_VERSION ; Mouse driver API version number ; Jump table. .addr INSTALL .addr UNINSTALL .addr HIDE .addr SHOW .addr SETBOX .addr GETBOX .addr MOVE .addr BUTTONS .addr POS .addr INFO .addr IOCTL .addr IRQ ; Callback table, set by the kernel before INSTALL is called CHIDE: jmp $0000 ; Hide the cursor CSHOW: jmp $0000 ; Show the cursor CMOVEX: jmp $0000 ; Move the cursor to X coord CMOVEY: jmp $0000 ; Move the cursor to Y coord ;---------------------------------------------------------------------------- ; Constants SCREEN_HEIGHT = 200 SCREEN_WIDTH = 320 .enum JOY UP = $01 DOWN = $02 LEFT = $04 RIGHT = $08 FIRE = $10 .endenum ;---------------------------------------------------------------------------- ; Global variables. The bounding box values are sorted so that they can be ; written with the least effort in the SETBOX and GETBOX routines, so don't ; reorder them. .bss Vars: YPos: .res 2 ; Current mouse position, Y XPos: .res 2 ; Current mouse position, X 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 Buttons: .res 1 ; Button mask ; Temporary value used in the int handler Temp: .res 1 ; Default values for above variables .rodata .proc DefVars .word SCREEN_HEIGHT/2 ; YPos .word SCREEN_WIDTH/2 ; XPos .word 0 ; XMin .word 0 ; YMin .word SCREEN_WIDTH ; XMax .word SCREEN_HEIGHT ; YMax .byte 0 ; Buttons .endproc .code ;---------------------------------------------------------------------------- ; INSTALL routine. Is called after the driver is loaded into memory. If ; possible, check if the hardware is present. ; Must return an MOUSE_ERR_xx code in a/x. INSTALL: ; Initialize variables. Just copy the default stuff over ldx #.sizeof(DefVars)-1 @L1: lda DefVars,x sta Vars,x dex bpl @L1 ; Be sure the mouse cursor is invisible and at the default location. We ; need to do that here, because our mouse interrupt handler doesn't set the ; mouse position if it hasn't changed. sei jsr CHIDE lda XPos ldx XPos+1 jsr CMOVEX lda YPos ldx YPos+1 jsr CMOVEY cli ; Done, return zero (= MOUSE_ERR_OK) ldx #$00 txa rts ;---------------------------------------------------------------------------- ; UNINSTALL routine. Is called before the driver is removed from memory. ; No return code required (the driver is removed from memory on return). UNINSTALL = HIDE ; Hide cursor on exit ;---------------------------------------------------------------------------- ; HIDE routine. Is called to hide the mouse pointer. The mouse kernel manages ; a counter for calls to show/hide, and the driver entry point is only called ; if the mouse is currently visible and should get hidden. For most drivers, ; no special action is required besides hiding the mouse cursor. ; No return code required. HIDE: sei jsr CHIDE cli rts ;---------------------------------------------------------------------------- ; SHOW routine. Is called to show the mouse pointer. The mouse kernel manages ; a counter for calls to show/hide, and the driver entry point is only called ; if the mouse is currently hidden and should become visible. For most drivers, ; no special action is required besides enabling the mouse cursor. ; No return code required. SHOW: sei jsr CSHOW cli rts ;---------------------------------------------------------------------------- ; SETBOX: Set the mouse bounding box. The parameters are passed as they come ; from the C program, that is, a pointer to a mouse_box struct in a/x. ; No checks are done if the mouse is currently inside the box, this is the job ; of the caller. It is not necessary to validate the parameters, trust the ; caller and save some code here. No return code required. SETBOX: sta ptr1 stx ptr1+1 ; Save data pointer ldy #.sizeof (MOUSE_BOX)-1 sei @L1: lda (ptr1),y sta XMin,y dey bpl @L1 cli rts ;---------------------------------------------------------------------------- ; GETBOX: Return the mouse bounding box. The parameters are passed as they ; come from the C program, that is, a pointer to a mouse_box struct in a/x. GETBOX: sta ptr1 stx ptr1+1 ; Save data pointer ldy #.sizeof (MOUSE_BOX)-1 sei @L1: lda XMin,y sta (ptr1),y dey bpl @L1 cli rts ;---------------------------------------------------------------------------- ; MOVE: Move the mouse to a new position. The position is passed as it comes ; from the C program, that is: X on the stack and Y in a/x. The C wrapper will ; remove the parameter from the stack on return. ; No checks are done if the new position is valid (within the bounding box or ; the screen). No return code required. ; MOVE: sei ; No interrupts sta YPos stx YPos+1 ; New Y position jsr CMOVEY ; Set it ldy #$01 lda (sp),y sta XPos+1 tax dey lda (sp),y sta XPos ; New X position jsr CMOVEX ; Move the cursor cli ; Allow interrupts rts ;---------------------------------------------------------------------------- ; BUTTONS: Return the button mask in a/x. BUTTONS: lda Buttons ldx #$00 rts ;---------------------------------------------------------------------------- ; POS: Return the mouse position in the MOUSE_POS struct pointed to by ptr1. ; No return code required. POS: ldy #MOUSE_POS::XCOORD ; Structure offset sei ; Disable interrupts lda XPos ; Transfer the position sta (ptr1),y lda XPos+1 iny sta (ptr1),y lda YPos iny sta (ptr1),y lda YPos+1 cli ; Enable interrupts iny sta (ptr1),y ; Store last byte rts ; Done ;---------------------------------------------------------------------------- ; INFO: Returns mouse position and current button mask in the MOUSE_INFO ; struct pointed to by ptr1. No return code required. ; ; We're cheating here to keep the code smaller: The first fields of the ; mouse_info struct are identical to the mouse_pos struct, so we will just ; call _mouse_pos to initialize the struct pointer and fill the position ; fields. INFO: jsr POS ; Fill in the button state lda Buttons ldy #MOUSE_INFO::BUTTONS sta (ptr1),y rts ;---------------------------------------------------------------------------- ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl ; specific data in ptr1, and the ioctl code in A. ; Must return an error code in a/x. ; IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioclts for now ldx #>MOUSE_ERR_INV_IOCTL rts ;---------------------------------------------------------------------------- ; IRQ: Irq handler entry point. Called as a subroutine but in IRQ context ; (so be careful). ; IRQ: lda #$7F sta CIA1_PRA lda CIA1_PRB ; Read port #1 and #%00001100 eor #%00001100 ; Make all bits active high asl sta Buttons lsr lsr lsr and #%00000001 ora Buttons sta Buttons ldx #%01000000 stx CIA1_PRA ldy #0 : dey bne :- ldx SID_ADConv1 stx XPos ldx SID_ADConv2 stx YPos lda #$FF tax bne @AddX ; Branch always lda #$01 ldx #$00 ; Calculate the new X coordinate (--> a/y) @AddX: add XPos tay ; Remember low byte txa adc XPos+1 tax ; Limit the X coordinate to the bounding box cpy XMin sbc XMin+1 bpl @L1 ldy XMin ldx XMin+1 jmp @L2 @L1: txa cpy XMax sbc XMax+1 bmi @L2 ldy XMax ldx XMax+1 @L2: sty XPos stx XPos+1 ; Move the mouse pointer to the new X pos tya jsr CMOVEX lda #$FF tax bne @AddY @Down: lda #$01 ldx #$00 ; Calculate the new Y coordinate (--> a/y) @AddY: add YPos tay ; Remember low byte txa adc YPos+1 tax ; Limit the Y coordinate to the bounding box cpy YMin sbc YMin+1 bpl @L3 ldy YMin ldx YMin+1 jmp @L4 @L3: txa cpy YMax sbc YMax+1 bmi @L4 ldy YMax ldx YMax+1 @L4: sty YPos stx YPos+1 ; Move the mouse pointer to the new X pos tya jmp CMOVEY
wagiminator/C64-Collection
2,023
C64_xu1541/software/tools/cc65-2.13.2/libsrc/c128/color.s
; ; 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 .import return0 .include "c128.inc" _textcolor: bit MODE ; Check 80/40 column mode bmi @L1 ; Jump if 80 columns ; 40 column mode ldx CHARCOLOR ; Get the old color sta CHARCOLOR ; Set the new color txa ; Old color -> A ldx #$00 ; Load high byte rts ; 80 column mode @L1: tax ; Move new color to X lda CHARCOLOR ; Get old color + attributes and #$F0 ; Keep old attributes ora $CE5C,x ; Translate VIC color -> VDC color ldx CHARCOLOR ; Get the old color sta CHARCOLOR ; Set the new color + old attributes txa ; Old color -> A and #$0F ; Mask out attributes ldx #$00 ; Load high byte ; translate vdc->vic colour vdctovic: ldy #16 @L2: cmp $CE5C-1,y beq @L3 dey bne @L2 @L3: tya rts _bgcolor: bit MODE bmi @L1 ; 40 column mode ldx VIC_BG_COLOR0 ; get old value sta VIC_BG_COLOR0 ; set new value txa ldx #$00 rts ; 80 column mode @L1: tax ; Move new color to X lda $CE5C,x ; Translate VIC color -> VDC color pha ldx #26 jsr $CDDA ; Read vdc register 26 jsr vdctovic tay pla ldx #26 jsr $CDCC ; Write vdc register 26 tya ldx #$00 rts _bordercolor: bit MODE bmi @L1 ; 40 column mode ldx VIC_BORDERCOLOR ; get old value sta VIC_BORDERCOLOR ; set new value txa ldx #$00 rts ; 80 column mode @L1: jmp return0
wagiminator/C64-Collection
11,372
C64_xu1541/software/tools/cc65-2.13.2/libsrc/c128/c128-1351.s
; ; Driver for the 1351 proportional mouse. Parts of the code are from ; the Commodore 1351 mouse users guide. ; ; Ullrich von Bassewitz, 2003-12-29, 2009-09-26 ; .include "zeropage.inc" .include "mouse-kernel.inc" .include "c128.inc" .macpack generic ; ------------------------------------------------------------------------ ; Header. Includes jump table .segment "JUMPTABLE" HEADER: ; Driver signature .byte $6d, $6f, $75 ; "mou" .byte MOUSE_API_VERSION ; Mouse driver API version number ; Jump table. .addr INSTALL .addr UNINSTALL .addr HIDE .addr SHOW .addr SETBOX .addr GETBOX .addr MOVE .addr BUTTONS .addr POS .addr INFO .addr IOCTL .addr IRQ ; Mouse driver flags .byte MOUSE_FLAG_LATE_IRQ ; Callback table, set by the kernel before INSTALL is called CHIDE: jmp $0000 ; Hide the cursor CSHOW: jmp $0000 ; Show the cursor CMOVEX: jmp $0000 ; Move the cursor to X coord CMOVEY: jmp $0000 ; Move the cursor to Y coord ;---------------------------------------------------------------------------- ; Constants SCREEN_HEIGHT = 200 SCREEN_WIDTH = 320 ;---------------------------------------------------------------------------- ; Global variables. The bounding box values are sorted so that they can be ; written with the least effort in the SETBOX and GETBOX routines, so don't ; reorder them. .bss Vars: OldPotX: .res 1 ; Old hw counter values OldPotY: .res 1 YPos: .res 2 ; Current mouse position, Y XPos: .res 2 ; Current mouse position, X 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 OldValue: .res 1 ; Temp for MoveCheck routine NewValue: .res 1 ; Temp for MoveCheck routine ; Default values for above variables .rodata .proc DefVars .byte 0, 0 ; OldPotX/OldPotY .word SCREEN_HEIGHT/2 ; YPos .word SCREEN_WIDTH/2 ; XPos .word 0 ; XMin .word 0 ; YMin .word SCREEN_WIDTH ; XMax .word SCREEN_HEIGHT ; YMax .endproc .code ;---------------------------------------------------------------------------- ; INSTALL routine. Is called after the driver is loaded into memory. If ; possible, check if the hardware is present. ; Must return an MOUSE_ERR_xx code in a/x. INSTALL: ; Initialize variables. Just copy the default stuff over ldx #.sizeof(DefVars)-1 @L1: lda DefVars,x sta Vars,x dex bpl @L1 ; Be sure the mouse cursor is invisible and at the default location. We ; need to do that here, because our mouse interrupt handler doesn't set the ; mouse position if it hasn't changed. sei jsr CHIDE lda XPos ldx XPos+1 jsr CMOVEX lda YPos ldx YPos+1 jsr CMOVEY cli ; Done, return zero (= MOUSE_ERR_OK) ldx #$00 txa rts ; Run into UNINSTALL instead ;---------------------------------------------------------------------------- ; UNINSTALL routine. Is called before the driver is removed from memory. ; No return code required (the driver is removed from memory on return). UNINSTALL = HIDE ; Hide cursor on exit ;---------------------------------------------------------------------------- ; HIDE routine. Is called to hide the mouse pointer. The mouse kernel manages ; a counter for calls to show/hide, and the driver entry point is only called ; if the mouse is currently visible and should get hidden. For most drivers, ; no special action is required besides hiding the mouse cursor. ; No return code required. HIDE: sei jsr CHIDE cli rts ;---------------------------------------------------------------------------- ; SHOW routine. Is called to show the mouse pointer. The mouse kernel manages ; a counter for calls to show/hide, and the driver entry point is only called ; if the mouse is currently hidden and should become visible. For most drivers, ; no special action is required besides enabling the mouse cursor. ; No return code required. SHOW: sei jsr CSHOW cli rts ;---------------------------------------------------------------------------- ; SETBOX: Set the mouse bounding box. The parameters are passed as they come ; from the C program, that is, a pointer to a mouse_box struct in a/x. ; No checks are done if the mouse is currently inside the box, this is the job ; of the caller. It is not necessary to validate the parameters, trust the ; caller and save some code here. No return code required. SETBOX: sta ptr1 stx ptr1+1 ; Save data pointer ldy #.sizeof (MOUSE_BOX)-1 sei @L1: lda (ptr1),y sta XMin,y dey bpl @L1 cli rts ;---------------------------------------------------------------------------- ; GETBOX: Return the mouse bounding box. The parameters are passed as they ; come from the C program, that is, a pointer to a mouse_box struct in a/x. GETBOX: sta ptr1 stx ptr1+1 ; Save data pointer ldy #.sizeof (MOUSE_BOX)-1 sei @L1: lda XMin,y sta (ptr1),y dey bpl @L1 cli rts ;---------------------------------------------------------------------------- ; MOVE: Move the mouse to a new position. The position is passed as it comes ; from the C program, that is: X on the stack and Y in a/x. The C wrapper will ; remove the parameter from the stack on return. ; No checks are done if the new position is valid (within the bounding box or ; the screen). No return code required. ; MOVE: sei ; No interrupts sta YPos stx YPos+1 ; New Y position jsr CMOVEY ; Set it ldy #$01 lda (sp),y sta XPos+1 tax dey lda (sp),y sta XPos ; New X position jsr CMOVEX ; Move the cursor cli ; Allow interrupts rts ;---------------------------------------------------------------------------- ; BUTTONS: Return the button mask in a/x. BUTTONS: lda #$7F sei sta CIA1_PRA lda CIA1_PRB ; Read joystick #0 cli ldx #0 and #$1F eor #$1F rts ;---------------------------------------------------------------------------- ; POS: Return the mouse position in the MOUSE_POS struct pointed to by ptr1. ; No return code required. POS: ldy #MOUSE_POS::XCOORD ; Structure offset sei ; Disable interrupts lda XPos ; Transfer the position sta (ptr1),y lda XPos+1 iny sta (ptr1),y lda YPos iny sta (ptr1),y lda YPos+1 cli ; Enable interrupts iny sta (ptr1),y ; Store last byte rts ; Done ;---------------------------------------------------------------------------- ; INFO: Returns mouse position and current button mask in the MOUSE_INFO ; struct pointed to by ptr1. No return code required. ; ; We're cheating here to keep the code smaller: The first fields of the ; mouse_info struct are identical to the mouse_pos struct, so we will just ; call _mouse_pos to initialize the struct pointer and fill the position ; fields. INFO: jsr POS ; Fill in the button state jsr BUTTONS ; Will not touch ptr1 ldy #MOUSE_INFO::BUTTONS sta (ptr1),y rts ;---------------------------------------------------------------------------- ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl ; specific data in ptr1, and the ioctl code in A. ; Must return an error code in a/x. ; IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioclts for now ldx #>MOUSE_ERR_INV_IOCTL rts ;---------------------------------------------------------------------------- ; IRQ: Irq handler entry point. Called as a subroutine but in IRQ context ; (so be careful). The routine MUST return carry set if the interrupt has been ; 'handled' - which means that the interrupt source is gone. Otherwise it ; MUST return carry clear. ; IRQ: lda SID_ADConv1 ; Get mouse X movement ldy OldPotX jsr MoveCheck ; Calculate movement vector sty OldPotX ; Skip processing if nothing has changed bcc @SkipX ; Calculate the new X coordinate (--> a/y) add XPos tay ; Remember low byte txa adc XPos+1 tax ; Limit the X coordinate to the bounding box cpy XMin sbc XMin+1 bpl @L1 ldy XMin ldx XMin+1 jmp @L2 @L1: txa cpy XMax sbc XMax+1 bmi @L2 ldy XMax ldx XMax+1 @L2: sty XPos stx XPos+1 ; Move the mouse pointer to the new X pos tya jsr CMOVEX ; Calculate the Y movement vector @SkipX: lda SID_ADConv2 ; Get mouse Y movement ldy OldPotY jsr MoveCheck ; Calculate movement sty OldPotY ; Skip processing if nothing has changed bcc @SkipY ; Calculate the new Y coordinate (--> a/y) sta OldValue lda YPos sub OldValue tay stx OldValue lda YPos+1 sbc OldValue tax ; Limit the Y coordinate to the bounding box cpy YMin sbc YMin+1 bpl @L3 ldy YMin ldx YMin+1 jmp @L4 @L3: txa cpy YMax sbc YMax+1 bmi @L4 ldy YMax ldx YMax+1 @L4: sty YPos stx YPos+1 ; Move the mouse pointer to the new X pos tya jsr CMOVEY ; Done clc ; Interrupt not "handled" @SkipY: rts ; -------------------------------------------------------------------------- ; ; 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 sub OldValue ; a = mod64 (new - old) and #%01111111 cmp #%01000000 ; if (a > 0) bcs @L1 ; lsr a ; a /= 2; beq @L2 ; if (a != 0) ldy NewValue ; y = NewValue sec rts ; return @L1: ora #%11000000 ; else or in high order bits cmp #$FF ; if (a != -1) beq @L2 sec ror a ; a /= 2 dex ; high byte = -1 (X = $FF) ldy NewValue sec rts @L2: txa ; A = $00 clc rts
wagiminator/C64-Collection
6,848
C64_xu1541/software/tools/cc65-2.13.2/libsrc/c128/c128-reu.s
; ; Extended memory driver for the Commodore REU. Driver works without ; problems when statically linked. ; ; Ullrich von Bassewitz, 2002-11-29 ; .include "zeropage.inc" .include "em-kernel.inc" .include "em-error.inc" .include "c128.inc" .macpack generic ; ------------------------------------------------------------------------ ; Header. Includes jump table .segment "JUMPTABLE" ; Driver signature .byte $65, $6d, $64 ; "emd" .byte EMD_API_VERSION ; EM API version number ; Jump table. .word INSTALL .word UNINSTALL .word PAGECOUNT .word MAP .word USE .word COMMIT .word COPYFROM .word COPYTO ; ------------------------------------------------------------------------ ; Constants REU_STATUS = $DF00 ; Status register REU_COMMAND = $DF01 ; Command register REU_C64ADDR = $DF02 ; C64 base address register REU_REUADDR = $DF04 ; REU base address register REU_COUNT = $DF07 ; Transfer count register REU_IRQMASK = $DF09 ; IRQ mask register REU_CONTROL = $DF0A ; Control register REU_TRIGGER = $FF00 ; REU command trigger OP_COPYFROM = $ED OP_COPYTO = $EC ; ------------------------------------------------------------------------ ; Data. .bss pagecount: .res 2 ; Number of pages available curpage: .res 2 ; Current page number window: .res 256 ; Memory "window" reu_params: .word $0000 ; Host address, lo, hi .word $0000 ; Exp address, lo, hi .byte $00 ; Expansion bank no. .word $0000 ; # bytes to move, lo, hi .byte $00 ; Interrupt mask reg. .byte $00 ; Adress control reg. .code ; ------------------------------------------------------------------------ ; INSTALL routine. Is called after the driver is loaded into memory. If ; possible, check if the hardware is present and determine the amount of ; memory available. ; Must return an EM_ERR_xx code in a/x. ; INSTALL: ldx #$00 ; High byte of return code lda #$55 sta REU_REUADDR cmp REU_REUADDR ; Check for presence of REU bne nodevice asl a ; A = $AA sta REU_REUADDR cmp REU_REUADDR ; Check for presence of REU bne nodevice ldy #>(128*4) ; Assume 128KB lda REU_STATUS and #$10 ; Check size bit beq @L1 ldy #>(256*4) ; 256KB when size bit is set @L1: sty pagecount+1 ldy #$FF sty curpage sty curpage+1 ; Invalidate the current page txa ; X = A = EM_ERR_OK rts ; No REU found nodevice: lda #EM_ERR_NO_DEVICE ; rts ; Run into UNINSTALL instead ; ------------------------------------------------------------------------ ; UNINSTALL routine. Is called before the driver is removed from memory. ; Can do cleanup or whatever. Must not return anything. ; UNINSTALL: rts ; ------------------------------------------------------------------------ ; PAGECOUNT: Return the total number of available pages in a/x. ; PAGECOUNT: lda pagecount ldx pagecount+1 rts ; ------------------------------------------------------------------------ ; MAP: Map the page in a/x into memory and return a pointer to the page in ; a/x. The contents of the currently mapped page (if any) may be discarded ; by the driver. ; MAP: sta curpage stx curpage+1 ; Remember the new page ldy #OP_COPYFROM jsr common ; Copy the window lda #<window ldx #>window ; Return the window address done: rts ; ------------------------------------------------------------------------ ; USE: Tell the driver that the window is now associated with a given page. USE: sta curpage stx curpage+1 ; Remember the page lda #<window ldx #>window ; Return the window rts ; ------------------------------------------------------------------------ ; COMMIT: Commit changes in the memory window to extended storage. COMMIT: lda curpage ldx curpage+1 ; Do we have a page mapped? bmi done ; Jump if no page mapped ldy #OP_COPYTO common: sty tmp1 ldy #<window sty REU_C64ADDR ldy #>window sty REU_C64ADDR+1 ldy #0 sty REU_REUADDR+0 sta REU_REUADDR+1 stx REU_REUADDR+2 sty REU_COUNT+0 ldy #1 sty REU_COUNT+1 ; Move 256 bytes bne transfer1 ; Transfer 256 bytes into REU ; ------------------------------------------------------------------------ ; COPYFROM: Copy from extended into linear memory. A pointer to a structure ; describing the request is passed in a/x. ; The function must not return anything. ; COPYFROM: ldy #OP_COPYFROM .byte $2C ; ------------------------------------------------------------------------ ; COPYTO: Copy from linear into extended memory. A pointer to a structure ; describing the request is passed in a/x. ; The function must not return anything. ; COPYTO: ldy #OP_COPYTO sty tmp1 ; Remember the passed pointer sta ptr1 stx ptr1+1 ; Save the pointer ; The structure passed to the functions has the same layout as the registers ; of the Commodore REU, so register programming is easy. ldy #7-1 @L1: lda (ptr1),y sta REU_C64ADDR,y dey bpl @L1 ; Invalidate the page in the memory window sty curpage+1 ; Y = $FF ; Reload the REU command and start the transfer transfer1: ldy tmp1 ; Transfer subroutine for the REU. Expects command in Y. transfer: sty REU_COMMAND ; Issue command ldy MMU_CR ; Save the current MMU settings lda #MMU_CFG_RAM0 ; sei ; sta MMU_CR ; Enable RAM in bank #0 lda REU_TRIGGER ; Don't change $FF00 sta REU_TRIGGER ; Start the transfer... sty MMU_CR ; Restore the old configuration cli rts
wagiminator/C64-Collection
4,571
C64_xu1541/software/tools/cc65-2.13.2/libsrc/c128/crt0.s
; ; Startup code for cc65 (C128 version) ; .export _exit .export __STARTUP__ : absolute = 1 ; Mark as startup .import callirq, initlib, donelib .import zerobss .import push0, callmain .import RESTOR, BSOUT, CLRCH .import __INTERRUPTOR_COUNT__ .import __RAM_START__, __RAM_SIZE__ .include "zeropage.inc" .include "c128.inc" ; ------------------------------------------------------------------------ ; Constants IRQInd = $2FD ; JMP $0000 - used as indirect IRQ vector ; ------------------------------------------------------------------------ ; Place the startup code in a special segment to cope with the quirks of ; c128 banking. .segment "STARTUP" ; BASIC header with a SYS call .org $1BFF .word Head ; Load address Head: .word @Next .word .version ; Line number .byte $9E,"7181" ; SYS 7181 .byte $00 ; End of BASIC line @Next: .word 0 ; BASIC end marker .reloc ; ------------------------------------------------------------------------ ; Actual code ; Close open files jsr CLRCH ; Switch to the second charset lda #14 jsr BSOUT ; Before doing anything else, we have to setup our banking configuration. ; Otherwise just the lowest 16K are actually RAM. Writing through the ROM ; to the underlying RAM works, but it is bad style. lda MMU_CR ; Get current memory configuration... pha ; ...and save it for later lda #MMU_CFG_CC65 ; Bank0 with kernal ROM sta MMU_CR ; Save the zero page locations we need ldx #zpspace-1 L1: lda sp,x sta zpsave,x dex bpl L1 ; Clear the BSS data jsr zerobss ; Save system stuff and setup the stack pla ; Get MMU setting sta mmusave tsx stx spsave ; Save the system stack pointer lda #<(__RAM_START__ + __RAM_SIZE__) sta sp lda #>(__RAM_START__ + __RAM_SIZE__) sta sp+1 ; Set argument stack ptr ; If we have IRQ functions, chain our stub into the IRQ vector lda #<__INTERRUPTOR_COUNT__ beq NoIRQ1 lda IRQVec ldx IRQVec+1 sta IRQInd+1 stx IRQInd+2 lda #<IRQStub ldx #>IRQStub sei sta IRQVec stx IRQVec+1 cli ; Call module constructors NoIRQ1: jsr initlib ; Set the bank for the file name to our execution bank. We must do this, ; *after* calling constructors, because some of them may depend on the ; original value of this register. lda #0 sta FNAM_BANK ; Push arguments and call main() jsr callmain ; Back from main (this is also the _exit entry). Run module destructors _exit: jsr donelib ; Reset the IRQ vector if we chained it. pha ; Save the return code on stack lda #<__INTERRUPTOR_COUNT__ beq NoIRQ2 lda IRQInd+1 ldx IRQInd+2 sei sta IRQVec stx IRQVec+1 cli ; Copy back the zero page stuff NoIRQ2: ldx #zpspace-1 L2: lda zpsave,x sta sp,x dex bpl L2 ; Place the program return code into ST pla sta ST ; Reset the stack and the memory configuration ldx spsave txs ldx mmusave stx MMU_CR ; Done, restore kernal vectors in an attempt to cleanup jmp RESTOR ; ------------------------------------------------------------------------ ; The C128 has ROM parallel to the RAM starting from $4000. The startup code ; above will change this setting so that we have RAM from $0000-$BFFF. This ; works quite well with the exception of interrupts: The interrupt handler ; is in ROM, and the ROM switches back to the ROM configuration, which means ; that parts of our program may not be accessible. To solve this, we place ; the following code into a special segment called "LOWCODE" which will be ; placed just above the startup code, so it goes into a RAM area that is ; not banked. .segment "LOWCODE" IRQStub: cld ; Just to be sure lda MMU_CR ; Get old register value pha ; And save on stack lda #MMU_CFG_CC65 ; Bank 0 with kernal ROM sta MMU_CR jsr callirq ; Call the functions pla ; Get old register value sta MMU_CR jmp IRQInd ; Jump to the saved IRQ vector ; ------------------------------------------------------------------------ ; Data .segment "ZPSAVE" zpsave: .res zpspace .bss spsave: .res 1 mmusave:.res 1
wagiminator/C64-Collection
6,632
C64_xu1541/software/tools/cc65-2.13.2/libsrc/c128/c128-georam.s
; ; Extended memory driver for the GEORAM cartridge. Driver works without ; problems when statically linked. ; ; Ullrich von Bassewitz, 2002-11-29 ; .include "zeropage.inc" .include "em-kernel.inc" .include "em-error.inc" .macpack generic ; ------------------------------------------------------------------------ ; Header. Includes jump table .segment "JUMPTABLE" ; Driver signature .byte $65, $6d, $64 ; "emd" .byte EMD_API_VERSION ; EM API version number ; Jump table. .word INSTALL .word UNINSTALL .word PAGECOUNT .word MAP .word USE .word COMMIT .word COPYFROM .word COPYTO ; ------------------------------------------------------------------------ ; Constants GR_WINDOW = $DE00 ; Address of GEORAM window GR_PAGE_LO = $DFFE ; Page register low GR_PAGE_HI = $DFFF ; Page register high ; ------------------------------------------------------------------------ ; Data. .data pagecount: .word 2048 ; Currently fixed .code ; ------------------------------------------------------------------------ ; INSTALL routine. Is called after the driver is loaded into memory. If ; possible, check if the hardware is present and determine the amount of ; memory available. ; Must return an EM_ERR_xx code in a/x. ; INSTALL: lda #<EM_ERR_OK ldx #>EM_ERR_OK ; rts ; Run into UNINSTALL instead ; ------------------------------------------------------------------------ ; UNINSTALL routine. Is called before the driver is removed from memory. ; Can do cleanup or whatever. Must not return anything. ; UNINSTALL: rts ; ------------------------------------------------------------------------ ; PAGECOUNT: Return the total number of available pages in a/x. ; PAGECOUNT: lda pagecount ldx pagecount+1 rts ; ------------------------------------------------------------------------ ; USE: Tell the driver that the window is now associated with a given page. ; The GeoRAM cartridge does not copy but actually map the window, so USE is ; identical to MAP. USE = MAP ; ------------------------------------------------------------------------ ; MAP: Map the page in a/x into memory and return a pointer to the page in ; a/x. The contents of the currently mapped page (if any) may be discarded ; by the driver. ; MAP: sta tmp1 txa asl tmp1 rol a asl tmp1 rol a sta GR_PAGE_HI lda tmp1 lsr a lsr a sta GR_PAGE_LO lda #<GR_WINDOW ldx #>GR_WINDOW ; Use the RTS from COMMIT below to save a precious byte of storage ; ------------------------------------------------------------------------ ; COMMIT: Commit changes in the memory window to extended storage. COMMIT: rts ; ------------------------------------------------------------------------ ; COPYFROM: Copy from extended into linear memory. A pointer to a structure ; describing the request is passed in a/x. ; The function must not return anything. ; COPYFROM: jsr setup ; Setup is: ; ; - ptr1 contains the struct pointer ; - ptr2 contains the linear memory buffer ; - ptr3 contains -(count-1) ; - tmp1 contains the low page register value ; - tmp2 contains the high page register value ; - X contains the page offset ; - Y contains zero jmp @L5 @L1: lda GR_WINDOW,x sta (ptr2),y iny bne @L2 inc ptr2+1 @L2: inx beq @L4 ; Bump count and repeat @L3: inc ptr3 bne @L1 inc ptr3+1 bne @L1 rts ; Bump page register @L4: inc tmp1 ; Bump low page register bit tmp1 ; Check for overflow in bit 6 bvc @L6 ; Jump if no overflow inc tmp2 @L5: lda tmp2 sta GR_PAGE_HI @L6: lda tmp1 sta GR_PAGE_LO jmp @L3 ; ------------------------------------------------------------------------ ; COPYTO: Copy from linear into extended memory. A pointer to a structure ; describing the request is passed in a/x. ; The function must not return anything. ; COPYTO: jsr setup ; Setup is: ; ; - ptr1 contains the struct pointer ; - ptr2 contains the linear memory buffer ; - ptr3 contains -(count-1) ; - tmp1 contains the low page register value ; - tmp2 contains the high page register value ; - X contains the page offset ; - Y contains zero jmp @L5 @L1: lda (ptr2),y sta GR_WINDOW,x iny bne @L2 inc ptr2+1 @L2: inx beq @L4 ; Bump count and repeat @L3: inc ptr3 bne @L1 inc ptr3+1 bne @L1 rts ; Bump page register @L4: inc tmp1 ; Bump low page register bit tmp1 ; Check for overflow in bit 6 bvc @L6 ; Jump if no overflow inc tmp2 @L5: lda tmp2 sta GR_PAGE_HI @L6: lda tmp1 sta GR_PAGE_LO jmp @L3 ; ------------------------------------------------------------------------ ; Helper function for COPYFROM and COPYTO: Store the pointer to the request ; structure and prepare data for the copy setup: sta ptr1 stx ptr1+1 ; Save passed pointer ; Get the page number from the struct and adjust it so that it may be used ; with the hardware. That is: lower 6 bits in tmp1, high bits in tmp2. ldy #EM_COPY::PAGE+1 lda (ptr1),y sta tmp2 dey lda (ptr1),y asl a rol tmp2 asl a rol tmp2 lsr a lsr a sta tmp1 ; Get the buffer pointer into ptr2 ldy #EM_COPY::BUF lda (ptr1),y sta ptr2 iny lda (ptr1),y sta ptr2+1 ; Get the count, calculate -(count-1) and store it into ptr3 ldy #EM_COPY::COUNT lda (ptr1),y eor #$FF sta ptr3 iny lda (ptr1),y eor #$FF sta ptr3+1 ; Get the page offset into X and clear Y ldy #EM_COPY::OFFS lda (ptr1),y tax ldy #$00 ; Done rts
wagiminator/C64-Collection
2,774
C64_xu1541/software/tools/cc65-2.13.2/libsrc/c128/c128-stdjoy.s
; ; Standard joystick driver for the C128. May be used multiple times when linked ; to the statically application. ; ; Ullrich von Bassewitz, 2002-12-21 ; .include "zeropage.inc" .include "joy-kernel.inc" .include "joy-error.inc" .include "c128.inc" .macpack generic ; ------------------------------------------------------------------------ ; Header. Includes jump table .segment "JUMPTABLE" ; Driver signature .byte $6A, $6F, $79 ; "joy" .byte JOY_API_VERSION ; Driver API version number ; Button state masks (8 values) .byte $01 ; JOY_UP .byte $02 ; JOY_DOWN .byte $04 ; JOY_LEFT .byte $08 ; JOY_RIGHT .byte $10 ; JOY_FIRE .byte $00 ; JOY_FIRE2 unavailable .byte $00 ; Future expansion .byte $00 ; Future expansion ; Jump table. .addr INSTALL .addr UNINSTALL .addr COUNT .addr READ .addr 0 ; IRQ entry not used ; ------------------------------------------------------------------------ ; Constants JOY_COUNT = 2 ; Number of joysticks we support ; ------------------------------------------------------------------------ ; Data. .code ; ------------------------------------------------------------------------ ; INSTALL routine. Is called after the driver is loaded into memory. If ; possible, check if the hardware is present and determine the amount of ; memory available. ; Must return an JOY_ERR_xx code in a/x. ; INSTALL: lda #<JOY_ERR_OK ldx #>JOY_ERR_OK ; rts ; Run into UNINSTALL instead ; ------------------------------------------------------------------------ ; UNINSTALL routine. Is called before the driver is removed from memory. ; Can do cleanup or whatever. Must not return anything. ; UNINSTALL: rts ; ------------------------------------------------------------------------ ; COUNT: Return the total number of available joysticks in a/x. ; COUNT: lda #<JOY_COUNT ldx #>JOY_COUNT rts ; ------------------------------------------------------------------------ ; READ: Read a particular joystick passed in A. ; READ: 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 sei sta CIA1_DDRA lda CIA1_PRA sty CIA1_DDRA cli and #$1F eor #$1F rts
wagiminator/C64-Collection
1,940
C64_xu1541/software/tools/cc65-2.13.2/libsrc/c128/systime.s
; ; Stefan Haubenthal, 27.7.2009 ; ; time_t _systime (void); ; /* Similar to time(), but: ; * - Is not ISO C ; * - Does not take the additional pointer ; * - Does not set errno when returning -1 ; */ ; .include "time.inc" .include "c128.inc" .constructor initsystime .importzp tmp1, tmp2 ;---------------------------------------------------------------------------- .code .proc __systime lda CIA1_TODHR bpl AM and #%01111111 sed clc adc #$12 cld AM: jsr BCD2dec sta TM + tm::tm_hour lda CIA1_TODMIN jsr BCD2dec sta TM + tm::tm_min lda CIA1_TODSEC jsr BCD2dec sta TM + tm::tm_sec lda CIA1_TOD10 ; Dummy read to unfreeze lda #<TM ldx #>TM jmp _mktime ; dec = (((BCD>>4)*10) + (BCD&0xf)) BCD2dec:tax and #%00001111 sta tmp1 txa and #%11110000 ; *16 lsr ; *8 sta tmp2 lsr lsr ; *2 adc tmp2 ; = *10 adc tmp1 rts .endproc ;---------------------------------------------------------------------------- ; Constructor that writes to the 1/10 sec register of the TOD to kick it ; into action. If this is not done, the clock hangs. We will read the register ; and write it again, ignoring a possible change in between. .proc initsystime lda CIA1_TOD10 sta CIA1_TOD10 rts .endproc ;---------------------------------------------------------------------------- ; TM struct with date set to 1970-01-01 .data TM: .word 0 ; tm_sec .word 0 ; tm_min .word 0 ; tm_hour .word 1 ; tm_mday .word 0 ; tm_mon .word 70 ; tm_year .word 0 ; tm_wday .word 0 ; tm_yday .word 0 ; tm_isdst
wagiminator/C64-Collection
7,750
C64_xu1541/software/tools/cc65-2.13.2/libsrc/c128/c128-vdc.s
; ; Extended memory driver for the VDC RAM available on all C128 machines ; (based on code by Ullrich von Bassewitz) ; Maciej 'YTM/Elysium' Witkowiak <ytm@elysium.pl> ; 06,20.12.2002 .include "zeropage.inc" .include "em-kernel.inc" .include "em-error.inc" .macpack generic ; ------------------------------------------------------------------------ ; Header. Includes jump table .segment "JUMPTABLE" ; Driver signature .byte $65, $6d, $64 ; "emd" .byte EMD_API_VERSION ; EM API version number ; Jump table. .word INSTALL .word DEINSTALL .word PAGECOUNT .word MAP .word USE .word COMMIT .word COPYFROM .word COPYTO ; ------------------------------------------------------------------------ ; Constants VDC_ADDR_REG = $D600 ; VDC address VDC_DATA_REG = $D601 ; VDC data VDC_DATA_HI = 18 ; used registers VDC_DATA_LO = 19 VDC_CSET = 28 VDC_DATA = 31 ; ------------------------------------------------------------------------ ; Data. .data pagecount: .word 64 ; $0000-$3fff as 16k default curpage: .word $ffff ; currently mapped-in page (invalid) .bss window: .res 256 ; memory window .code ; ------------------------------------------------------------------------ ; INSTALL routine. Is called after the driver is loaded into memory. If ; possible, check if the hardware is present and determine the amount of ; memory available. ; Must return an EM_ERR_xx code in a/x. ; INSTALL: ; do test for VDC presence here??? ldx #VDC_CSET ; determine size of RAM... jsr vdcgetreg sta tmp1 ora #%00010000 jsr vdcputreg ; turn on 64k jsr settestadr1 ; save original value of test byte jsr vdcgetbyte sta tmp2 lda #$55 ; write $55 here ldy #ptr1 jsr test64k ; read it here and there lda #$aa ; write $aa here ldy #ptr2 jsr test64k ; read it here and there jsr settestadr1 lda tmp2 jsr vdcputbyte ; restore original value of test byte lda ptr1 ; do bytes match? cmp ptr1+1 bne @have64k lda ptr2 cmp ptr2+1 bne @have64k ldx #VDC_CSET lda tmp1 jsr vdcputreg ; restore 16/64k flag jmp @endok ; and leave default values for 16k @have64k: lda #<256 ldx #>256 sta pagecount stx pagecount+1 @endok: lda #<EM_ERR_OK ldx #>EM_ERR_OK rts test64k: sta tmp1 sty ptr3 lda #0 sta ptr3+1 jsr settestadr1 lda tmp1 jsr vdcputbyte ; write $55 jsr settestadr1 jsr vdcgetbyte ; read here pha jsr settestadr2 jsr vdcgetbyte ; and there ldy #1 sta (ptr3),y pla dey sta (ptr3),y rts settestadr1: ldy #$02 ; test page 2 (here) .byte $2c settestadr2: ldy #$42 ; or page 64+2 (there) lda #0 jmp vdcsetsrcaddr ; ------------------------------------------------------------------------ ; DEINSTALL routine. Is called before the driver is removed from memory. ; Can do cleanup or whatever. Must not return anything. ; DEINSTALL: ;on C128 restore font and clear the screen? rts ; ------------------------------------------------------------------------ ; PAGECOUNT: Return the total number of available pages in a/x. ; PAGECOUNT: lda pagecount ldx pagecount+1 rts ; ------------------------------------------------------------------------ ; MAP: Map the page in a/x into memory and return a pointer to the page in ; a/x. The contents of the currently mapped page (if any) may be discarded ; by the driver. ; MAP: sta curpage stx curpage+1 sta ptr1+1 ldy #0 sty ptr1 lda #<window sta ptr2 lda #>window sta ptr2+1 jsr transferin lda #<window ldx #>window rts ; copy a single page from (ptr1):VDCRAM to (ptr2):RAM transferin: lda ptr1 ldy ptr1+1 jsr vdcsetsrcaddr ; set source address in VDC ldy #0 ldx #VDC_DATA stx VDC_ADDR_REG @L0: bit VDC_ADDR_REG bpl @L0 lda VDC_DATA_REG ; get 2 bytes at a time to speed-up sta (ptr2),y ; (in fact up to 8 bytes could be fetched with special VDC config) iny lda VDC_DATA_REG sta (ptr2),y iny bne @L0 rts ; ------------------------------------------------------------------------ ; USE: Tell the driver that the window is now associated with a given page. USE: sta curpage stx curpage+1 ; Remember the page lda #<window ldx #>window ; Return the window done: rts ; ------------------------------------------------------------------------ ; COMMIT: Commit changes in the memory window to extended storage. COMMIT: lda curpage ; jump if no page mapped ldx curpage+1 bmi done sta ptr1+1 ldy #0 sty ptr1 lda #<window sta ptr2 lda #>window sta ptr2+1 ; fall through to transferout ; copy a single page from (ptr2):RAM to (ptr1):VDCRAM transferout: lda ptr1 ldy ptr1+1 jsr vdcsetsrcaddr ; set source address in VDC ldy #0 ldx #VDC_DATA stx VDC_ADDR_REG @L0: bit VDC_ADDR_REG bpl @L0 lda (ptr2),y ; speedup does not work for writing sta VDC_DATA_REG iny bne @L0 rts ; ------------------------------------------------------------------------ ; COPYFROM: Copy from extended into linear memory. A pointer to a structure ; describing the request is passed in a/x. ; The function must not return anything. ; COPYFROM: jsr setup beq @L2 ; Skip if no full pages ; Copy full pages @L1: jsr transferin inc ptr1+1 inc ptr2+1 dec tmp1 bne @L1 ; Copy the remainder of the page @L2: ldy #EM_COPY::COUNT lda (ptr3),y ; Get bytes in last page beq @L4 sta tmp1 ; Transfer the bytes in the last page ldy #0 @L3: jsr vdcgetbyte sta (ptr2),y iny dec tmp1 lda tmp1 bne @L3 @L4: rts ; ------------------------------------------------------------------------ ; COPYTO: Copy from linear into extended memory. A pointer to a structure ; describing the request is passed in a/x. ; The function must not return anything. ; COPYTO: jsr setup beq @L2 ; Skip if no full pages ; Copy full pages @L1: jsr transferout inc ptr1+1 inc ptr2+1 dec tmp1 bne @L1 ; Copy the remainder of the page @L2: ldy #EM_COPY::COUNT lda (ptr3),y ; Get bytes in last page beq @L4 sta tmp1 ; Transfer the bytes in the last page ldy #0 @L3: lda (ptr2),y jsr vdcputbyte iny dec tmp1 lda tmp1 bne @L3 @L4: rts ;------------------------------------------------------------------------- ; Helper functions to handle VDC ram ; vdcsetsrcaddr: ldx #VDC_DATA_LO stx VDC_ADDR_REG @L0: bit VDC_ADDR_REG bpl @L0 sta VDC_DATA_REG dex tya stx VDC_ADDR_REG sta VDC_DATA_REG rts vdcgetbyte: ldx #VDC_DATA vdcgetreg: stx VDC_ADDR_REG @L0: bit VDC_ADDR_REG bpl @L0 lda VDC_DATA_REG rts vdcputbyte: ldx #VDC_DATA vdcputreg: stx VDC_ADDR_REG @L0: bit VDC_ADDR_REG bpl @L0 sta VDC_DATA_REG rts ; ------------------------------------------------------------------------ ; Helper function for COPYFROM and COPYTO: Store the pointer to the request ; structure and prepare data for the copy ; setup: sta ptr3 stx ptr3+1 ; Save the passed em_copy pointer ldy #EM_COPY::OFFS lda (ptr3),y sta ptr1 ldy #EM_COPY::PAGE lda (ptr3),y sta ptr1+1 ; From ldy #EM_COPY::BUF lda (ptr3),y sta ptr2 iny lda (ptr3),y sta ptr2+1 ; To ldy #EM_COPY::COUNT+1 lda (ptr3),y ; Get number of pages sta tmp1 rts
wagiminator/C64-Collection
2,158
C64_xu1541/software/tools/cc65-2.13.2/libsrc/c128/kernal.s
; ; Ullrich von Bassewitz, 19.11.2002 ; ; C128 kernal functions ; .export C64MODE .export SWAPPER .export SETBNK .export CINT .export IOINIT .export RAMTAS .export RESTOR .export VECTOR .export SETMSG .export SECOND .export TKSA .export MEMTOP .export MEMBOT .export SCNKEY .export SETTMO .export ACPTR .export CIOUT .export UNTLK .export UNLSN .export LISTEN .export TALK .export READST .export SETLFS .export SETNAM .export OPEN .export CLOSE .export CHKIN .export CKOUT .export CLRCH .export BASIN .export BSOUT .export LOAD .export SAVE .export SETTIM .export RDTIM .export STOP .export GETIN .export CLALL .export UDTIM .export SCREEN .export PLOT .export IOBASE ;----------------------------------------------------------------------------- ; All functions are available in the kernal jump table ; Extended jump table C64MODE = $FF4D SWAPPER = $FF5F SETBNK = $FF68 ; CINT = $FF81 IOINIT = $FF84 RAMTAS = $FF87 RESTOR = $FF8A VECTOR = $FF8D SETMSG = $FF90 SECOND = $FF93 TKSA = $FF96 MEMTOP = $FF99 MEMBOT = $FF9C SCNKEY = $FF9F SETTMO = $FFA2 ACPTR = $FFA5 CIOUT = $FFA8 UNTLK = $FFAB UNLSN = $FFAE LISTEN = $FFB1 TALK = $FFB4 READST = $FFB7 SETLFS = $FFBA SETNAM = $FFBD OPEN = $FFC0 CLOSE = $FFC3 CHKIN = $FFC6 CKOUT = $FFC9 CLRCH = $FFCC BASIN = $FFCF BSOUT = $FFD2 LOAD = $FFD5 SAVE = $FFD8 SETTIM = $FFDB RDTIM = $FFDE STOP = $FFE1 GETIN = $FFE4 CLALL = $FFE7 UDTIM = $FFEA SCREEN = $FFED PLOT = $FFF0 IOBASE = $FFF3
wagiminator/C64-Collection
23,391
C64_xu1541/software/tools/cc65-2.13.2/libsrc/c128/c128-640-480-2.s
; ; Graphics driver for the 640x480x2 mode on the C128 VDC 64k ; (values for this mode based on Fred Bowen's document) ; Maciej 'YTM/Elysium' Witkowiak <ytm@elysium.pl> ; 23.12.2002 ; 2004-04-04, Greg King ; ; NOTES: ; For any smart monkey that will try to optimize this: PLEASE do tests on ; real VDC, not only VICE. ; ; Only DONE routine contains C128-mode specific stuff, everything else will ; work in C64-mode of C128 (C64 needs full VDC init then). ; ; With special initialization and CALC we can get 320x200 double-pixel mode. ; ; Color translation values for BROWN and GRAY3 are obviously wrong, they ; could be replaced by equiv. of ORANGE and GRAY2 but this would give only ; 14 of 16 colors available. ; ; Register 25 ($19) is said to require different value for VDC v1, but I ; couldn't find what it should be. .include "zeropage.inc" .include "tgi-kernel.inc" .include "tgi-mode.inc" .include "tgi-error.inc" .macpack generic ; ------------------------------------------------------------------------ ; Constants VDC_ADDR_REG = $D600 ; VDC address VDC_DATA_REG = $D601 ; VDC data VDC_DSP_HI = 12 ; registers used VDC_DSP_LO = 13 VDC_DATA_HI = 18 VDC_DATA_LO = 19 VDC_VSCROLL = 24 VDC_HSCROLL = 25 VDC_COLORS = 26 VDC_CSET = 28 VDC_COUNT = 30 VDC_DATA = 31 ; ------------------------------------------------------------------------ ; Header. Includes jump table and constants. .segment "JUMPTABLE" ; First part of the header is a structure that has a magic and defines the ; capabilities of the driver .byte $74, $67, $69 ; "tgi" .byte TGI_API_VERSION ; TGI version number xres: .word 640 ; X resolution yres: .word 480 ; Y resolution .byte 2 ; Number of drawing colors pages: .byte 0 ; Number of screens available .byte 8 ; System font X size .byte 8 ; System font Y size .res 4, $00 ; Reserved for future extensions ; Next comes the jump table. Currently all entries must be valid and may point ; to an RTS for test versions (function not implemented). .addr INSTALL .addr UNINSTALL .addr INIT .addr DONE .addr GETERROR .addr CONTROL .addr CLEAR .addr SETVIEWPAGE .addr SETDRAWPAGE .addr SETCOLOR .addr SETPALETTE .addr GETPALETTE .addr GETDEFPALETTE .addr SETPIXEL .addr GETPIXEL .addr LINE .addr BAR .addr CIRCLE .addr TEXTSTYLE .addr OUTTEXT .addr 0 ; IRQ entry is unused ; ------------------------------------------------------------------------ ; Data. ; Variables mapped to the zero page segment variables. Some of these are ; used for passing parameters to the driver. X1 = ptr1 Y1 = ptr2 X2 = ptr3 Y2 = ptr4 RADIUS = tmp1 ADDR = tmp1 ; (2) CALC TEMP = tmp3 ; CALC icmp TEMP2 = tmp4 ; icmp TEMP3 = sreg ; LINE TEMP4 = sreg+1 ; LINE ; Line routine stuff (must be on zpage) PB = ptr3 ; (2) LINE UB = ptr4 ; (2) LINE ERR = regsave ; (2) LINE NX = regsave+2 ; (2) LINE ; Circle stuff XX = ptr3 ; (2) CIRCLE YY = ptr4 ; (2) CIRCLE MaxO = sreg ; (overwritten by TEMP3+TEMP4, but restored from OG/OU anyway) XS = regsave ; (2) CIRCLE YS = regsave+2 ; (2) CIRCLE ; Absolute variables used in the code .bss ERROR: .res 1 ; Error code PALETTE: .res 2 ; The current palette BITMASK: .res 1 ; $00 = clear, $FF = set pixels OLDCOLOR: .res 1 ; colors before entering gfx mode ; Line routine stuff (combined with CIRCLE to save space) OGora: COUNT: .res 2 OUkos: NY: .res 2 Y3: DX: .res 1 DY: .res 1 AX: .res 1 AY: .res 1 ; Text output stuff TEXTMAGX: .res 1 TEXTMAGY: .res 1 TEXTDIR: .res 1 ; Constants and tables .rodata DEFPALETTE: .byte $00, $0f ; White on black PALETTESIZE = * - DEFPALETTE BITTAB: .byte $80,$40,$20,$10,$08,$04,$02,$01 BITMASKL: .byte %11111111, %01111111, %00111111, %00011111 .byte %00001111, %00000111, %00000011, %00000001 BITMASKR: .byte %10000000, %11000000, %11100000, %11110000 .byte %11111000, %11111100, %11111110, %11111111 ; color translation table (indexed by VIC color) COLTRANS: .byte $00, $0f, $08, $06, $0a, $04, $02, $0c .byte $0d, $0b, $09, $01, $0e, $05, $03, $07 ; colors BROWN and GRAY3 are wrong ; VDC initialization table (reg),(val),...,$ff InitVDCTab: .byte VDC_DSP_HI, 0 ; viewpage 0 as default .byte VDC_DSP_LO, 0 .byte VDC_HSCROLL, $87 .byte 2, $66 .byte 4, $4c .byte 5, $06 .byte 6, $4c .byte 7, $47 .byte 8, $03 .byte 9, $06 .byte 27, $00 .byte $ff SCN80CLR: .byte 27,88,147,27,88,0 .code ; ------------------------------------------------------------------------ ; INSTALL routine. Is called after the driver is loaded into memory. May ; initialize anything that has to be done just once. Is probably empty ; most of the time. ; ; Must set an error code: NO ; INSTALL: ; check for VDC version and update register $19 value ; check for VDC ram size and update number of available screens ldx #VDC_CSET ; determine size of RAM... jsr VDCReadReg sta tmp1 ora #%00010000 jsr VDCWriteReg ; turn on 64k jsr settestadr1 ; save original value of test byte jsr VDCReadByte sta tmp2 lda #$55 ; write $55 here ldy #ptr1 jsr test64k ; read it here and there lda #$aa ; write $aa here ldy #ptr2 jsr test64k ; read it here and there jsr settestadr1 lda tmp2 jsr VDCWriteByte ; restore original value of test byte lda ptr1 ; do bytes match? cmp ptr1+1 bne @have64k lda ptr2 cmp ptr2+1 bne @have64k ldx #VDC_CSET lda tmp1 jsr VDCWriteReg ; restore 16/64k flag jmp @endok ; and leave default values for 16k @have64k: lda #1 sta pages @endok: rts test64k: sta tmp1 sty ptr3 lda #0 sta ptr3+1 jsr settestadr1 lda tmp1 jsr VDCWriteByte ; write $55 jsr settestadr1 jsr VDCReadByte ; read here pha jsr settestadr2 jsr VDCReadByte ; and there ldy #1 sta (ptr3),y pla dey sta (ptr3),y rts settestadr1: ldy #$02 ; test page 2 (here) .byte $2c settestadr2: ldy #$42 ; or page 64+2 (there) lda #0 jmp VDCSetSourceAddr ; ------------------------------------------------------------------------ ; UNINSTALL routine. Is called before the driver is removed from memory. May ; clean up anything done by INSTALL but is probably empty most of the time. ; ; Must set an error code: NO ; UNINSTALL: rts ; ------------------------------------------------------------------------ ; INIT: Changes an already installed device from text mode to graphics ; mode. ; Note that INIT/DONE may be called multiple times while the driver ; is loaded, while INSTALL is only called once, so any code that is needed ; to initializes variables and so on must go here. Setting palette and ; clearing the screen is not needed because this is called by the graphics ; kernel later. ; The graphics kernel will never call INIT when a graphics mode is already ; active, so there is no need to protect against that. ; ; Must set an error code: YES ; INIT: lda pages ; is there enough memory? bne @L11 ; Jump if there is one screen lda #TGI_ERR_INV_MODE ; Error bne @L9 ; Initialize variables @L11: ldx #$FF stx BITMASK ; Remeber current color value ldx #VDC_COLORS jsr VDCReadReg sta OLDCOLOR ; Switch into graphics mode (set view page 0) ldy #0 @L2: ldx InitVDCTab,y bmi @L3 iny lda InitVDCTab,y jsr VDCWriteReg iny bne @L2 @L3: ; Done, reset the error code lda #TGI_ERR_OK @L9: sta ERROR rts ; ------------------------------------------------------------------------ ; DONE: Will be called to switch the graphics device back into text mode. ; The graphics kernel will never call DONE when no graphics mode is active, ; so there is no need to protect against that. ; ; Must set an error code: NO ; DONE: ; This part is C128-mode specific jsr $e179 ; reload character set and setup VDC jsr $ff62 lda $d7 ; in 80-columns? bne @L01 @L0: lda SCN80CLR,y beq @L1 jsr $ffd2 ; print \xe,clr,\xe iny bne @L0 @L01: lda #147 jsr $ffd2 ; print clr @L1: lda #0 ; restore view page ldx #VDC_DSP_HI jsr VDCWriteReg lda OLDCOLOR ldx #VDC_COLORS jsr VDCWriteReg ; restore color (background) lda #$47 ldx #VDC_HSCROLL jmp VDCWriteReg ; switch to text screen ; ------------------------------------------------------------------------ ; GETERROR: Return the error code in A and clear it. GETERROR: ldx #TGI_ERR_OK lda ERROR stx ERROR rts ; ------------------------------------------------------------------------ ; CONTROL: Platform/driver specific entry point. ; ; Must set an error code: YES ; CONTROL: lda #TGI_ERR_INV_FUNC sta ERROR rts ; ------------------------------------------------------------------------ ; CLEAR: Clears the screen. ; ; Must set an error code: NO ; CLEAR: lda #0 tay jsr VDCSetSourceAddr lda #0 ldx #VDC_VSCROLL jsr VDCWriteReg ; set fill mode lda #0 jsr VDCWriteByte ; put 1rst byte (fill value) ldy #159 ; 159 times lda #0 ; 256 bytes ldx #VDC_COUNT @L1: jsr VDCWriteReg dey bne @L1 rts ; ------------------------------------------------------------------------ ; SETVIEWPAGE: Set the visible page. Called with the new page in A (0..n). ; The page number is already checked to be valid by the graphics kernel. ; ; Must set an error code: NO (will only be called if page ok) ; SETVIEWPAGE: rts ; ------------------------------------------------------------------------ ; SETDRAWPAGE: Set the drawable page. Called with the new page in A (0..n). ; The page number is already checked to be valid by the graphics kernel. ; ; Must set an error code: NO (will only be called if page ok) ; SETDRAWPAGE: rts ; ------------------------------------------------------------------------ ; SETCOLOR: Set the drawing color (in A). The new color is already checked ; to be in a valid range (0..maxcolor-1). ; ; Must set an error code: NO (will only be called if color ok) ; SETCOLOR: tax beq @L1 lda #$FF @L1: sta BITMASK rts ; ------------------------------------------------------------------------ ; SETPALETTE: Set the palette (not available with all drivers/hardware). ; A pointer to the palette is passed in ptr1. Must set an error if palettes ; are not supported ; ; Must set an error code: YES ; SETPALETTE: ldy #PALETTESIZE - 1 @L1: lda (ptr1),y ; Copy the palette and #$0F ; Make a valid color sta PALETTE,y dey bpl @L1 ; Get the color entries from the palette ldy PALETTE+1 ; Foreground color lda COLTRANS,y asl a asl a asl a asl a ldy PALETTE ; Background color ora COLTRANS,y ldx #VDC_COLORS jsr VDCWriteReg lda #TGI_ERR_OK ; Clear error code sta ERROR rts ; ------------------------------------------------------------------------ ; GETPALETTE: Return the current palette in A/X. Even drivers that cannot ; set the palette should return the default palette here, so there's no ; way for this function to fail. ; ; Must set an error code: NO ; GETPALETTE: lda #<PALETTE ldx #>PALETTE rts ; ------------------------------------------------------------------------ ; GETDEFPALETTE: Return the default palette for the driver in A/X. All ; drivers should return something reasonable here, even drivers that don't ; support palettes, otherwise the caller has no way to determine the colors ; of the (not changeable) palette. ; ; Must set an error code: NO (all drivers must have a default palette) ; GETDEFPALETTE: lda #<DEFPALETTE ldx #>DEFPALETTE rts ; ------------------------------------------------------------------------ ; SETPIXEL: Draw one pixel at X1/Y1 = ptr1/ptr2 with the current drawing ; color. The coordinates passed to this function are never outside the ; visible screen area, so there is no need for clipping inside this function. ; ; Must set an error code: NO ; SETPIXELCLIP: lda Y1+1 bmi @finito ; y<0 lda X1+1 bmi @finito ; x<0 lda X1 ldx X1+1 sta ADDR stx ADDR+1 ldx #ADDR lda xres ldy xres+1 jsr icmp ; ( x < xres ) ... bcs @finito lda Y1 ldx Y1+1 sta ADDR stx ADDR+1 ldx #ADDR lda yres ldy yres+1 jsr icmp ; ... && ( y < yres ) bcc SETPIXEL @finito:rts SETPIXEL: jsr CALC ; Calculate coordinates stx TEMP lda ADDR ldy ADDR+1 jsr VDCSetSourceAddr jsr VDCReadByte ldx TEMP sta TEMP eor BITMASK and BITTAB,X eor TEMP pha lda ADDR ldy ADDR+1 jsr VDCSetSourceAddr pla jsr VDCWriteByte @L9: rts ; ------------------------------------------------------------------------ ; GETPIXEL: Read the color value of a pixel and return it in A/X. The ; coordinates passed to this function are never outside the visible screen ; area, so there is no need for clipping inside this function. GETPIXEL: jsr CALC ; Calculate coordinates stx TEMP ; preserve X lda ADDR ldy ADDR+1 jsr VDCSetSourceAddr jsr VDCReadByte ldx TEMP ldy #$00 and BITTAB,X beq @L1 iny @L1: tya ; Get color value into A ldx #$00 ; Clear high byte rts ; ------------------------------------------------------------------------ ; LINE: Draw a line from X1/Y1 to X2/Y2, where X1/Y1 = ptr1/ptr2 and ; X2/Y2 = ptr3/ptr4 using the current drawing color. ; ; Must set an error code: NO ; LINE: ; nx = abs(x2 - x1) lda X2 sec sbc X1 sta NX lda X2+1 sbc X1+1 tay lda NX jsr abs sta NX sty NX+1 ; ny = abs(y2 - y1) lda Y2 sec sbc Y1 sta NY lda Y2+1 sbc Y1+1 tay lda NY jsr abs sta NY sty NY+1 ; if (x2>=x1) ldx #X2 lda X1 ldy X1+1 jsr icmp bcc @L0243 ; dx = 1; lda #1 bne @L0244 ; else ; dx = -1; @L0243: lda #$ff @L0244: sta DX ; if (y2>=y1) ldx #Y2 lda Y1 ldy Y1+1 jsr icmp bcc @L024A ; dy = 1; lda #1 bne @L024B ; else ; dy = -1; @L024A: lda #$ff @L024B: sta DY ; err = ax = ay = 0; lda #0 sta ERR sta ERR+1 sta AX sta AY ; if (nx<ny) { ldx #NX lda NY ldy NY+1 jsr icmp bcs @L0255 ; nx <-> ny lda NX ldx NY sta NY stx NX lda NX+1 ldx NY+1 sta NY+1 stx NX+1 ; ax = dx lda DX sta AX ; ay = dy lda DY sta AY ; dx = dy = 0; lda #0 sta DX sta DY ; ny = - ny; @L0255: lda NY ldy NY+1 jsr neg sta NY sty NY+1 ; for (count=nx;count>0;--count) { lda NX ldx NX+1 sta COUNT stx COUNT+1 @L0166: lda COUNT ; count>0 ora COUNT+1 bne @L0167 rts ; setpixel(X1,Y1) @L0167: jsr SETPIXELCLIP ; pb = err + ny lda ERR clc adc NY sta PB lda ERR+1 adc NY+1 sta PB+1 tax ; ub = pb + nx lda PB clc adc NX sta UB txa adc NX+1 sta UB+1 ; x1 = x1 + dx ldx #0 lda DX bpl @L027B dex @L027B: clc adc X1 sta X1 txa adc X1+1 sta X1+1 ; y1 = y1 + ay ldx #0 lda AY bpl @L027E dex @L027E: clc adc Y1 sta Y1 txa adc Y1+1 sta Y1+1 ; if (abs(pb)<abs(ub)) { lda PB ldy PB+1 jsr abs sta TEMP3 sty TEMP4 lda UB ldy UB+1 jsr abs ldx #TEMP3 jsr icmp bpl @L027F ; err = pb lda PB ldx PB+1 jmp @L0312 ; } else { x1 = x1 + ax @L027F: ldx #0 lda AX bpl @L0288 dex @L0288: clc adc X1 sta X1 txa adc X1+1 sta X1+1 ; y1 = y1 + dy ldx #0 lda DY bpl @L028B dex @L028B: clc adc Y1 sta Y1 txa adc Y1+1 sta Y1+1 ; err = ub } lda UB ldx UB+1 @L0312: sta ERR stx ERR+1 ; } (--count) sec lda COUNT sbc #1 sta COUNT bcc @L0260 jmp @L0166 @L0260: dec COUNT+1 jmp @L0166 ; ------------------------------------------------------------------------ ; BAR: Draw a filled rectangle with the corners X1/Y1, X2/Y2, where ; X1/Y1 = ptr1/ptr2 and X2/Y2 = ptr3/ptr4 using the current drawing color. ; Contrary to most other functions, the graphics kernel will sort and clip ; the coordinates before calling the driver, so on entry the following ; conditions are valid: ; X1 <= X2 ; Y1 <= Y2 ; (X1 >= 0) && (X1 < XRES) ; (X2 >= 0) && (X2 < XRES) ; (Y1 >= 0) && (Y1 < YRES) ; (Y2 >= 0) && (Y2 < YRES) ; ; Must set an error code: NO ; BAR: inc Y2 bne HORLINE inc Y2+1 ; Original code for a horizontal line HORLINE: lda X1 pha lda X1+1 pha jsr CALC ; get data for LEFT lda BITMASKL,x ; remember left address and bitmask pha lda ADDR pha lda ADDR+1 pha lda X2 sta X1 lda X2+1 sta X1+1 jsr CALC ; get data for RIGHT lda BITMASKR,x sta TEMP3 pla ; recall data for LEFT sta X1+1 pla sta X1 ; put left address into X1 pla cmp #%11111111 ; if left bit <> 0 beq @L1 sta TEMP2 ; do left byte only... lda X1 ldy X1+1 jsr VDCSetSourceAddr jsr VDCReadByte sta TEMP eor BITMASK and TEMP2 eor TEMP pha lda X1 ldy X1+1 jsr VDCSetSourceAddr pla jsr VDCWriteByte inc X1 ; ... and proceed bne @L1 inc X1+1 ; do right byte (if Y2=0 ++ADDR and skip) @L1: lda TEMP3 cmp #%11111111 ; if right bit <> 7 bne @L11 inc ADDR ; right bit = 7 - the next one is the last bne @L10 inc ADDR+1 @L10: bne @L2 @L11: lda ADDR ; do right byte only... ldy ADDR+1 jsr VDCSetSourceAddr jsr VDCReadByte sta TEMP eor BITMASK and TEMP3 eor TEMP pha lda ADDR ldy ADDR+1 jsr VDCSetSourceAddr pla jsr VDCWriteByte @L2: ; do the fill in the middle lda ADDR ; calculate offset in full bytes sec sbc X1 beq @L3 ; if equal - there are no more bytes sta ADDR lda X1 ; setup for the left side ldy X1+1 jsr VDCSetSourceAddr lda BITMASK ; get color jsr VDCWriteByte ; put 1st value ldx ADDR dex beq @L3 ; 1 byte already written stx ADDR ; if there are more bytes - fill them... ldx #VDC_VSCROLL lda #0 jsr VDCWriteReg ; setup for fill ldx #VDC_COUNT lda ADDR jsr VDCWriteReg ; ... fill them NOW! @L3: pla sta X1+1 pla sta X1 ; End of horizontal line code inc Y1 bne @L4 inc Y1+1 @L4: lda Y1 cmp Y2 bne @L5 lda Y1+1 cmp Y2+1 bne @L5 rts @L5: jmp HORLINE ; ------------------------------------------------------------------------ ; CIRCLE: Draw a circle around the center X1/Y1 (= ptr1/ptr2) with the ; radius in tmp1 and the current drawing color. ; ; Must set an error code: NO ; CIRCLE: lda RADIUS bne @L1 jmp SETPIXELCLIP ; Plot as a point @L1: sta XX ; x = r; lda #0 sta XX+1 sta YY sta YY+1 sta MaxO sta MaxO+1 ; y =0; mo=0; lda X1 ldx X1+1 sta XS stx XS+1 lda Y1 ldx Y1+1 sta YS stx YS+1 ; XS/YS to remember the center ; while (y<x) { @L013B: ldx #YY lda XX ldy XX+1 jsr icmp bcc @L12 rts @L12: ; plot points in 8 slices... lda XS clc adc XX sta X1 lda XS+1 adc XX+1 sta X1+1 ; x1 = xs+x lda YS clc adc YY sta Y1 pha lda YS+1 adc YY+1 sta Y1+1 ; (stack)=ys+y, y1=(stack) pha jsr SETPIXELCLIP ; plot(xs+x,ys+y) lda YS sec sbc YY sta Y1 sta Y3 lda YS+1 sbc YY+1 sta Y1+1 ; y3 = y1 = ys-y sta Y3+1 jsr SETPIXELCLIP ; plot(xs+x,ys-y) pla sta Y1+1 pla sta Y1 ; y1 = ys+y lda XS sec sbc XX sta X1 lda XS+1 sbc XX+1 sta X1+1 jsr SETPIXELCLIP ; plot (xs-x,ys+y) lda Y3 sta Y1 lda Y3+1 sta Y1+1 jsr SETPIXELCLIP ; plot (xs-x,ys-y) lda XS clc adc YY sta X1 lda XS+1 adc YY+1 sta X1+1 ; x1 = xs+y lda YS clc adc XX sta Y1 pha lda YS+1 adc XX+1 sta Y1+1 ; (stack)=ys+x, y1=(stack) pha jsr SETPIXELCLIP ; plot(xs+y,ys+x) lda YS sec sbc XX sta Y1 sta Y3 lda YS+1 sbc XX+1 sta Y1+1 ; y3 = y1 = ys-x sta Y3+1 jsr SETPIXELCLIP ; plot(xs+y,ys-x) pla sta Y1+1 pla sta Y1 ; y1 = ys+x(stack) lda XS sec sbc YY sta X1 lda XS+1 sbc YY+1 sta X1+1 jsr SETPIXELCLIP ; plot (xs-y,ys+x) lda Y3 sta Y1 lda Y3+1 sta Y1+1 jsr SETPIXELCLIP ; plot (xs-y,ys-x) ; og = mo+y+y+1 lda MaxO ldx MaxO+1 clc adc YY tay txa adc YY+1 tax tya clc adc YY tay txa adc YY+1 tax tya clc adc #1 bcc @L0143 inx @L0143: sta OGora stx OGora+1 ; ou = og-x-x+1 sec sbc XX tay txa sbc XX+1 tax tya sec sbc XX tay txa sbc XX+1 tax tya clc adc #1 bcc @L0146 inx @L0146: sta OUkos stx OUkos+1 ; ++y inc YY bne @L0148 inc YY+1 @L0148: ; if (abs(ou)<abs(og)) lda OUkos ldy OUkos+1 jsr abs sta TEMP3 sty TEMP4 lda OGora ldy OGora+1 jsr abs ldx #TEMP3 jsr icmp bpl @L0149 ; { --x; sec lda XX sbc #1 sta XX bcs @L014E dec XX+1 @L014E: ; mo = ou; } lda OUkos ldx OUkos+1 jmp @L014G ; else { mo = og } @L0149: lda OGora ldx OGora+1 @L014G: sta MaxO stx MaxO+1 ; } jmp @L013B ; ------------------------------------------------------------------------ ; TEXTSTYLE: Set the style used when calling OUTTEXT. Text scaling in X and Y ; direction is passend in X/Y, the text direction is passed in A. ; ; Must set an error code: NO ; TEXTSTYLE: stx TEXTMAGX sty TEXTMAGY sta TEXTDIR rts ; ------------------------------------------------------------------------ ; OUTTEXT: Output text at X/Y = ptr1/ptr2 using the current color and the ; current text style. The text to output is given as a zero terminated ; string with address in ptr3. ; ; Must set an error code: NO ; OUTTEXT: rts ; ------------------------------------------------------------------------ ; Calculate all variables to plot the pixel at X1/Y1. ;------------------------ ;< X1,Y1 - pixel ;> ADDR - address of card ;> X - bit number (X1 & 7) CALC: lda Y1 pha lda Y1+1 pha lsr ror Y1 ; Y=Y/2 sta Y1+1 sta ADDR+1 lda Y1 asl rol ADDR+1 asl rol ADDR+1 ; Y*4 clc adc Y1 sta ADDR lda Y1+1 adc ADDR+1 sta ADDR+1 ; Y*4+Y=Y*5 lda ADDR asl rol ADDR+1 asl rol ADDR+1 asl rol ADDR+1 asl rol ADDR+1 sta ADDR ; Y*5*16=Y*80 lda X1+1 sta TEMP lda X1 lsr TEMP ror lsr TEMP ror lsr TEMP ror clc adc ADDR sta ADDR lda ADDR+1 ; ADDR = Y*80+x/8 adc TEMP sta ADDR+1 pla sta Y1+1 pla sta Y1 and #1 beq @even ; even line - no offset lda ADDR clc adc #<21360 sta ADDR lda ADDR+1 adc #>21360 sta ADDR+1 ; odd lines are 21360 bytes farther @even: lda X1 and #7 tax rts ;------------- ; copies of some runtime routines abs: ; a/y := abs(a/y) cpy #$00 bpl absend ; negay neg: eor #$ff add #1 pha tya eor #$ff adc #0 tay pla absend: rts icmp: ; compare a/y to zp,x sta TEMP ; TEMP/TEMP2 - arg2 sty TEMP2 lda $0,x pha lda $1,x tay pla tax tya ; x/a - arg1 (a=high) sec sbc TEMP2 bne @L4 cpx TEMP beq @L3 adc #$ff ora #$01 @L3: rts @L4: bvc @L3 eor #$ff ora #$01 rts ;------------- ; VDC helpers VDCSetSourceAddr: pha tya ldx #VDC_DATA_HI jsr VDCWriteReg pla ldx #VDC_DATA_LO bne VDCWriteReg VDCReadByte: ldx #VDC_DATA VDCReadReg: stx VDC_ADDR_REG @L0: bit VDC_ADDR_REG bpl @L0 lda VDC_DATA_REG rts VDCWriteByte: ldx #VDC_DATA VDCWriteReg: stx VDC_ADDR_REG @L0: bit VDC_ADDR_REG bpl @L0 sta VDC_DATA_REG rts
wagiminator/C64-Collection
2,902
C64_xu1541/software/tools/cc65-2.13.2/libsrc/c128/mcbdefault.s
; ; Default mouse callbacks for the C64 ; ; Ullrich von Bassewitz, 2004-03-20 ; ; All functions in this module should be interrupt safe, because they may ; be called from an interrupt handler ; .export _mouse_def_callbacks .include "mouse-kernel.inc" .include "c128.inc" .macpack generic ; Sprite definitions. The first value can be changed to adjust the number ; of the sprite used for the mouse. MOUSE_SPR = 0 ; Sprite used for the mouse MOUSE_SPR_MASK = $01 .shl MOUSE_SPR ; Positive mask MOUSE_SPR_NMASK = .lobyte(.not MOUSE_SPR_MASK) ; Negative mask VIC_SPR_X = (VIC_SPR0_X + 2*MOUSE_SPR) ; Sprite X register VIC_SPR_Y = (VIC_SPR0_Y + 2*MOUSE_SPR) ; Sprite Y register .code ; -------------------------------------------------------------------------- ; Hide the mouse pointer. Always called with interrupts disabled. .proc hide lda #MOUSE_SPR_NMASK and VIC_SPR_ENA sta VIC_SPR_ENA rts .endproc ; -------------------------------------------------------------------------- ; Show the mouse pointer. Always called with interrupts disabled. .proc show lda #MOUSE_SPR_MASK ora VIC_SPR_ENA sta VIC_SPR_ENA rts .endproc ; -------------------------------------------------------------------------- ; Move the mouse pointer X position to the value in a/x. Always called with ; interrupts disabled. .proc movex ; Add the X correction and set the low byte. This frees A. add #24 ; X correction sta VIC_SPR_X ; Set the high byte txa adc #0 bne @L1 ; Branch if high byte not zero lda VIC_SPR_HI_X ; Get high X bits of all sprites and #MOUSE_SPR_NMASK ; Clear high bit for sprite sta VIC_SPR_HI_X rts @L1: lda VIC_SPR_HI_X ; Get high X bits of all sprites ora #MOUSE_SPR_MASK ; Set high bit for sprite sta VIC_SPR_HI_X rts .endproc ; -------------------------------------------------------------------------- ; Move the mouse pointer Y position to the value in a/x. Always called with ; interrupts disabled. .proc movey clc ldx PALFLAG bne @L1 adc #50 ; FIXME: Should be NTSC, is PAL value sta VIC_SPR_Y ; Set Y position rts @L1: adc #50 ; Add PAL correction sta VIC_SPR_Y ; Set Y position rts .endproc ; -------------------------------------------------------------------------- ; Callback structure .rodata _mouse_def_callbacks: .addr hide .addr show .addr movex .addr movey
wagiminator/C64-Collection
4,023
C64_xu1541/software/tools/cc65-2.13.2/libsrc/c128/mainargs.s
; mainargs.s ; ; Ullrich von Bassewitz, 2003-03-07 ; Based on code from Stefan A. Haubenthal, <polluks@web.de> ; 2003-05-18, Greg King ; 2004-04-28, 2005-02-26, Ullrich von Bassewitz ; ; Scan a group of arguments that are in BASIC's input-buffer. ; Build an array that points to the beginning of each argument. ; Send, to main(), that array and the count of the arguments. ; ; Command-lines look like these lines: ; ; run ; run : rem ; run:rem arg1 " arg 2 is quoted " arg3 "" arg5 ; ; "run" and "rem" are entokenned; the args. are not. Leading and trailing ; spaces outside of quotes are ignored. ; ; TO-DO: ; - The "file-name" might be a path-name; don't copy the directory-components. ; - Add a control-character quoting mechanism. .constructor initmainargs, 24 .import __argc, __argv .include "c128.inc" MAXARGS = 10 ; Maximum number of arguments allowed REM = $8f ; BASIC token-code NAME_LEN = 16 ; maximum length of command-name ; Get possible command-line arguments. Goes into the special INIT segment, ; which may be reused after the startup code is run .segment "INIT" initmainargs: ; Assume that the program was loaded, a moment ago, by the traditional LOAD ; statement. Save the "most-recent filename" as argument #0. ; Because the buffer, that we're copying into, was zeroed out, ; we don't need to add a NUL character. ; ldy FNAM_LEN cpy #NAME_LEN + 1 bcc L1 ldy #NAME_LEN - 1 ; limit the length L0: lda #FNAM ; Load vector address for FETCH routine ldx FNAM_BANK ; Load bank for FETCH routine jsr INDFET ; Load byte from (FETVEC),y sta name,y ; Save byte from filename L1: dey bpl L0 inc __argc ; argc always is equal to, at least, 1 ; Find the "rem" token. ; ldx #0 L2: lda BASIC_BUF,x beq done ; no "rem," no args. inx cmp #REM bne L2 ldy #1 * 2 ; Find the next argument next: lda BASIC_BUF,x beq done ; End of line reached inx cmp #' ' ; Skip leading spaces beq next ; ; Found start of next argument. We've incremented the pointer in X already, so ; it points to the second character of the argument. This is useful since we ; will check now for a quoted argument, in which case we will have to skip this ; first character. found: cmp #'"' ; Is the argument quoted? beq setterm ; Jump if so dex ; Reset pointer to first argument character lda #' ' ; A space ends the argument setterm:sta term ; Set end of argument marker ; Now store a pointer to the argument into the next slot. Since the BASIC ; input buffer is located at the start of a RAM page, no calculations are ; necessary. txa ; Get low byte sta argv,y ; argv[y]= &arg iny lda #>BASIC_BUF sta argv,y iny inc __argc ; Found another arg ; Search for the end of the argument argloop:lda BASIC_BUF,x beq done inx cmp term bne argloop ; We've found the end of the argument. X points one character behind it, and ; A contains the terminating character. To make the argument a valid C string, ; replace the terminating character by a zero. lda #0 sta BASIC_BUF-1,x ; Check if the maximum number of command line arguments is reached. If not, ; parse the next one. lda __argc ; Get low byte of argument count cmp #MAXARGS ; Maximum number of arguments reached? bcc next ; Parse next one if not ; (The last vector in argv[] already is NULL.) done: lda #<argv ldx #>argv sta __argv stx __argv + 1 rts ; These arrays are zeroed before initmainargs is called. ; char name[16+1]; ; char* argv[MAXARGS+1]={name}; ; .bss term: .res 1 name: .res NAME_LEN + 1 .data argv: .addr name .res MAXARGS * 2
wagiminator/C64-Collection
1,954
C64_xu1541/software/tools/cc65-2.13.2/libsrc/c128/cputc.s
; ; Ullrich von Bassewitz, 2000-08-06, 2002-12-21 ; Using lots of code from MagerValp, MagerValp@cling.gu.se ; ; void cputcxy (unsigned char x, unsigned char y, char c); ; void cputc (char c); ; .export _cputcxy, _cputc, cputdirect, putchar .export newline, plot .constructor initcputc .destructor donecputc .import popa, _gotoxy .import PLOT .include "c128.inc" cputdirect = PRINT newline = NEWLINE ;-------------------------------------------------------------------------- .code _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 #$0A ; CR? beq cr ; Output a cr cmp #$0D ; LF? bne L2 jmp NEWLINE ; Update cursor position ; Printable char of some sort L2: cmp #' ' bcc L4 ; Other control char tay bmi L5 cmp #$60 bcc L3 and #$DF bne L4 ; Branch always L3: and #$3F L4: jmp PRINT ; Output character ; Handle character if high bit set L5: and #$7F cmp #$7E ; PI? bne L6 lda #$5E ; Load screen code for PI bne L4 L6: ora #$40 bne L4 ; Branch always ; Carriage return cr: lda #0 sta CURS_X ; 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 = $CC2F ;-------------------------------------------------------------------------- ; Module constructor/destructor. Don't move the constructor into the INIT ; segment, because it shares most of the code with the destructor. initcputc: lda #$C0 .byte $2C donecputc: lda #$00 sta SCROLL rts
wagiminator/C64-Collection
3,796
C64_xu1541/software/tools/cc65-2.13.2/libsrc/c128/c128-ptvjoy.s
; ; PTV-4 Player joystick driver for the C128 ; ; Ullrich von Bassewitz, 2003-09-28, using the C64 driver from ; Groepaz/Hitmen, 2002-12-23, which is ; obviously based on Ullrichs driver :) ; .include "zeropage.inc" .include "joy-kernel.inc" .include "joy-error.inc" .include "c128.inc" .macpack generic ; ------------------------------------------------------------------------ ; Header. Includes jump table .segment "JUMPTABLE" ; Driver signature .byte $6A, $6F, $79 ; "joy" .byte JOY_API_VERSION ; Driver API version number ; Button state masks (8 values) .byte $01 ; JOY_UP .byte $02 ; JOY_DOWN .byte $04 ; JOY_LEFT .byte $08 ; JOY_RIGHT .byte $10 ; JOY_FIRE .byte $00 ; JOY_FIRE2 unavailable .byte $00 ; Future expansion .byte $00 ; Future expansion ; Jump table. .addr INSTALL .addr UNINSTALL .addr COUNT .addr READ .addr 0 ; IRQ entry unused ; ------------------------------------------------------------------------ ; Constants JOY_COUNT = 4 ; Number of joysticks we support .code ; ------------------------------------------------------------------------ ; INSTALL routine. Is called after the driver is loaded into memory. If ; possible, check if the hardware is present and determine the amount of ; memory available. ; Must return an JOY_ERR_xx code in a/x. ; INSTALL: lda #<JOY_ERR_OK ldx #>JOY_ERR_OK ; rts ; Run into UNINSTALL instead ; ------------------------------------------------------------------------ ; UNINSTALL routine. Is called before the driver is removed from memory. ; Can do cleanup or whatever. Must not return anything. ; UNINSTALL: rts ; ------------------------------------------------------------------------ ; COUNT: Return the total number of available joysticks in a/x. ; COUNT: lda #<JOY_COUNT ldx #>JOY_COUNT rts ; ------------------------------------------------------------------------ ; READ: Read a particular joystick passed in A. ; READ: 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: dex bne joy3 lda #$E0 ldy #$FF sei sta CIA1_DDRA lda CIA1_PRA sty CIA1_DDRA cli and #$1F eor #$1F rts ; Read joystick 3 joy3: lda #%10000000 ; cia 2 port B Data-Direction sta CIA2_DDRB ; bit 7: out bit 6-0: in dex bne joy4 lda #$80 ; cia 2 port B read/write sta CIA2_PRB ; (output one at PB7) lda CIA2_PRB ; cia 2 port B read/write and #$1f ; get bit 4-0 (PB4-PB0) eor #$1f rts ; Read joystick 4 joy4: lda #$00 ; cia 2 port B read/write sta CIA2_PRB ; (output zero at PB7) lda CIA2_PRB ; cia 2 port B read/write and #$0f ; get bit 3-0 (PB3-PB0) sta tmp1 ; joy 4 directions lda CIA2_PRB ; cia 2 port B read/write and #%00100000 ; get bit 5 (PB5) lsr ora tmp1 eor #$1f ldx #0 rts
wagiminator/C64-Collection
23,225
C64_xu1541/software/tools/cc65-2.13.2/libsrc/c128/c128-640-200-2.s
; ; Graphics driver for the 640x200x2 mode on the C128 VDC ; Maciej 'YTM/Elysium' Witkowiak <ytm@elysium.pl> ; 23.12.2002 ; 2004-04-04, Greg King ; ; NOTES: ; For any smart monkey that will try to optimize this: PLEASE do tests on ; real VDC, not only VICE. ; ; Only DONE routine contains C128-mode specific stuff, everything else will ; work in C64-mode of C128 (C64 needs full VDC init then). ; ; With special initialization and CALC we can get 320x200 double-pixel mode. ; ; Color translation values for BROWN and GRAY3 are obviously wrong, they ; could be replaced by equiv. of ORANGE and GRAY2 but this would give only ; 14 of 16 colors available. ; ; Register 25 ($19) is said to require different value for VDC v1, but I ; couldn't find what it should be. .include "zeropage.inc" .include "tgi-kernel.inc" .include "tgi-mode.inc" .include "tgi-error.inc" .macpack generic ; ------------------------------------------------------------------------ ; Constants VDC_ADDR_REG = $D600 ; VDC address VDC_DATA_REG = $D601 ; VDC data VDC_DSP_HI = 12 ; registers used VDC_DSP_LO = 13 VDC_DATA_HI = 18 VDC_DATA_LO = 19 VDC_VSCROLL = 24 VDC_HSCROLL = 25 VDC_COLORS = 26 VDC_CSET = 28 VDC_COUNT = 30 VDC_DATA = 31 ; ------------------------------------------------------------------------ ; Header. Includes jump table and constants. .segment "JUMPTABLE" ; First part of the header is a structure that has a magic and defines the ; capabilities of the driver .byte $74, $67, $69 ; "tgi" .byte TGI_API_VERSION ; TGI API version number xres: .word 640 ; X resolution yres: .word 200 ; Y resolution .byte 2 ; Number of drawing colors pages: .byte 1 ; Number of screens available .byte 8 ; System font X size .byte 8 ; System font Y size .res 4, $00 ; Reserved for future extensions ; Next comes the jump table. Currently all entries must be valid and may point ; to an RTS for test versions (function not implemented). .addr INSTALL .addr UNINSTALL .addr INIT .addr DONE .addr GETERROR .addr CONTROL .addr CLEAR .addr SETVIEWPAGE .addr SETDRAWPAGE .addr SETCOLOR .addr SETPALETTE .addr GETPALETTE .addr GETDEFPALETTE .addr SETPIXEL .addr GETPIXEL .addr LINE .addr BAR .addr CIRCLE .addr TEXTSTYLE .addr OUTTEXT .addr 0 ; IRQ entry is unused ; ------------------------------------------------------------------------ ; Data. ; Variables mapped to the zero page segment variables. Some of these are ; used for passing parameters to the driver. X1 = ptr1 Y1 = ptr2 X2 = ptr3 Y2 = ptr4 RADIUS = tmp1 ADDR = tmp1 ; (2) CALC TEMP = tmp3 ; CALC icmp TEMP2 = tmp4 ; icmp TEMP3 = sreg ; LINE TEMP4 = sreg+1 ; LINE ; Line routine stuff (must be on zpage) PB = ptr3 ; (2) LINE UB = ptr4 ; (2) LINE ERR = regsave ; (2) LINE NX = regsave+2 ; (2) LINE ; Circle stuff XX = ptr3 ; (2) CIRCLE YY = ptr4 ; (2) CIRCLE MaxO = sreg ; (overwritten by TEMP3+TEMP4, but restored from OG/OU anyway) XS = regsave ; (2) CIRCLE YS = regsave+2 ; (2) CIRCLE ; Absolute variables used in the code .bss SCRBASE: .res 1 ; High byte of screen base ERROR: .res 1 ; Error code PALETTE: .res 2 ; The current palette BITMASK: .res 1 ; $00 = clear, $FF = set pixels OLDCOLOR: .res 1 ; colors before entering gfx mode ; Line routine stuff (combined with CIRCLE to save space) OGora: COUNT: .res 2 OUkos: NY: .res 2 Y3: DX: .res 1 DY: .res 1 AX: .res 1 AY: .res 1 ; Text output stuff TEXTMAGX: .res 1 TEXTMAGY: .res 1 TEXTDIR: .res 1 ; Constants and tables .rodata DEFPALETTE: .byte $00, $0f ; White on black PALETTESIZE = * - DEFPALETTE BITTAB: .byte $80,$40,$20,$10,$08,$04,$02,$01 BITMASKL: .byte %11111111, %01111111, %00111111, %00011111 .byte %00001111, %00000111, %00000011, %00000001 BITMASKR: .byte %10000000, %11000000, %11100000, %11110000 .byte %11111000, %11111100, %11111110, %11111111 ; color translation table (indexed by VIC color) COLTRANS: .byte $00, $0f, $08, $06, $0a, $04, $02, $0c .byte $0d, $0b, $09, $01, $0e, $05, $03, $07 ; colors BROWN and GRAY3 are wrong ; VDC initialization table (reg),(val),...,$ff InitVDCTab: .byte VDC_DSP_HI, 0 ; viewpage 0 as default .byte VDC_DSP_LO, 0 .byte VDC_HSCROLL, $87 .byte $ff SCN80CLR: .byte 27,88,147,27,88,0 .code ; ------------------------------------------------------------------------ ; INSTALL routine. Is called after the driver is loaded into memory. May ; initialize anything that has to be done just once. Is probably empty ; most of the time. ; ; Must set an error code: NO ; INSTALL: ; check for VDC version and update register $19 value ; check for VDC ram size and update number of available screens ldx #VDC_CSET ; determine size of RAM... jsr VDCReadReg sta tmp1 ora #%00010000 jsr VDCWriteReg ; turn on 64k jsr settestadr1 ; save original value of test byte jsr VDCReadByte sta tmp2 lda #$55 ; write $55 here ldy #ptr1 jsr test64k ; read it here and there lda #$aa ; write $aa here ldy #ptr2 jsr test64k ; read it here and there jsr settestadr1 lda tmp2 jsr VDCWriteByte ; restore original value of test byte lda ptr1 ; do bytes match? cmp ptr1+1 bne @have64k lda ptr2 cmp ptr2+1 bne @have64k ldx #VDC_CSET lda tmp1 jsr VDCWriteReg ; restore 16/64k flag jmp @endok ; and leave default values for 16k @have64k: lda #4 sta pages @endok: lda #0 sta SCRBASE ; draw page 0 as default rts test64k: sta tmp1 sty ptr3 lda #0 sta ptr3+1 jsr settestadr1 lda tmp1 jsr VDCWriteByte ; write $55 jsr settestadr1 jsr VDCReadByte ; read here pha jsr settestadr2 jsr VDCReadByte ; and there ldy #1 sta (ptr3),y pla dey sta (ptr3),y rts settestadr1: ldy #$02 ; test page 2 (here) .byte $2c settestadr2: ldy #$42 ; or page 64+2 (there) lda #0 jmp VDCSetSourceAddr ; ------------------------------------------------------------------------ ; UNINSTALL routine. Is called before the driver is removed from memory. May ; clean up anything done by INSTALL but is probably empty most of the time. ; ; Must set an error code: NO ; UNINSTALL: rts ; ------------------------------------------------------------------------ ; INIT: Changes an already installed device from text mode to graphics ; mode. ; Note that INIT/DONE may be called multiple times while the driver ; is loaded, while INSTALL is only called once, so any code that is needed ; to initializes variables and so on must go here. Setting palette and ; clearing the screen is not needed because this is called by the graphics ; kernel later. ; The graphics kernel will never call INIT when a graphics mode is already ; active, so there is no need to protect against that. ; ; Must set an error code: YES ; INIT: ; Initialize variables @L1: ldx #$FF stx BITMASK ; Remeber current color value ldx #VDC_COLORS jsr VDCReadReg sta OLDCOLOR ; Switch into graphics mode (set view page 0) ldy #0 @L2: ldx InitVDCTab,y bmi @L3 iny lda InitVDCTab,y jsr VDCWriteReg iny bne @L2 @L3: ; Done, reset the error code lda #TGI_ERR_OK sta ERROR rts ; ------------------------------------------------------------------------ ; DONE: Will be called to switch the graphics device back into text mode. ; The graphics kernel will never call DONE when no graphics mode is active, ; so there is no need to protect against that. ; ; Must set an error code: NO ; DONE: ; This part is C128-mode specific jsr $e179 ; reload character set and setup VDC jsr $ff62 lda $d7 ; in 80-columns? bne @L01 @L0: lda SCN80CLR,y beq @L1 jsr $ffd2 ; print \xe,clr,\xe iny bne @L0 @L01: lda #147 jsr $ffd2 ; print clr @L1: lda #0 ; restore view page ldx #VDC_DSP_HI jsr VDCWriteReg lda OLDCOLOR ldx #VDC_COLORS jsr VDCWriteReg ; restore color (background) lda #$47 ldx #VDC_HSCROLL jmp VDCWriteReg ; switch to text screen ; ------------------------------------------------------------------------ ; GETERROR: Return the error code in A and clear it. GETERROR: ldx #TGI_ERR_OK lda ERROR stx ERROR rts ; ------------------------------------------------------------------------ ; CONTROL: Platform/driver specific entry point. ; ; Must set an error code: YES ; CONTROL: lda #TGI_ERR_INV_FUNC sta ERROR rts ; ------------------------------------------------------------------------ ; CLEAR: Clears the screen. ; ; Must set an error code: NO ; CLEAR: lda #0 ldy SCRBASE jsr VDCSetSourceAddr lda #0 ldx #VDC_VSCROLL jsr VDCWriteReg ; set fill mode lda #0 jsr VDCWriteByte ; put 1rst byte (fill value) ldy #62 ; 62 times lda #0 ; 256 bytes ldx #VDC_COUNT @L1: jsr VDCWriteReg dey bne @L1 lda #127 jmp VDCWriteReg ; 1+62*256+127=16000=(640*256)/8 ; ------------------------------------------------------------------------ ; SETVIEWPAGE: Set the visible page. Called with the new page in A (0..n). ; The page number is already checked to be valid by the graphics kernel. ; ; Must set an error code: NO (will only be called if page ok) ; SETVIEWPAGE: clc ror ror ror ldx #VDC_DSP_HI jmp VDCWriteReg ; ------------------------------------------------------------------------ ; SETDRAWPAGE: Set the drawable page. Called with the new page in A (0..n). ; The page number is already checked to be valid by the graphics kernel. ; ; Must set an error code: NO (will only be called if page ok) ; SETDRAWPAGE: clc ror ror ror sta SCRBASE rts ; ------------------------------------------------------------------------ ; SETCOLOR: Set the drawing color (in A). The new color is already checked ; to be in a valid range (0..maxcolor-1). ; ; Must set an error code: NO (will only be called if color ok) ; SETCOLOR: tax beq @L1 lda #$FF @L1: sta BITMASK rts ; ------------------------------------------------------------------------ ; SETPALETTE: Set the palette (not available with all drivers/hardware). ; A pointer to the palette is passed in ptr1. Must set an error if palettes ; are not supported ; ; Must set an error code: YES ; SETPALETTE: ldy #PALETTESIZE - 1 @L1: lda (ptr1),y ; Copy the palette and #$0F ; Make a valid color sta PALETTE,y dey bpl @L1 ; Get the color entries from the palette ldy PALETTE+1 ; Foreground color lda COLTRANS,y asl a asl a asl a asl a ldy PALETTE ; Background color ora COLTRANS,y ldx #VDC_COLORS jsr VDCWriteReg ; Clear error code lda #TGI_ERR_OK sta ERROR rts ; ------------------------------------------------------------------------ ; GETPALETTE: Return the current palette in A/X. Even drivers that cannot ; set the palette should return the default palette here, so there's no ; way for this function to fail. ; ; Must set an error code: NO ; GETPALETTE: lda #<PALETTE ldx #>PALETTE rts ; ------------------------------------------------------------------------ ; GETDEFPALETTE: Return the default palette for the driver in A/X. All ; drivers should return something reasonable here, even drivers that don't ; support palettes, otherwise the caller has no way to determine the colors ; of the (not changeable) palette. ; ; Must set an error code: NO (all drivers must have a default palette) ; GETDEFPALETTE: lda #<DEFPALETTE ldx #>DEFPALETTE rts ; ------------------------------------------------------------------------ ; SETPIXEL: Draw one pixel at X1/Y1 = ptr1/ptr2 with the current drawing ; color. The coordinates passed to this function are never outside the ; visible screen area, so there is no need for clipping inside this function. ; ; Must set an error code: NO ; SETPIXELCLIP: lda Y1+1 bmi @finito ; y<0 lda X1+1 bmi @finito ; x<0 lda X1 ldx X1+1 sta ADDR stx ADDR+1 ldx #ADDR lda xres ldy xres+1 jsr icmp ; ( x < xres ) ... bcs @finito lda Y1 ldx Y1+1 sta ADDR stx ADDR+1 ldx #ADDR lda yres ldy yres+1 jsr icmp ; ... && ( y < yres ) bcc SETPIXEL @finito:rts SETPIXEL: jsr CALC ; Calculate coordinates stx TEMP lda ADDR ldy ADDR+1 jsr VDCSetSourceAddr jsr VDCReadByte ldx TEMP sta TEMP eor BITMASK and BITTAB,X eor TEMP pha lda ADDR ldy ADDR+1 jsr VDCSetSourceAddr pla jsr VDCWriteByte @L9: rts ; ------------------------------------------------------------------------ ; GETPIXEL: Read the color value of a pixel and return it in A/X. The ; coordinates passed to this function are never outside the visible screen ; area, so there is no need for clipping inside this function. GETPIXEL: jsr CALC ; Calculate coordinates stx TEMP ; preserve X lda ADDR ldy ADDR+1 jsr VDCSetSourceAddr jsr VDCReadByte ldx TEMP ldy #$00 and BITTAB,X beq @L1 iny @L1: tya ; Get color value into A ldx #$00 ; Clear high byte rts ; ------------------------------------------------------------------------ ; LINE: Draw a line from X1/Y1 to X2/Y2, where X1/Y1 = ptr1/ptr2 and ; X2/Y2 = ptr3/ptr4 using the current drawing color. ; ; Must set an error code: NO ; LINE: ; nx = abs(x2 - x1) lda X2 sec sbc X1 sta NX lda X2+1 sbc X1+1 tay lda NX jsr abs sta NX sty NX+1 ; ny = abs(y2 - y1) lda Y2 sec sbc Y1 sta NY lda Y2+1 sbc Y1+1 tay lda NY jsr abs sta NY sty NY+1 ; if (x2>=x1) ldx #X2 lda X1 ldy X1+1 jsr icmp bcc @L0243 ; dx = 1; lda #1 bne @L0244 ; else ; dx = -1; @L0243: lda #$ff @L0244: sta DX ; if (y2>=y1) ldx #Y2 lda Y1 ldy Y1+1 jsr icmp bcc @L024A ; dy = 1; lda #1 bne @L024B ; else ; dy = -1; @L024A: lda #$ff @L024B: sta DY ; err = ax = ay = 0; lda #0 sta ERR sta ERR+1 sta AX sta AY ; if (nx<ny) { ldx #NX lda NY ldy NY+1 jsr icmp bcs @L0255 ; nx <-> ny lda NX ldx NY sta NY stx NX lda NX+1 ldx NY+1 sta NY+1 stx NX+1 ; ax = dx lda DX sta AX ; ay = dy lda DY sta AY ; dx = dy = 0; lda #0 sta DX sta DY ; ny = - ny; @L0255: lda NY ldy NY+1 jsr neg sta NY sty NY+1 ; for (count=nx;count>0;--count) { lda NX ldx NX+1 sta COUNT stx COUNT+1 @L0166: lda COUNT ; count>0 ora COUNT+1 bne @L0167 rts ; setpixel(X1,Y1) @L0167: jsr SETPIXELCLIP ; pb = err + ny lda ERR clc adc NY sta PB lda ERR+1 adc NY+1 sta PB+1 tax ; ub = pb + nx lda PB clc adc NX sta UB txa adc NX+1 sta UB+1 ; x1 = x1 + dx ldx #0 lda DX bpl @L027B dex @L027B: clc adc X1 sta X1 txa adc X1+1 sta X1+1 ; y1 = y1 + ay ldx #0 lda AY bpl @L027E dex @L027E: clc adc Y1 sta Y1 txa adc Y1+1 sta Y1+1 ; if (abs(pb)<abs(ub)) { lda PB ldy PB+1 jsr abs sta TEMP3 sty TEMP4 lda UB ldy UB+1 jsr abs ldx #TEMP3 jsr icmp bpl @L027F ; err = pb lda PB ldx PB+1 jmp @L0312 ; } else { x1 = x1 + ax @L027F: ldx #0 lda AX bpl @L0288 dex @L0288: clc adc X1 sta X1 txa adc X1+1 sta X1+1 ; y1 = y1 + dy ldx #0 lda DY bpl @L028B dex @L028B: clc adc Y1 sta Y1 txa adc Y1+1 sta Y1+1 ; err = ub } lda UB ldx UB+1 @L0312: sta ERR stx ERR+1 ; } (--count) sec lda COUNT sbc #1 sta COUNT bcc @L0260 jmp @L0166 @L0260: dec COUNT+1 jmp @L0166 ; ------------------------------------------------------------------------ ; BAR: Draw a filled rectangle with the corners X1/Y1, X2/Y2, where ; X1/Y1 = ptr1/ptr2 and X2/Y2 = ptr3/ptr4 using the current drawing color. ; Contrary to most other functions, the graphics kernel will sort and clip ; the coordinates before calling the driver, so on entry the following ; conditions are valid: ; X1 <= X2 ; Y1 <= Y2 ; (X1 >= 0) && (X1 < XRES) ; (X2 >= 0) && (X2 < XRES) ; (Y1 >= 0) && (Y1 < YRES) ; (Y2 >= 0) && (Y2 < YRES) ; ; Must set an error code: NO ; BAR: inc Y2 bne HORLINE inc Y2+1 ; Original code for a horizontal line HORLINE: lda X1 pha lda X1+1 pha jsr CALC ; get data for LEFT lda BITMASKL,x ; remember left address and bitmask pha lda ADDR pha lda ADDR+1 pha lda X2 sta X1 lda X2+1 sta X1+1 jsr CALC ; get data for RIGHT lda BITMASKR,x sta TEMP3 pla ; recall data for LEFT sta X1+1 pla sta X1 ; put left address into X1 pla cmp #%11111111 ; if left bit <> 0 beq @L1 sta TEMP2 ; do left byte only... lda X1 ldy X1+1 jsr VDCSetSourceAddr jsr VDCReadByte sta TEMP eor BITMASK and TEMP2 eor TEMP pha lda X1 ldy X1+1 jsr VDCSetSourceAddr pla jsr VDCWriteByte inc X1 ; ... and proceed bne @L1 inc X1+1 ; do right byte (if Y2=0 ++ADDR and skip) @L1: lda TEMP3 cmp #%11111111 ; if right bit <> 7 bne @L11 inc ADDR ; right bit = 7 - the next one is the last bne @L10 inc ADDR+1 @L10: bne @L2 @L11: lda ADDR ; do right byte only... ldy ADDR+1 jsr VDCSetSourceAddr jsr VDCReadByte sta TEMP eor BITMASK and TEMP3 eor TEMP pha lda ADDR ldy ADDR+1 jsr VDCSetSourceAddr pla jsr VDCWriteByte @L2: ; do the fill in the middle lda ADDR ; calculate offset in full bytes sec sbc X1 beq @L3 ; if equal - there are no more bytes sta ADDR lda X1 ; setup for the left side ldy X1+1 jsr VDCSetSourceAddr lda BITMASK ; get color jsr VDCWriteByte ; put 1st value ldx ADDR dex beq @L3 ; 1 byte already written stx ADDR ; if there are more bytes - fill them... ldx #VDC_VSCROLL lda #0 jsr VDCWriteReg ; setup for fill ldx #VDC_COUNT lda ADDR jsr VDCWriteReg ; ... fill them NOW! @L3: pla sta X1+1 pla sta X1 ; End of horizontal line code inc Y1 bne @L4 inc Y1+1 @L4: lda Y1 cmp Y2 bne @L5 lda Y1+1 cmp Y2+1 bne @L5 rts @L5: jmp HORLINE ; ------------------------------------------------------------------------ ; CIRCLE: Draw a circle around the center X1/Y1 (= ptr1/ptr2) with the ; radius in tmp1 and the current drawing color. ; ; Must set an error code: NO ; CIRCLE: lda RADIUS bne @L1 jmp SETPIXELCLIP ; Plot as a point @L1: sta XX ; x = r; lda #0 sta XX+1 sta YY sta YY+1 sta MaxO sta MaxO+1 ; y =0; mo=0; lda X1 ldx X1+1 sta XS stx XS+1 lda Y1 ldx Y1+1 sta YS stx YS+1 ; XS/YS to remember the center ; while (y<x) { @L013B: ldx #YY lda XX ldy XX+1 jsr icmp bcc @L12 rts @L12: ; plot points in 8 slices... lda XS clc adc XX sta X1 lda XS+1 adc XX+1 sta X1+1 ; x1 = xs+x lda YS clc adc YY sta Y1 pha lda YS+1 adc YY+1 sta Y1+1 ; (stack)=ys+y, y1=(stack) pha jsr SETPIXELCLIP ; plot(xs+x,ys+y) lda YS sec sbc YY sta Y1 sta Y3 lda YS+1 sbc YY+1 sta Y1+1 ; y3 = y1 = ys-y sta Y3+1 jsr SETPIXELCLIP ; plot(xs+x,ys-y) pla sta Y1+1 pla sta Y1 ; y1 = ys+y lda XS sec sbc XX sta X1 lda XS+1 sbc XX+1 sta X1+1 jsr SETPIXELCLIP ; plot (xs-x,ys+y) lda Y3 sta Y1 lda Y3+1 sta Y1+1 jsr SETPIXELCLIP ; plot (xs-x,ys-y) lda XS clc adc YY sta X1 lda XS+1 adc YY+1 sta X1+1 ; x1 = xs+y lda YS clc adc XX sta Y1 pha lda YS+1 adc XX+1 sta Y1+1 ; (stack)=ys+x, y1=(stack) pha jsr SETPIXELCLIP ; plot(xs+y,ys+x) lda YS sec sbc XX sta Y1 sta Y3 lda YS+1 sbc XX+1 sta Y1+1 ; y3 = y1 = ys-x sta Y3+1 jsr SETPIXELCLIP ; plot(xs+y,ys-x) pla sta Y1+1 pla sta Y1 ; y1 = ys+x(stack) lda XS sec sbc YY sta X1 lda XS+1 sbc YY+1 sta X1+1 jsr SETPIXELCLIP ; plot (xs-y,ys+x) lda Y3 sta Y1 lda Y3+1 sta Y1+1 jsr SETPIXELCLIP ; plot (xs-y,ys-x) ; og = mo+y+y+1 lda MaxO ldx MaxO+1 clc adc YY tay txa adc YY+1 tax tya clc adc YY tay txa adc YY+1 tax tya clc adc #1 bcc @L0143 inx @L0143: sta OGora stx OGora+1 ; ou = og-x-x+1 sec sbc XX tay txa sbc XX+1 tax tya sec sbc XX tay txa sbc XX+1 tax tya clc adc #1 bcc @L0146 inx @L0146: sta OUkos stx OUkos+1 ; ++y inc YY bne @L0148 inc YY+1 @L0148: ; if (abs(ou)<abs(og)) lda OUkos ldy OUkos+1 jsr abs sta TEMP3 sty TEMP4 lda OGora ldy OGora+1 jsr abs ldx #TEMP3 jsr icmp bpl @L0149 ; { --x; sec lda XX sbc #1 sta XX bcs @L014E dec XX+1 @L014E: ; mo = ou; } lda OUkos ldx OUkos+1 jmp @L014G ; else { mo = og } @L0149: lda OGora ldx OGora+1 @L014G: sta MaxO stx MaxO+1 ; } jmp @L013B ; ------------------------------------------------------------------------ ; TEXTSTYLE: Set the style used when calling OUTTEXT. Text scaling in X and Y ; direction is passend in X/Y, the text direction is passed in A. ; ; Must set an error code: NO ; TEXTSTYLE: stx TEXTMAGX sty TEXTMAGY sta TEXTDIR rts ; ------------------------------------------------------------------------ ; OUTTEXT: Output text at X/Y = ptr1/ptr2 using the current color and the ; current text style. The text to output is given as a zero terminated ; string with address in ptr3. ; ; Must set an error code: NO ; OUTTEXT: rts ; ------------------------------------------------------------------------ ; Calculate all variables to plot the pixel at X1/Y1. ;------------------------ ;< X1,Y1 - pixel ;> ADDR - address of card ;> X - bit number (X1 & 7) CALC: lda Y1+1 sta ADDR+1 lda Y1 asl rol ADDR+1 asl rol ADDR+1 ; Y*4 clc adc Y1 sta ADDR lda Y1+1 adc ADDR+1 sta ADDR+1 ; Y*4+Y=Y*5 lda ADDR asl rol ADDR+1 asl rol ADDR+1 asl rol ADDR+1 asl rol ADDR+1 sta ADDR ; Y*5*16=Y*80 lda X1+1 sta TEMP lda X1 lsr TEMP ror lsr TEMP ror lsr TEMP ror clc adc ADDR sta ADDR lda ADDR+1 ; ADDR = Y*80+x/8 adc TEMP sta ADDR+1 lda ADDR+1 adc SCRBASE sta ADDR+1 lda X1 and #7 tax rts ;------------- ; copies of some runtime routines abs: ; a/y := abs(a/y) cpy #$00 bpl absend ; negay neg: eor #$ff add #1 pha tya eor #$ff adc #0 tay pla absend: rts icmp: ; compare a/y to zp,x sta TEMP ; TEMP/TEMP2 - arg2 sty TEMP2 lda $0,x pha lda $1,x tay pla tax tya ; x/a - arg1 (a=high) sec sbc TEMP2 bne @L4 cpx TEMP beq @L3 adc #$ff ora #$01 @L3: rts @L4: bvc @L3 eor #$ff ora #$01 rts ;------------- ; VDC helpers VDCSetSourceAddr: pha tya ldx #VDC_DATA_HI jsr VDCWriteReg pla ldx #VDC_DATA_LO bne VDCWriteReg VDCReadByte: ldx #VDC_DATA VDCReadReg: stx VDC_ADDR_REG @L0: bit VDC_ADDR_REG bpl @L0 lda VDC_DATA_REG rts VDCWriteByte: ldx #VDC_DATA VDCWriteReg: stx VDC_ADDR_REG @L0: bit VDC_ADDR_REG bpl @L0 sta VDC_DATA_REG rts
wagiminator/C64-Collection
2,259
C64_xu1541/software/tools/cc65-2.13.2/libsrc/c128/break.s
; ; Ullrich von Bassewitz, 27.09.1998 ; ; void set_brk (unsigned Addr); ; void reset_brk (void); ; .export _set_brk, _reset_brk .destructor _reset_brk .export _brk_a, _brk_x, _brk_y, _brk_sr, _brk_pc .importzp ptr1 .include "c128.inc" .bss _brk_a: .res 1 _brk_x: .res 1 _brk_y: .res 1 _brk_sr: .res 1 _brk_pc: .res 2 .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 brk_old+1 ora brk_old+2 ; Did we save the vector already? bne @L1 ; Jump if we installed the handler already lda BRKVec ; Save the old vector sta brk_old+1 lda BRKVec+1 sta brk_old+2 lda #<brk_stub ; Set the break vector to our stub ldx #>brk_stub sta BRKVec stx BRKVec+1 lda #<brk_handler ; Set the indirect vector to our handler ldx #>brk_handler sta brk_ind+1 stx brk_ind+2 @L1: rts .endproc ; Reset the break vector .proc _reset_brk lda brk_old+1 ldx brk_old+2 beq @L9 ; Jump if vector not installed sta BRKVec stx BRKVec+1 lda #$00 sta brk_old+1 ; Clear the saved vector sta brk_old+2 @L9: 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 ; Break stub, must go into low (non banked) memory .segment "LOWCODE" .proc brk_stub pla ; Get original MMU_CR value sta MMU_CR ; And set it jmp brk_ind ; Jump indirect to break .endproc ; ------------------------------------------------------------------------ ; Data .data ; Old break vector preceeded by a jump opcode brk_old: jmp $0000 ; Indirect vectors preceeded by a jump opcode brk_ind: jmp $0000
wagiminator/C64-Collection
6,797
C64_xu1541/software/tools/cc65-2.13.2/libsrc/c128/c128-ram.s
; ; Extended memory driver for the C128 RAM in bank #1. Driver works without ; problems when statically linked. ; ; Ullrich von Bassewitz, 2002-12-04 ; .include "zeropage.inc" .include "em-kernel.inc" .include "em-error.inc" .include "c128.inc" .macpack generic ; ------------------------------------------------------------------------ ; Header. Includes jump table .segment "JUMPTABLE" ; Driver signature .byte $65, $6d, $64 ; "emd" .byte EMD_API_VERSION ; EM API version number ; Jump table. .word INSTALL .word UNINSTALL .word PAGECOUNT .word MAP .word USE .word COMMIT .word COPYFROM .word COPYTO ; ------------------------------------------------------------------------ ; Constants BASE = $400 TOPMEM = $FF00 PAGES = (TOPMEM - BASE) / 256 ; ------------------------------------------------------------------------ ; Data. .bss curpage: .res 1 ; Current page number window: .res 256 ; Memory "window" .code ; ------------------------------------------------------------------------ ; INSTALL routine. Is called after the driver is loaded into memory. If ; possible, check if the hardware is present and determine the amount of ; memory available. ; Must return an EM_ERR_xx code in a/x. ; INSTALL: ldx #$FF stx curpage stx curpage+1 ; Invalidate the current page inx txa ; A = X = EM_ERR_OK rts ; ------------------------------------------------------------------------ ; UNINSTALL routine. Is called before the driver is removed from memory. ; Can do cleanup or whatever. Must not return anything. ; UNINSTALL: rts ; ------------------------------------------------------------------------ ; PAGECOUNT: Return the total number of available pages in a/x. ; PAGECOUNT: lda #<PAGES ldx #>PAGES rts ; ------------------------------------------------------------------------ ; MAP: Map the page in a/x into memory and return a pointer to the page in ; a/x. The contents of the currently mapped page (if any) may be discarded ; by the driver. ; MAP: sta curpage stx curpage+1 ; Remember the new page clc adc #>BASE sta ptr1+1 ldy #$00 sty ptr1 lda #<ptr1 sta FETVEC ; Transfer one page @L1: ldx #MMU_CFG_RAM1 jsr FETCH sta window,y iny bne @L1 ; Return the memory window lda #<window ldx #>window ; Return the window address rts ; ------------------------------------------------------------------------ ; USE: Tell the driver that the window is now associated with a given page. USE: sta curpage stx curpage+1 ; Remember the page lda #<window ldx #>window ; Return the window rts ; ------------------------------------------------------------------------ ; COMMIT: Commit changes in the memory window to extended storage. COMMIT: lda curpage ; Get the current page ldx curpage+1 bmi done ; Jump if no page mapped clc adc #>BASE sta ptr1+1 ldy #$00 sty ptr1 lda #<ptr1 sta STAVEC ; Transfer one page. Y must be zero on entry @L1: lda window,y ldx #MMU_CFG_RAM1 jsr STASH iny bne @L1 ; Done done: rts ; ------------------------------------------------------------------------ ; COPYFROM: Copy from extended into linear memory. A pointer to a structure ; describing the request is passed in a/x. ; The function must not return anything. ; COPYFROM: sta ptr3 stx ptr3+1 ; Save the passed em_copy pointer ldy #EM_COPY::OFFS lda (ptr3),y sta ptr1 ldy #EM_COPY::PAGE lda (ptr3),y clc adc #>BASE sta ptr1+1 ; From ldy #EM_COPY::BUF lda (ptr3),y sta ptr2 iny lda (ptr3),y sta ptr2+1 ; To lda #<ptr1 sta FETVEC ldy #EM_COPY::COUNT+1 lda (ptr3),y ; Get number of pages beq @L2 ; Skip if no full pages sta tmp1 ; Copy full pages ldy #$00 @L1: ldx #MMU_CFG_RAM1 jsr FETCH sta (ptr2),y iny bne @L1 inc ptr1+1 inc ptr2+1 dec tmp1 bne @L1 ; Copy the remainder of the page @L2: ldy #EM_COPY::COUNT lda (ptr3),y ; Get bytes in last page beq @L4 sta tmp1 ldy #$00 @L3: ldx #MMU_CFG_RAM1 jsr FETCH sta (ptr2),y iny dec tmp1 bne @L3 ; Done @L4: rts ; ------------------------------------------------------------------------ ; COPYTO: Copy from linear into extended memory. A pointer to a structure ; describing the request is passed in a/x. ; The function must not return anything. ; COPYTO: sta ptr3 stx ptr3+1 ; Save the passed em_copy pointer ldy #EM_COPY::OFFS lda (ptr3),y sta ptr1 ldy #EM_COPY::PAGE lda (ptr3),y clc adc #>BASE sta ptr1+1 ; To ldy #EM_COPY::BUF lda (ptr3),y sta ptr2 iny lda (ptr3),y sta ptr2+1 ; From lda #<ptr1 sta STAVEC ldy #EM_COPY::COUNT+1 lda (ptr3),y ; Get number of pages beq @L2 ; Skip if no full pages sta tmp1 ; Copy full pages ldy #$00 @L1: lda (ptr2),y ldx #MMU_CFG_RAM1 jsr STASH iny bne @L1 inc ptr1+1 inc ptr2+1 dec tmp1 bne @L1 ; Copy the remainder of the page @L2: ldy #EM_COPY::COUNT lda (ptr3),y ; Get bytes in last page beq @L4 sta tmp1 ldy #$00 @L3: lda (ptr2),y ldx #MMU_CFG_RAM1 jsr STASH iny dec tmp1 bne @L3 ; Done @L4: rts
wagiminator/C64-Collection
1,548
C64_xu1541/software/tools/cc65-2.13.2/libsrc/c128/cgetc.s
; ; Ullrich von Bassewitz, 06.08.1998 ; ; char cgetc (void); ; .export _cgetc .constructor initcgetc .destructor donecgetc .import cursor .include "c128.inc" ;-------------------------------------------------------------------------- _cgetc: lda KEY_COUNT ; Get number of characters bne L2 ; Jump if there are already chars waiting ; Switch on the cursor if needed. We MUST always switch the cursor on, ; before switching it off, because switching it off will restore the ; character attribute remembered when it was switched on. So just switching ; it off will restore the wrong character attribute. jsr CURS_SET ; Set cursor to current position jsr CURS_ON lda cursor bne L1 lda #$01 jsr CURS_OFF L1: lda KEY_COUNT ; Check characters again beq L1 jsr CURS_OFF ; Switch cursor off, if characters available L2: jsr KBDREAD ; Read char and return in A ldx #0 rts ;-------------------------------------------------------------------------- ; Module constructor/destructor .bss keyvec: .res 2 .segment "INIT" initcgetc: ; Save the old vector lda KeyStoreVec sta keyvec lda KeyStoreVec+1 sta keyvec+1 ; Set the new vector. I can only hope that this works for other C128 ; versions... lda #<$C6B7 ldx #>$C6B7 jmp SetVec .code donecgetc: lda keyvec ldx keyvec+1 SetVec: sei sta KeyStoreVec stx KeyStoreVec+1 cli rts
wagiminator/C64-Collection
11,035
C64_xu1541/software/tools/cc65-2.13.2/libsrc/c128/c128-joymouse.s
; ; Driver for a "joystick mouse". ; ; Ullrich von Bassewitz, 2004-04-05, 2009-09-26 ; .include "zeropage.inc" .include "mouse-kernel.inc" .include "c128.inc" .macpack generic ; ------------------------------------------------------------------------ ; Header. Includes jump table .segment "JUMPTABLE" HEADER: ; Driver signature .byte $6d, $6f, $75 ; "mou" .byte MOUSE_API_VERSION ; Mouse driver API version number ; Jump table. .addr INSTALL .addr UNINSTALL .addr HIDE .addr SHOW .addr SETBOX .addr GETBOX .addr MOVE .addr BUTTONS .addr POS .addr INFO .addr IOCTL .addr IRQ ; Mouse driver flags .byte MOUSE_FLAG_LATE_IRQ ; Callback table, set by the kernel before INSTALL is called CHIDE: jmp $0000 ; Hide the cursor CSHOW: jmp $0000 ; Show the cursor CMOVEX: jmp $0000 ; Move the cursor to X coord CMOVEY: jmp $0000 ; Move the cursor to Y coord ;---------------------------------------------------------------------------- ; Constants SCREEN_HEIGHT = 200 SCREEN_WIDTH = 320 .enum JOY UP = $01 DOWN = $02 LEFT = $04 RIGHT = $08 FIRE = $10 .endenum ;---------------------------------------------------------------------------- ; Global variables. The bounding box values are sorted so that they can be ; written with the least effort in the SETBOX and GETBOX routines, so don't ; reorder them. .bss Vars: YPos: .res 2 ; Current mouse position, Y XPos: .res 2 ; Current mouse position, X 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 Buttons: .res 1 ; Button mask ; Temporary value used in the int handler Temp: .res 1 ; Default values for above variables .rodata .proc DefVars .word SCREEN_HEIGHT/2 ; YPos .word SCREEN_WIDTH/2 ; XPos .word 0 ; XMin .word 0 ; YMin .word SCREEN_WIDTH ; XMax .word SCREEN_HEIGHT ; YMax .byte 0 ; Buttons .endproc .code ;---------------------------------------------------------------------------- ; INSTALL routine. Is called after the driver is loaded into memory. If ; possible, check if the hardware is present. ; Must return an MOUSE_ERR_xx code in a/x. INSTALL: ; Initialize variables. Just copy the default stuff over ldx #.sizeof(DefVars)-1 @L1: lda DefVars,x sta Vars,x dex bpl @L1 ; Be sure the mouse cursor is invisible and at the default location. We ; need to do that here, because our mouse interrupt handler doesn't set the ; mouse position if it hasn't changed. sei jsr CHIDE lda XPos ldx XPos+1 jsr CMOVEX lda YPos ldx YPos+1 jsr CMOVEY cli ; Done, return zero (= MOUSE_ERR_OK) ldx #$00 txa rts ;---------------------------------------------------------------------------- ; UNINSTALL routine. Is called before the driver is removed from memory. ; No return code required (the driver is removed from memory on return). UNINSTALL = HIDE ; Hide cursor on exit ;---------------------------------------------------------------------------- ; HIDE routine. Is called to hide the mouse pointer. The mouse kernel manages ; a counter for calls to show/hide, and the driver entry point is only called ; if the mouse is currently visible and should get hidden. For most drivers, ; no special action is required besides hiding the mouse cursor. ; No return code required. HIDE: sei jsr CHIDE cli rts ;---------------------------------------------------------------------------- ; SHOW routine. Is called to show the mouse pointer. The mouse kernel manages ; a counter for calls to show/hide, and the driver entry point is only called ; if the mouse is currently hidden and should become visible. For most drivers, ; no special action is required besides enabling the mouse cursor. ; No return code required. SHOW: sei jsr CSHOW cli rts ;---------------------------------------------------------------------------- ; SETBOX: Set the mouse bounding box. The parameters are passed as they come ; from the C program, that is, a pointer to a mouse_box struct in a/x. ; No checks are done if the mouse is currently inside the box, this is the job ; of the caller. It is not necessary to validate the parameters, trust the ; caller and save some code here. No return code required. SETBOX: sta ptr1 stx ptr1+1 ; Save data pointer ldy #.sizeof (MOUSE_BOX)-1 sei @L1: lda (ptr1),y sta XMin,y dey bpl @L1 cli rts ;---------------------------------------------------------------------------- ; GETBOX: Return the mouse bounding box. The parameters are passed as they ; come from the C program, that is, a pointer to a mouse_box struct in a/x. GETBOX: sta ptr1 stx ptr1+1 ; Save data pointer ldy #.sizeof (MOUSE_BOX)-1 sei @L1: lda XMin,y sta (ptr1),y dey bpl @L1 cli rts ;---------------------------------------------------------------------------- ; MOVE: Move the mouse to a new position. The position is passed as it comes ; from the C program, that is: X on the stack and Y in a/x. The C wrapper will ; remove the parameter from the stack on return. ; No checks are done if the new position is valid (within the bounding box or ; the screen). No return code required. ; MOVE: sei ; No interrupts sta YPos stx YPos+1 ; New Y position jsr CMOVEY ; Set it ldy #$01 lda (sp),y sta XPos+1 tax dey lda (sp),y sta XPos ; New X position jsr CMOVEX ; Move the cursor cli ; Allow interrupts rts ;---------------------------------------------------------------------------- ; BUTTONS: Return the button mask in a/x. BUTTONS: lda Buttons ldx #$00 rts ;---------------------------------------------------------------------------- ; POS: Return the mouse position in the MOUSE_POS struct pointed to by ptr1. ; No return code required. POS: ldy #MOUSE_POS::XCOORD ; Structure offset sei ; Disable interrupts lda XPos ; Transfer the position sta (ptr1),y lda XPos+1 iny sta (ptr1),y lda YPos iny sta (ptr1),y lda YPos+1 cli ; Enable interrupts iny sta (ptr1),y ; Store last byte rts ; Done ;---------------------------------------------------------------------------- ; INFO: Returns mouse position and current button mask in the MOUSE_INFO ; struct pointed to by ptr1. No return code required. ; ; We're cheating here to keep the code smaller: The first fields of the ; mouse_info struct are identical to the mouse_pos struct, so we will just ; call _mouse_pos to initialize the struct pointer and fill the position ; fields. INFO: jsr POS ; Fill in the button state lda Buttons ldy #MOUSE_INFO::BUTTONS sta (ptr1),y rts ;---------------------------------------------------------------------------- ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl ; specific data in ptr1, and the ioctl code in A. ; Must return an error code in a/x. ; IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioclts for now ldx #>MOUSE_ERR_INV_IOCTL rts ;---------------------------------------------------------------------------- ; IRQ: Irq handler entry point. Called as a subroutine but in IRQ context ; (so be careful). The routine MUST return carry set if the interrupt has been ; 'handled' - which means that the interrupt source is gone. Otherwise it ; MUST return carry clear. ; IRQ: lda #$7F sta CIA1_PRA lda CIA1_PRB ; Read joystick #0 and #$1F eor #$1F ; Make all bits active high sta Temp ; Check for a pressed button and place the result into Buttons ldx #$00 ; Assume no button pressed and #JOY::FIRE ; Check fire button beq @L0 ; Jump if not pressed ldx #MOUSE_BTN_LEFT ; Left (only) button is pressed @L0: stx Buttons ; Check left/right lda Temp ; Read joystick #0 and #(JOY::LEFT | JOY::RIGHT) beq @SkipX ; ; We will cheat here and rely on the fact that either the left, OR the right ; bit can be active and #JOY::RIGHT ; Check RIGHT bit bne @Right lda #$FF tax bne @AddX ; Branch always @Right: lda #$01 ldx #$00 ; Calculate the new X coordinate (--> a/y) @AddX: add XPos tay ; Remember low byte txa adc XPos+1 tax ; Limit the X coordinate to the bounding box cpy XMin sbc XMin+1 bpl @L1 ldy XMin ldx XMin+1 jmp @L2 @L1: txa cpy XMax sbc XMax+1 bmi @L2 ldy XMax ldx XMax+1 @L2: sty XPos stx XPos+1 ; Move the mouse pointer to the new X pos tya jsr CMOVEX ; Calculate the Y movement vector @SkipX: lda Temp ; Read joystick #0 and #(JOY::UP | JOY::DOWN) ; Check up/down beq @SkipY ; ; We will cheat here and rely on the fact that either the up, OR the down ; bit can be active lsr a ; Check UP bit bcc @Down lda #$FF tax bne @AddY @Down: lda #$01 ldx #$00 ; Calculate the new Y coordinate (--> a/y) @AddY: add YPos tay ; Remember low byte txa adc YPos+1 tax ; Limit the Y coordinate to the bounding box cpy YMin sbc YMin+1 bpl @L3 ldy YMin ldx YMin+1 jmp @L4 @L3: txa cpy YMax sbc YMax+1 bmi @L4 ldy YMax ldx YMax+1 @L4: sty YPos stx YPos+1 ; Move the mouse pointer to the new X pos tya jsr CMOVEY ; Done @SkipY: clc ; Interrupt not "handled" rts
wagiminator/C64-Collection
1,580
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/strncpy.s
; ; Ullrich von Bassewitz, 2003-05-04 ; ; char* __fastcall__ strncpy (char* dest, const char* src, unsigned size); ; .export _strncpy .import popax .importzp ptr1, ptr2, tmp1, tmp2, tmp3 .proc _strncpy eor #$FF sta tmp1 txa eor #$FF sta tmp2 ; Store -size - 1 jsr popax ; get src sta ptr1 stx ptr1+1 jsr popax ; get dest sta ptr2 stx ptr2+1 stx tmp3 ; remember for function return ; Copy src -> dest up to size bytes ldx tmp1 ; Load low byte of ones complement of size ldy #$00 L1: inx bne L2 inc tmp2 beq L9 L2: lda (ptr1),y ; Copy one character sta (ptr2),y beq L5 ; Bail out if terminator reached (A = 0) iny bne L1 inc ptr1+1 inc ptr2+1 ; Bump high bytes bne L1 ; Branch always ; Fill the remaining bytes. L3: inx ; Counter low byte beq L6 ; Branch on overflow L4: sta (ptr2),y ; Clear one byte L5: iny ; Bump pointer bne L3 inc ptr2+1 ; Bump high byte bne L3 ; Branch always ; Bump the counter high byte L6: inc tmp2 bne L4 ; Done, return dest L9: lda ptr2 ; Get low byte ldx tmp3 ; Get unchanged high byte rts .endproc
wagiminator/C64-Collection
1,416
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/atexit.s
; ; Ullrich von Bassewitz, 06.06.1998 ; ; int atexit (void (*f) (void)); ; .export _atexit .destructor doatexit, 5 .import callax .include "errno.inc" .macpack cpu ; --------------------------------------------------------------------------- .proc _atexit ldy exitfunc_index cpy #exitfunc_max ; Slot available? beq @Error ; Jump if no ; Enter the function into the table sta exitfunc_table,y iny txa sta exitfunc_table,y iny sty exitfunc_index ; Done, return zero lda #0 tax rts ; Error, no space left @Error: lda #ENOSPC ; No space left jsr __seterrno ldx #$FF ; Return -1 txa rts .endproc ; --------------------------------------------------------------------------- .code .proc doatexit ldy exitfunc_index ; Get index beq @L9 ; Jump if done dey lda exitfunc_table,y tax dey lda exitfunc_table,y sty exitfunc_index jsr callax ; Call the function .if (.cpu .bitand ::CPU_ISET_65SC02) bra doatexit .else jmp doatexit ; Next one .endif @L9: rts .endproc ; --------------------------------------------------------------------------- .bss exitfunc_index: .res 1 ; Index into table, inc'ed by 2 exitfunc_table: .res 10 ; 5 exit functions exitfunc_max = <(* - exitfunc_table)
wagiminator/C64-Collection
1,215
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/_sys.s
; ; void __fastcall__ _sys (struct regs* r); ; ; Ullrich von Bassewitz, 16.12.1998 ; .export __sys .import jmpvec .importzp ptr1 __sys: sta ptr1 stx ptr1+1 ; Save the pointer to r ; Fetch the PC and store it into the jump vector ldy #5 lda (ptr1),y sta jmpvec+2 dey lda (ptr1),y sta jmpvec+1 ; Remember the flags so we can restore them to a known state after calling the ; routine php ; Get the flags, keep the state of bit 4 and 5 using the other flags from ; the flags value passed by the caller. Push the new flags and push A. dey php pla ; Current flags -> A eor (ptr1),y and #%00110000 eor (ptr1),y pha ; Push new flags value ldy #0 lda (ptr1),y pha ; Get and assign X and Y iny lda (ptr1),y tax iny lda (ptr1),y tay ; Set a and the flags, call the machine code routine pla plp jsr jmpvec ; Back from the routine. Save the flags and A. php pha ; Put the register values into the regs structure tya ldy #2 sta (ptr1),y dey txa sta (ptr1),y dey pla sta (ptr1),y ldy #3 pla sta (ptr1),y ; Restore the old flags value plp ; Done rts
wagiminator/C64-Collection
2,381
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/strnicmp.s
; ; Christian Groessler, 10.02.2009 ; derived from strncmp.s and stricmp.s ; ; int __fastcall__ strnicmp (const char* s1, const char* s2, size_t count); ; int __fastcall__ strncasecmp (const char* s1, const char* s2, size_t count); ; .export _strnicmp, _strncasecmp .import popax, __ctype .importzp ptr1, ptr2, ptr3, tmp1 .include "ctype.inc" _strnicmp: _strncasecmp: ; Convert the given counter value in a/x from a downward counter into an ; upward counter, so we can increment the counter in the loop below instead ; of decrementing it. This adds some overhead now, but is cheaper than ; executing a more complex test in each iteration of the loop. We do also ; correct the value by one, so we can do the test on top of the loop. eor #$FF sta ptr3 txa eor #$FF sta ptr3+1 ; Get the remaining arguments jsr popax ; get s2 sta ptr2 stx ptr2+1 jsr popax ; get s1 sta ptr1 stx ptr1+1 ; Loop setup ldy #0 ; Start of compare loop. Check the counter. Loop: inc ptr3 beq IncHi ; Increment high byte ; Compare a byte from the strings Comp: lda (ptr2),y tax lda __ctype,x ; get character classification and #CT_LOWER ; lower case char? beq L1 ; jump if no txa ; get character back sec sbc #<('a'-'A') ; make upper case char tax ; L1: stx tmp1 ; remember upper case equivalent lda (ptr1),y ; get character from first string tax lda __ctype,x ; get character classification and #CT_LOWER ; lower case char? beq L2 ; jump if no txa ; get character back sec sbc #<('a'-'A') ; make upper case char tax L2: cpx tmp1 ; compare characters bne NotEqual ; Jump if strings different txa ; End of strings? beq Equal1 ; Jump if EOS reached, a/x == 0 ; Increment the pointers iny bne Loop inc ptr1+1 inc ptr2+1 bne Loop ; Branch always ; Increment hi byte IncHi: inc ptr3+1 bne Comp ; Jump if counter not zero ; Exit code if strings are equal. a/x not set Equal: lda #$00 tax Equal1: rts ; Exit code if strings not equal NotEqual: bcs L3 ldx #$FF ; Make result negative rts L3: ldx #$01 ; Make result positive rts
wagiminator/C64-Collection
16,448
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/modload.s
;*****************************************************************************/ ;* */ ;* modload.s */ ;* */ ;* o65 module loader for the cc65 library */ ;* */ ;* */ ;* */ ;* (C) 2002 Ullrich von Bassewitz */ ;* Wacholderweg 14 */ ;* D-70597 Stuttgart */ ;* EMail: uz@musoftware.de */ ;* */ ;* */ ;* This software is provided 'as-is', without any expressed or implied */ ;* warranty. In no event will the authors be held liable for any damages */ ;* arising from the use of this software. */ ;* */ ;* Permission is granted to anyone to use this software for any purpose, */ ;* including commercial applications, and to alter it and redistribute it */ ;* freely, subject to the following restrictions: */ ;* */ ;* 1. The origin of this software must not be misrepresented; you must not */ ;* claim that you wrote the original software. If you use this software */ ;* in a product, an acknowledgment in the product documentation would be */ ;* appreciated but is not required. */ ;* 2. Altered source versions must be plainly marked as such, and must not */ ;* be misrepresented as being the original software. */ ;* 3. This notice may not be removed or altered from any source */ ;* distribution. */ ;* */ ;*****************************************************************************/ .include "o65.inc" .include "modload.inc" .import pushax, pusha0, push0, push1, decax1 .import _malloc, _free, _bzero .import __ZP_START__ ; Linker generated .importzp sp, ptr1, tmp1, regbank .macpack generic ;------------------------------------------------------------------------------ ; Variables stored in the register bank in the zero page. Placing the variables ; here will protect them when calling other C functions. Module = regbank+0 ; Pointer to module memory Ctrl = regbank+2 ; Pointer to mod_ctrl structure TPtr = regbank+4 ; Pointer to module data for relocation ;------------------------------------------------------------------------------ ; Static module data .bss ; Save areas and error recovery data Stack: .byte 0 ; Old stackpointer RegBankSave: .res 6 ; Save area for register bank ; The header of the o65 file. Since we don't need the first 8 bytes any ; longer, once we've checked them, we will overlay them with other data to ; save a few bytes. Header: .tag O65_HDR ; The o65 header ; Input InputByte = Header ; Byte read from input ; Relocation RelocVal = Header + 1 ; Relocation value .data Read: jmp $FFFF ; Jump to read routine .rodata ExpectedHdr: .byte O65_MARKER_0, O65_MARKER_1 ; non C64 marker .byte O65_MAGIC_0, O65_MAGIC_1, O65_MAGIC_2 ; Magic ("o65") .byte O65_VERSION ; Version .word O65_MODE_CC65 ; Mode word ExpectedHdrSize = * - ExpectedHdr ;------------------------------------------------------------------------------ ; PushCallerData: Push the callerdata member from control structure onto the ; C stack. .code PushCallerData: ldy #MOD_CTRL::CALLERDATA+1 lda (Ctrl),y tax dey lda (Ctrl),y jmp pushax ;------------------------------------------------------------------------------ ; RestoreRegBank: Restore the register bank contents from the save area. Will ; destroy A and X (the latter will be zero on return). .code RestoreRegBank: ldx #6 @L1: lda RegBankSave-1,x sta regbank-1,x dex bne @L1 rts ;------------------------------------------------------------------------------ ; GetReloc: Return a relocation value based on the segment in A. ; The routine uses some knowledge about the values to make the code shorter. .code GetReloc: cmp #O65_SEGID_TEXT bcc FormatError cmp #O65_SEGID_ZP beq @L1 bcs FormatError ; Text, data and bss segment lda Module ldx Module+1 ; Return start address of buffer rts ; Zero page relocation @L1: lda #<__ZP_START__ ldx #>__ZP_START__ rts ;------------------------------------------------------------------------------ ; ReadByte: Read one byte with error checking into InputByte and A. ; ReadAndCheckError: Call read with the current C stack and check for errors. .bss ReadSize: .res 2 .code ReadByte: ; C->read (C->callerdata, &B, 1) jsr PushCallerData lda #<InputByte ldx #>InputByte jsr pushax ldx #0 lda #1 ; This is a second entry point used by the other calls to Read ReadAndCheckError: sta ReadSize stx ReadSize+1 jsr Read ; Check the return code and bail out in case of problems cmp ReadSize bne @L1 cpx ReadSize+1 beq @L2 ; Jump if ok @L1: lda #MLOAD_ERR_READ bne CleanupAndExit ; Done @L2: lda InputByte ; If called ReadByte, load the byte read Done: rts ;------------------------------------------------------------------------------ ; FormatError: Bail out with an o65 format error .code FormatError: lda #MLOAD_ERR_FMT ; bne CleanupAndExit ; Branch always ;------------------------------------------------------------------------------ ; CleanupAndExit: Free any allocated resources, restore the stack and return ; to the caller. .code CleanupAndExit: ; Restore the stack so we may return to the caller from here ldx Stack txs ; Save the error return code pha ; Check if we have to free the allocated block lda Module ldx Module+1 bne @L1 tay ; Test high byte beq @L2 @L1: jsr _free ; Free the allocated block ; Restore the register bank @L2: jsr RestoreRegBank ; Restore the error code and return to the caller ldx #$00 ; Load the high byte pla rts ;------------------------------------------------------------------------------ ; RelocSeg: Relocate the segment pointed to by a/x .code RelocSeg: jsr decax1 ; Start value is segment-1 sta TPtr stx TPtr+1 Loop: jsr ReadByte ; Read byte from relocation table beq Done ; Bail out if end of table reached cmp #255 ; Special offset? bne @L1 ; Increment offset by 254 and continue lda TPtr add #254 sta TPtr bcc Loop inc TPtr+1 jmp Loop ; Increment offset by A @L1: add TPtr sta TPtr bcc @L2 inc TPtr+1 ; Read the relocation byte, extract the segment id, fetch the corresponding ; relocation value and place it into ptr1 @L2: jsr ReadByte and #O65_SEGID_MASK jsr GetReloc sta RelocVal stx RelocVal+1 ; Get the relocation byte again, this time extract the relocation type. lda InputByte and #O65_RTYPE_MASK ; Check for and handle the different relocation types. cmp #O65_RTYPE_WORD beq RelocWord cmp #O65_RTYPE_HIGH beq RelocHigh cmp #O65_RTYPE_LOW bne FormatError ; Relocate the low byte RelocLow: ldy #0 clc lda RelocVal bcc AddCommon ; Relocate a high byte RelocHigh: jsr ReadByte ; Read low byte from relocation table ldy #0 clc adc RelocVal ; We just need the carry AddHigh: lda RelocVal+1 AddCommon: adc (TPtr),y sta (TPtr),y jmp Loop ; Done, next entry ; Relocate a word RelocWord: ldy #0 clc lda RelocVal adc (TPtr),y sta (TPtr),y iny bne AddHigh ; Branch always (add high byte) ;------------------------------------------------------------------------------ ; mod_load: Load and relocate an o65 module .code _mod_load: ; Save the register bank and clear the Module pointer pha ldy #6 @L1: lda regbank-1,y sta RegBankSave-1,y dey bne @L1 sty Module sty Module+1 pla ; Save the passed parameter sta Ctrl stx Ctrl+1 ; Save the stack pointer so we can bail out even from subroutines tsx stx Stack ; Get the read function pointer from the control structure and place it into ; our call vector ldy #MOD_CTRL::READ lda (Ctrl),y sta Read+1 iny lda (Ctrl),y sta Read+2 ; Read the o65 header: C->read (C->callerdata, &H, sizeof (H)) jsr PushCallerData lda #<Header ldx #>Header jsr pushax lda #.sizeof(O65_HDR) ldx #0 ; Always less than 256 jsr ReadAndCheckError ; Bails out in case of errors ; We read the o65 header successfully. Validate it. ldy #ExpectedHdrSize-1 ValidateHeader: lda Header,y cmp ExpectedHdr,y bne HeaderError dey bpl ValidateHeader ; Header is ok as far as we can say now. Read all options, check for the ; OS option and ignore all others. The OS option contains a version number ; and the module id as additional data. iny ; Y = $00 sty TPtr+1 ; Flag for OS option read Opt: jsr ReadByte ; Read the length byte beq OptDone ; Jump if done sta TPtr ; Use TPtr as a counter ; An option has a length of at least 2 bytes cmp #2 bcc HeaderError ; Must be 2 bytes total at least ; Check for the OS option dec TPtr jsr ReadByte ; Get the option type cmp #O65_OPT_OS ; OS option? bne SkipOpt ; No: Skip lda TPtr ; Get remaining length+1 cmp #5 ; CC65 has 6 bytes total bne OSError jsr ReadByte ; Get the operating system cmp #O65_OS_CC65 bne OSError ; Wrong operating system jsr ReadByte ; Get the version number, expect zero bne OSError ; Wrong version jsr ReadByte ; Get low byte of id ldy #MOD_CTRL::MODULE_ID sta (Ctrl),y jsr ReadByte ldy #MOD_CTRL::MODULE_ID+1 sta (Ctrl),y inc TPtr+1 ; Remember that we got the OS jmp Opt ; Skip one option SkipOpt: dec TPtr beq Opt ; Next option jsr ReadByte ; Skip one byte jmp SkipOpt ; Operating system error OSError: lda #MLOAD_ERR_OS jmp CleanupAndExit ; Options done, check that we got the OS option OptDone: lda TPtr+1 bne CalcSizes ; Entry point for header errors HeaderError: lda #MLOAD_ERR_HDR jmp CleanupAndExit ; Skipped all options. Calculate the size of text+data and of text+data+bss ; (the latter is the size of the memory block we need). We will store the ; total module size also into the control structure for evaluation by the ; caller CalcSizes: lda Header + O65_HDR::TLEN add Header + O65_HDR::DLEN sta TPtr lda Header + O65_HDR::TLEN + 1 adc Header + O65_HDR::DLEN + 1 sta TPtr+1 lda TPtr add Header + O65_HDR::BLEN pha ; Save low byte of total size ldy #MOD_CTRL::MODULE_SIZE sta (Ctrl),y lda TPtr+1 adc Header + O65_HDR::BLEN + 1 iny sta (Ctrl),y tax pla ; Restore low byte of total size ; Total memory size is now in a/x. Allocate memory and remember the result, ; both, locally and in the control structure so it the caller can access ; the memory block. After that, check if we got the requested memory. jsr _malloc sta Module stx Module+1 ldy #MOD_CTRL::MODULE sta (Ctrl),y txa iny sta (Ctrl),y ora Module bne GotMem ; Could not allocate memory lda #MLOAD_ERR_MEM jmp CleanupAndExit ; Control structure is complete now. Clear the bss segment. ; bzero (bss_addr, bss_size) GotMem: lda Module add TPtr pha lda Module+1 adc TPtr+1 ; Module + tlen + dlen tax pla jsr pushax lda Header + O65_HDR::BLEN ldx Header + O65_HDR::BLEN+1 jsr _bzero ; bzero (bss, bss_size); ; Load code and data segment into memory. The sum of the sizes of ; code+data segment is still in TPtr. ; C->read (C->callerdata, C->module, H.tlen + H.dlen) jsr PushCallerData lda Module ldx Module+1 jsr pushax lda TPtr ldx TPtr+1 jsr ReadAndCheckError ; Bails out in case of errors ; We've got the code and data segments in memory. Next section contains ; undefined references which we don't support. So check if the count of ; undefined references is actually zero. jsr ReadByte bne Undef jsr ReadByte beq Reloc Undef: jmp FormatError ; Number of undefined references was zero. Next come the relocation tables ; for code and data segment. Relocate the code segment Reloc: lda Module ldx Module + 1 ; Code segment address jsr RelocSeg ; Relocate the data segment lda Module add Header + O65_HDR::TLEN pha lda Module + 1 adc Header + O65_HDR::TLEN + 1 tax pla ; Data segment address in a/x jsr RelocSeg ; We're done. Restore the register bank and return a success code jsr RestoreRegBank ; X will be zero on return lda #MLOAD_OK rts
wagiminator/C64-Collection
1,495
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/getcwd.s
; ; Ullrich von Bassewitz, 2003-08-12 ; ; char* __fastcall__ getcwd (char* buf, size_t size); ; .export _getcwd .import popax .import __cwd .importzp ptr1, ptr2 .include "errno.inc" ;-------------------------------------------------------------------------- .proc _getcwd ; Remember -size-1 because this simplifies the following loop eor #$FF sta ptr2 txa eor #$FF sta ptr2+1 jsr popax ; Get buf sta ptr1 stx ptr1+1 ; Save buf ; Copy __cwd to the given buffer checking the length ldy #$00 loop: inc ptr2 bne @L1 inc ptr2+1 beq overflow ; Copy one character, end the loop if the zero terminator is reached. We ; don't support directories longer than 255 characters for now. @L1: lda __cwd,y sta (ptr1),y beq done iny bne loop ; For some reason the cwd is longer than 255 characters. This should not ; happen, we handle it as if the passed buffer was too short. ; ; String overflow, return ERANGE overflow: lda #<ERANGE sta __errno lda #>ERANGE sta __errno+1 tax ; High byte of ERANGE is zero, return zero rts ; Success, return buf done: lda ptr1 ldx ptr1+1 rts .endproc
wagiminator/C64-Collection
1,472
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/fprintf.s
; ; int fprintf (FILE* f, const char* Format, ...); ; ; Ullrich von Bassewitz, 1.12.2000 ; .export _fprintf .import addysp, decsp4, _vfprintf .importzp sp, ptr1 .macpack generic ; ---------------------------------------------------------------------------- ; Data .bss ParamSize: .res 1 ; Number of parameter bytes ; ---------------------------------------------------------------------------- ; Code .code _fprintf: sty ParamSize ; Number of param bytes passed in Y ; We have to push f and format, both in the order they already have on stack. ; To make this somewhat more efficient, we will create space on the stack and ; then do a copy of the complete block instead of pushing each parameter ; separately. Since the size of the arguments passed is the same as the size ; of the fixed arguments, this will allow us to calculate the pointer to the ; fixed size arguments easier (they're just ParamSize bytes away). jsr decsp4 ; Calculate a pointer to the Format argument lda ParamSize add sp sta ptr1 ldx sp+1 bcc @L1 inx @L1: stx ptr1+1 ; Now copy both, f and format ldy #4-1 @L2: lda (ptr1),y sta (sp),y dey bpl @L2 ; Load va_list (last and __fastcall__ parameter to vfprintf) lda ptr1 ldx ptr1+1 ; Call vfprintf jsr _vfprintf ; Cleanup the stack. We will return what we got from vfprintf ldy ParamSize jmp addysp
wagiminator/C64-Collection
1,559
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/stroserr.s
; ; Ullrich von Bassewitz, 17.07.2002 ; ; const char* __fastcall__ _stroserror (unsigned char errcode); ; /* Map an operating system error number to an error message. */ ; .export __stroserror .import __sys_oserrlist .importzp ptr1, tmp1 .macpack generic ; The table is built as a list of entries ; ; .byte entrylen ; .byte errorcode ; .asciiz errormsg ; ; and terminated by an entry with length zero that is returned if the ; error code could not be found. __stroserror: sta tmp1 ; Save the error code ldy #<__sys_oserrlist sty ptr1 ldy #>__sys_oserrlist sty ptr1+1 ; Setup pointer to message table @L1: ldy #0 lda (ptr1),y ; Get the length beq Done ; Bail out if end of list reached iny lda (ptr1),y ; Compare the error code cmp tmp1 beq Done ; Jump if found ; Not found, move pointer to next entry dey clc lda ptr1 adc (ptr1),y sta ptr1 bcc @L1 inc ptr1+1 bcs @L1 ; Branch always ; We've found the code or reached the end of the list Done: ldx ptr1+1 lda ptr1 add #2 ; Add a total of #2 bcc @L1 inx ; Bump high byte @L1: rts
wagiminator/C64-Collection
1,476
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/snprintf.s
; ; int snprintf (char* buf, size_t size, const char* Format, ...); ; ; Ullrich von Bassewitz, 2009-09-26 ; .export _snprintf .import pushax, addysp, decsp6, _vsnprintf .importzp sp, ptr1 .macpack generic ; ---------------------------------------------------------------------------- ; Data .bss ParamSize: .res 1 ; Number of parameter bytes ; ---------------------------------------------------------------------------- ; Code .code _snprintf: sty ParamSize ; Number of param bytes passed in Y ; We have to push buf/size/format, both in the order they already have on stack. ; To make this somewhat more efficient, we will create space on the stack and ; then do a copy of the complete block instead of pushing each parameter ; separately. Since the size of the arguments passed is the same as the size ; of the fixed arguments, this will allow us to calculate the pointer to the ; fixed size arguments easier (they're just ParamSize bytes away). jsr decsp6 ; Calculate a pointer to the Format argument lda ParamSize add sp sta ptr1 ldx sp+1 bcc @L1 inx @L1: stx ptr1+1 ; Now copy buf/size/format ldy #6-1 @L2: lda (ptr1),y sta (sp),y dey bpl @L2 ; Load va_list (last and __fastcall__ parameter to vsprintf) lda ptr1 ldx ptr1+1 ; Call vsnprintf jsr _vsnprintf ; Cleanup the stack. We will return what we got from vsprintf ldy ParamSize jmp addysp
wagiminator/C64-Collection
1,474
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/_heapmaxavail.s
; ; Ullrich von Bassewitz, 2003-02-01 ; ; Return the size of the largest free block on the heap. ; ; size_t __fastcall__ _heapmaxavail (void); ; ; .importzp ptr1, ptr2 .export __heapmaxavail .include "_heap.inc" .macpack generic ;----------------------------------------------------------------------------- ; Code __heapmaxavail: ; size_t Size = (_heapend - _heapptr) * sizeof (*_heapend); lda __heapend sub __heapptr sta ptr2 lda __heapend+1 sbc __heapptr+1 sta ptr2+1 ; struct freeblock* F = _heapfirst; lda __heapfirst sta ptr1 lda __heapfirst+1 @L1: sta ptr1+1 ; while (F) { ora ptr1 beq @L3 ; Jump if end of free list reached ; if (Size < F->size) { ldy #freeblock::size lda ptr2 sub (ptr1),y iny lda ptr2+1 sbc (ptr1),y bcs @L2 ; Size = F->size; ldy #freeblock::size lda (ptr1),y sta ptr2 iny lda (ptr1),y sta ptr2+1 ; F = F->next; @L2: iny ; Points to F->next lda (ptr1),y tax iny lda (ptr1),y stx ptr1 jmp @L1 ; return Size; @L3: lda ptr2 ldx ptr2+1 rts
wagiminator/C64-Collection
2,070
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/atoi.s
; ; Ullrich von Bassewitz, 05.06.1998 ; ; int atoi (const char* s); ; long atol (const char* s); ; .export _atoi, _atol .import negeax, __ctype .importzp sreg, ptr1, ptr2, tmp1 .include "ctype.inc" ; ; Conversion routine (32 bit) ; _atoi: _atol: sta ptr1 ; Store s stx ptr1+1 ldy #0 sty ptr2 sty ptr2+1 ; initial value (32 bit) sty sreg sty sreg+1 ; Skip whitespace L1: lda (ptr1),y tax lda __ctype,x ; get character classification and #CT_SPACE_TAB ; tab or space? beq L2 ; jump if no iny bne L1 inc ptr1+1 bne L1 ; branch always ; Check for a sign. The character is in X L2: txa ; get char ldx #0 ; flag: positive cmp #'+' ; ### portable? beq L3 cmp #'-' ; ### portable? bne L5 dex ; flag: negative L3: iny bne L5 inc ptr1+1 ; Store the sign flag and setup for conversion L5: stx tmp1 ; remember sign flag L6: lda (ptr1),y ; get next char tax lda __ctype,x ; get character classification and #$04 ; digit? beq L8 ; done ; Multiply ptr2 (the converted value) by 10 jsr mul2 ; * 2 lda sreg+1 pha lda sreg pha lda ptr2+1 pha lda ptr2 pha ; Save value jsr mul2 ; * 4 jsr mul2 ; * 8 clc pla adc ptr2 sta ptr2 pla adc ptr2+1 sta ptr2+1 pla adc sreg sta sreg pla adc sreg+1 sta sreg+1 ; x*2 + x*8 = x*10 ; Get the character back and add it txa ; get char back sec sbc #'0' ; make numeric value clc adc ptr2 sta ptr2 bcc L7 inc ptr2+1 bne L7 inc sreg bne L7 inc sreg+1 ; Next character L7: iny bne L6 inc ptr1+1 bne L6 ; Conversion done. Load the low 16 bit into A/X L8: lda ptr2 ldx ptr2+1 ; Negate the value if necessary, otherwise we're done ldy tmp1 ; sign beq L9 ; Branch if positive ; Negate the 32 bit value in ptr2/sreg jmp negeax ; ; Helper functions ; mul2: asl ptr2 rol ptr2+1 rol sreg rol sreg+1 ; * 2 L9: rts
wagiminator/C64-Collection
1,665
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/scanf.s
; ; int scanf(const char* Format, ...); ; ; 2000-12-01, Ullrich von Bassewitz ; 2004-12-31, Greg King ; .export _scanf .import _stdin, pushax, addysp, _vfscanf .import sp:zp, ptr1:zp .macpack generic ; ---------------------------------------------------------------------------- ; Code ; _scanf: sty ArgSize ; Number of argument bytes passed in .Y ; We are using a (hopefully) clever trick here to reduce code size. On entry, ; the stack pointer points to the last pushed argument of the variable ; argument list. Adding the number of argument bytes, would result in a ; pointer that points _above_ the Format argument. ; Because we have to push stdin anyway, we will do that here, so: ; ; * we will save the subtraction of 2 (__fixargs__) later; ; * we will have the address of the Format argument which needs to ; be pushed next. lda _stdin ldx _stdin+1 jsr pushax ; Now, calculate the va_list pointer, which does point to Format. lda sp ldx sp+1 add ArgSize bcc @L1 inx @L1: sta ptr1 stx ptr1+1 ; Push a copy of Format. ldy #1 lda (ptr1),y tax dey lda (ptr1),y jsr pushax ; Load va_list [last and __fastcall__ argument to vfscanf()]. lda ptr1 ldx ptr1+1 ; Call vfscanf(). jsr _vfscanf ; Clean up the stack. We will return what we got from vfscanf(). ldy ArgSize jmp addysp ; ---------------------------------------------------------------------------- ; Data ; .bss ArgSize: .res 1 ; Number of argument bytes
wagiminator/C64-Collection
1,294
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/rand.s
; ; Randum number generator ; ; Written and donated by Sidney Cadot - sidney@ch.twi.tudelft.nl ; ; May be distributed with the cc65 runtime using the same license. ; ; ; int rand (void); ; void srand (unsigned seed); ; ; Uses 4-byte state. ; Multiplier must be 1 (mod 4) ; Added value must be 1 (mod 2) ; This guarantees max. period (2**32) ; Bits 8-22 are returned (positive 2-byte int) ; where 0 is LSB, 31 is MSB. ; This is better as lower bits exhibit easily ; detectable patterns. ; .export _rand, _srand .data ; The seed. When srand() is not called, the C standard says that that rand() ; should behave as if srand() was called with an argument of 1 before. rand: .dword 1 .code _rand: clc lda rand+0 ; SEED *= $01010101 adc rand+1 sta rand+1 adc rand+2 sta rand+2 adc rand+3 sta rand+3 clc lda rand+0 ; SEED += $31415927 adc #$27 sta rand+0 lda rand+1 adc #$59 sta rand+1 pha lda rand+2 adc #$41 sta rand+2 and #$7f ; Suppress sign bit (make it positive) tax lda rand+3 adc #$31 sta rand+3 pla ; return bit 8-22 in (X,A) rts _srand: sta rand+0 ; Store the seed stx rand+1 lda #0 sta rand+2 ; Set MSW to zero sta rand+3 rts
wagiminator/C64-Collection
16,381
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/_printf.s
; ; _printf: Basic layer for all printf type functions. ; ; Ullrich von Bassewitz, 21.10.2000 ; .export __printf .import popax, pushax, pusheax, decsp6, push1, axlong, axulong .import _ltoa, _ultoa .import _strlower, _strlen .importzp sp, ptr1, ptr2, tmp1, regbank, sreg .macpack generic ; ---------------------------------------------------------------------------- ; We will store variables into the register bank in the zeropage. Define ; equates for these variables. ArgList = regbank+0 ; Argument list pointer Format = regbank+2 ; Format string OutData = regbank+4 ; Function parameters ; ---------------------------------------------------------------------------- ; Other zero page cells Base = ptr1 FSave = ptr1 FCount = ptr2 .code ; ---------------------------------------------------------------------------- ; Get one character from the format string and increment the pointer. Will ; return zero in Y. GetFormatChar: ldy #0 lda (Format),y IncFormatPtr: inc Format bne @L1 inc Format+1 @L1: rts ; ---------------------------------------------------------------------------- ; Output a pad character: outfunc (d, &padchar, 1) OutputPadChar: lda PadChar ; ---------------------------------------------------------------------------- ; Call the output function with one character in A Output1: sta CharArg jsr PushOutData lda #<CharArg ldx #>CharArg jsr pushax jsr push1 jmp CallOutFunc ; fout (OutData, &CharArg, 1) ; ---------------------------------------------------------------------------- ; Decrement the argument list pointer by 2 DecArgList2: lda ArgList sub #2 sta ArgList bcs @L1 dec ArgList+1 @L1: rts ; ---------------------------------------------------------------------------- ; Get an unsigned int or long argument depending on the IsLong flag. GetUnsignedArg: lda IsLong ; Check flag bne GetLongArg ; Long sets all jsr GetIntArg ; Get an integer argument jmp axulong ; Convert to unsigned long ; ---------------------------------------------------------------------------- ; Get an signed int or long argument depending on the IsLong flag. GetSignedArg: lda IsLong ; Check flag bne GetLongArg ; Long sets all jsr GetIntArg ; Get an integer argument jmp axlong ; Convert to long ; ---------------------------------------------------------------------------- ; Get a long argument from the argument list. Returns 0 in Y. GetLongArg: jsr GetIntArg ; Get high word sta sreg stx sreg+1 ; Run into GetIntArg fetching the low word ; ---------------------------------------------------------------------------- ; Get an integer argument from the argument list. Returns 0 in Y. GetIntArg: jsr DecArgList2 ldy #1 lda (ArgList),y tax dey lda (ArgList),y rts ; ---------------------------------------------------------------------------- ; Read an integer from the format string. Will return zero in Y. ReadInt: ldy #0 sty ptr1 sty ptr1+1 ; Start with zero @Loop: lda (Format),y ; Get format string character sub #'0' ; Make number from ascii digit bcc @L9 ; Jump if done cmp #9+1 bcs @L9 ; Jump if done ; Skip the digit character jsr IncFormatPtr ; Add the digit to the value we have in ptr1 pha ; Save digit value lda ptr1 ldx ptr1+1 asl ptr1 rol ptr1+1 ; * 2 asl ptr1 rol ptr1+1 ; * 4, assume carry clear adc ptr1 sta ptr1 txa adc ptr1+1 sta ptr1+1 ; * 5 asl ptr1 rol ptr1+1 ; * 10, assume carry clear pla adc ptr1 ; Add digit value sta ptr1 bcc @Loop inc ptr1+1 bcs @Loop ; Branch always ; We're done converting @L9: lda ptr1 ldx ptr1+1 ; Load result rts ; ---------------------------------------------------------------------------- ; Put a character into the argument buffer and increment the buffer index PutBuf: ldy BufIdx inc BufIdx sta Buf,y rts ; ---------------------------------------------------------------------------- ; Get a pointer to the current buffer end and push it onto the stack PushBufPtr: lda #<Buf ldx #>Buf add BufIdx bcc @L1 inx @L1: jmp pushax ; ---------------------------------------------------------------------------- ; Push OutData onto the software stack PushOutData: lda OutData ldx OutData+1 jmp pushax ; ---------------------------------------------------------------------------- ; Output Width pad characters ; PadLoop: jsr OutputPadChar OutputPadding: inc Width bne PadLoop inc Width+1 bne PadLoop rts ; ---------------------------------------------------------------------------- ; Output the argument itself: outfunc (d, str, arglen); ; OutputArg: jsr PushOutData lda Str ldx Str+1 jsr pushax lda ArgLen ldx ArgLen+1 jsr pushax jmp CallOutFunc ; ---------------------------------------------------------------------------- ; ltoa: Wrapper for _ltoa that pushes all arguments ltoa: sty Base ; Save base jsr pusheax ; Push value jsr PushBufPtr ; Push the buffer pointer... lda Base ; Restore base jmp _ltoa ; ultoa (l, s, base); ; ---------------------------------------------------------------------------- ; ultoa: Wrapper for _ultoa that pushes all arguments ultoa: sty Base ; Save base jsr pusheax ; Push value jsr PushBufPtr ; Push the buffer pointer... lda Base ; Restore base jmp _ultoa ; ultoa (l, s, base); ; ---------------------------------------------------------------------------- ; __printf: ; Save the register bank variables into the save area pha ; Save low byte of ap ldy #5 Save: lda regbank,y sta RegSave,y dey bpl Save ; Get the parameters from the stack pla ; Restore low byte of ap sta ArgList ; Argument list pointer stx ArgList+1 jsr popax ; Format string sta Format stx Format+1 jsr popax ; Output descriptor sta OutData stx OutData+1 ; Initialize the output counter in the output descriptor to zero lda #0 tay sta (OutData),y iny sta (OutData),y ; Get the output function from the output descriptor and remember it iny lda (OutData),y sta CallOutFunc+1 iny lda (OutData),y sta CallOutFunc+2 ; Start parsing the format string MainLoop: lda Format ; Remember current format pointer sta FSave lda Format+1 sta FSave+1 ldy #0 ; Index @L1: lda (Format),y ; Get next char beq @L2 ; Jump on end of string cmp #'%' ; Format spec? beq @L2 iny ; Bump pointer bne @L1 inc Format+1 ; Bump high byte of pointer bne @L1 ; Branch always ; Found a '%' character or end of string. Update the Format pointer so it is ; current (points to this character). @L2: tya ; Low byte of offset add Format sta Format bcc @L3 inc Format+1 ; Calculate, how many characters must be output. Beware: This number may ; be zero. A still contains the low byte of the pointer. @L3: sub FSave sta FCount lda Format+1 sbc FSave+1 sta FCount+1 ora FCount ; Is the result zero? beq @L4 ; Jump if yes ; Output the characters that we have until now. To make the call to out ; faster, build the stack frame by hand (don't use pushax) jsr decsp6 ; 3 args ldy #5 lda OutData+1 sta (sp),y dey lda OutData sta (sp),y dey lda FSave+1 sta (sp),y dey lda FSave sta (sp),y dey lda FCount+1 sta (sp),y dey lda FCount sta (sp),y jsr CallOutFunc ; Call the output function ; We're back from out(), or we didn't call it. Check for end of string. @L4: jsr GetFormatChar ; Get one char, zero in Y tax ; End of format string reached? bne NotDone ; End not reached ; End of format string reached. Restore the zeropage registers and return. ldx #5 Rest: lda RegSave,x sta regbank,x dex bpl Rest rts ; Still a valid format character. Check for '%' and a '%%' sequence. Output ; anything that is not a format specifier. On intro, Y is zero. NotDone: cmp #'%' bne @L1 lda (Format),y ; Check for "%%" cmp #'%' bne FormatSpec ; Jump if really a format specifier jsr IncFormatPtr ; Skip the second '%' @L1: jsr Output1 ; Output the character... jmp MainLoop ; ...and continue ; We have a real format specifier ; Format is: %[flags][width][.precision][mod]type ; Y is zero on entry. FormatSpec: ; Initialize the flags lda #0 ldx #FormatVarSize-1 @L1: sta FormatVars,x dex bpl @L1 ; Start with reading the flags if there are any. X is $FF which is used ; for "true" ReadFlags: lda (Format),y ; Get next char... cmp #'-' bne @L1 stx LeftJust beq @L4 @L1: cmp #'+' bne @L2 stx AddSign beq @L4 @L2: cmp #' ' bne @L3 stx AddBlank beq @L4 @L3: cmp #'#' bne ReadPadding stx AltForm @L4: jsr IncFormatPtr jmp ReadFlags ; ...and start over ; Done with flags, read the pad char. Y is still zero if we come here. ReadPadding: ldx #' ' ; PadChar cmp #'0' bne @L1 tax ; PadChar is '0' jsr IncFormatPtr lda (Format),y ; Read current for later @L1: stx PadChar ; Read the width. Even here, Y is still zero. A contains the current character ; from the format string ReadWidth: cmp #'*' bne @L1 jsr IncFormatPtr jsr GetIntArg ; Width is an additional argument jmp @L2 @L1: jsr ReadInt ; Read integer from format string... @L2: sta Width stx Width+1 ; ...and remember in Width ; Read the precision. Even here, Y is still zero. sty Prec ; Assume Precision is zero sty Prec+1 lda (Format),y ; Load next format string char cmp #'.' ; Precision given? bne ReadMod ; Branch if no precision given ReadPrec: jsr IncFormatPtr ; Skip the '.' lda (Format),y cmp #'*' ; Variable precision? bne @L1 jsr IncFormatPtr ; Skip the '*' jsr GetIntArg ; Get integer argument jmp @L2 @L1: jsr ReadInt ; Read integer from format string @L2: sta Prec stx Prec+1 ; Read the modifiers. Y is still zero. ReadMod: lda (Format),y cmp #'z' ; size_t - same as unsigned beq @L2 cmp #'h' ; short - same as int beq @L2 cmp #'t' ; ptrdiff_t - same as int beq @L2 cmp #'j' ; intmax_t/uintmax_t - same as long beq @L1 cmp #'L' ; long double beq @L1 cmp #'l' ; long int bne DoFormat @L1: lda #$FF sta IsLong @L2: jsr IncFormatPtr jmp ReadMod ; Initialize the argument buffer pointers. We use a static buffer (ArgBuf) to ; assemble strings. A zero page index (BufIdx) is used to keep the current ; write position. A pointer to the buffer (Str) is used to point to the the ; argument in case we will not use the buffer but a user supplied string. ; Y is zero when we come here. DoFormat: sty BufIdx ; Clear BufIdx ldx #<Buf stx Str ldx #>Buf stx Str+1 ; Skip the current format character, then check it (current char in A) jsr IncFormatPtr ; Is it a character? cmp #'c' bne CheckInt ; It is a character jsr GetIntArg ; Get the argument (promoted to int) sta Buf ; Place it as zero terminated string... lda #0 sta Buf+1 ; ...into the buffer jmp HaveArg ; Done ; Is it an integer? CheckInt: cmp #'d' beq @L1 cmp #'i' bne CheckCount ; It is an integer @L1: ldx #0 lda AddBlank ; Add a blank for positives? beq @L2 ; Jump if no ldx #' ' @L2: lda AddSign ; Add a plus for positives (precedence)? beq @L3 ldx #'+' @L3: stx Leader ; Integer argument jsr GetSignedArg ; Get argument as a long ldy sreg+1 ; Check sign bmi @Int1 ldy Leader beq @Int1 sty Buf inc BufIdx @Int1: ldy #10 ; Base jsr ltoa ; Push arguments, call _ltoa jmp HaveArg ; Is it a count pseudo format? CheckCount: cmp #'n' bne CheckOctal ; It is a count pseudo argument jsr GetIntArg sta ptr1 stx ptr1+1 ; Get user supplied pointer ldy #0 lda (OutData),y ; Low byte of OutData->ccount sta (ptr1),y iny lda (OutData),y ; High byte of OutData->ccount sta (ptr1),y jmp MainLoop ; Done ; Check for an octal digit CheckOctal: cmp #'o' bne CheckPointer ; Integer in octal representation jsr GetSignedArg ; Get argument as a long ldy AltForm ; Alternative form? beq @Oct1 ; Jump if no pha ; Save low byte of value stx tmp1 ora tmp1 ora sreg ora sreg+1 ora Prec ora Prec+1 ; Check if value or Prec != 0 beq @Oct1 lda #'0' jsr PutBuf pla ; Restore low byte @Oct1: ldy #8 ; Load base jsr ltoa ; Push arguments, call _ltoa jmp HaveArg ; Check for a pointer specifier (%p) CheckPointer: cmp #'p' bne CheckString ; It's a pointer. Use %#x conversion ldx #0 stx IsLong ; IsLong = 0; inx stx AltForm ; AltForm = 1; lda #'x' bne IsHex ; Branch always ; Check for a string specifier (%s) CheckString: cmp #'s' bne CheckUnsigned ; It's a string jsr GetIntArg ; Get 16bit argument sta Str stx Str+1 jmp HaveArg ; Check for an unsigned integer (%u) CheckUnsigned: cmp #'u' bne CheckHex ; It's an unsigned integer jsr GetUnsignedArg ; Get argument as unsigned long ldy #10 ; Load base jsr ultoa ; Push arguments, call _ultoa jmp HaveArg ; Check for a hexadecimal integer (%x) CheckHex: cmp #'x' beq IsHex cmp #'X' bne UnknownFormat ; Hexadecimal integer IsHex: pha ; Save the format spec lda AltForm beq @L1 lda #'0' jsr PutBuf lda #'X' jsr PutBuf @L1: jsr GetUnsignedArg ; Get argument as an unsigned long ldy #16 ; Load base jsr ultoa ; Push arguments, call _ultoa pla ; Get the format spec cmp #'x' ; Lower case? bne @L2 lda Str ldx Str+1 jsr _strlower ; Make characters lower case @L2: jmp HaveArg ; Unknown format character, skip it UnknownFormat: jmp MainLoop ; We have the argument, do argument string formatting HaveArg: ; ArgLen = strlen (Str); lda Str ldx Str+1 jsr _strlen ; Get length of argument sta ArgLen stx ArgLen+1 ; if (Prec && Prec < ArgLen) ArgLen = Prec; lda Prec ora Prec+1 beq @L1 ldx Prec cpx ArgLen lda Prec+1 tay sbc ArgLen+1 bcs @L1 stx ArgLen sty ArgLen+1 ; if (Width > ArgLen) { ; Width -= ArgLen; /* padcount */ ; } else { ; Width = 0; ; } ; Since width is used as a counter below, calculate -(width+1) @L1: sec lda Width sbc ArgLen tax lda Width+1 sbc ArgLen+1 bcs @L2 lda #0 tax @L2: eor #$FF sta Width+1 txa eor #$FF sta Width ; /* Do padding on the left side if needed */ ; if (!leftjust) { ; /* argument right justified */ ; while (width) { ; fout (d, &padchar, 1); ; --width; ; } ; } lda LeftJust bne @L3 jsr OutputPadding ; Output the argument itself @L3: jsr OutputArg ; /* Output right padding bytes if needed */ ; if (leftjust) { ; /* argument left justified */ ; while (width) { ; fout (d, &padchar, 1); ; --width; ; } ; } lda LeftJust beq @L4 jsr OutputPadding ; Done, parse next chars from format string @L4: jmp MainLoop ; ---------------------------------------------------------------------------- ; Local data (all static) .bss ; Save area for the zero page registers RegSave: .res 6 ; One character argument for OutFunc CharArg: .byte 0 ; Format variables FormatVars: LeftJust: .byte 0 AddSign: .byte 0 AddBlank: .byte 0 AltForm: .byte 0 PadChar: .byte 0 Width: .word 0 Prec: .word 0 IsLong: .byte 0 Leader: .byte 0 BufIdx: .byte 0 ; Argument string pointer FormatVarSize = * - FormatVars ; Argument buffer and pointer Buf: .res 20 Str: .word 0 ArgLen: .res 2 .data ; Stuff from OutData. Is used as a vector and must be aligned CallOutFunc: jmp $0000
wagiminator/C64-Collection
1,317
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/stricmp.s
; ; Ullrich von Bassewitz, 03.06.1998 ; ; int stricmp (const char* s1, const char* s2); /* DOS way */ ; int strcasecmp (const char* s1, const char* s2); /* UNIX way */ ; .export _stricmp, _strcasecmp .import popax .import __ctype .importzp ptr1, ptr2, tmp1 .include "ctype.inc" _stricmp: _strcasecmp: sta ptr2 ; Save s2 stx ptr2+1 jsr popax ; get s1 sta ptr1 stx ptr1+1 ldy #0 loop: lda (ptr2),y ; get char from second string tax lda __ctype,x ; get character classification and #CT_LOWER ; lower case char? beq L1 ; jump if no txa ; get character back clc adc #<('A'-'a') ; make upper case char tax ; L1: stx tmp1 ; remember upper case equivalent lda (ptr1),y ; get character from first string tax lda __ctype,x ; get character classification and #CT_LOWER ; lower case char? beq L2 ; jump if no txa ; get character back clc adc #<('A'-'a') ; make upper case char tax L2: cpx tmp1 ; compare characters bne L3 txa ; end of strings? beq L5 ; a/x both zero iny bne loop inc ptr1+1 inc ptr2+1 bne loop L3: bcs L4 ldx #$FF rts L4: ldx #$01 L5: rts
wagiminator/C64-Collection
2,655
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/modfree.s
;*****************************************************************************/ ;* */ ;* modfree.s */ ;* */ ;* Free loaded o65 modules */ ;* */ ;* */ ;* */ ;* (C) 2002 Ullrich von Bassewitz */ ;* Wacholderweg 14 */ ;* D-70597 Stuttgart */ ;* EMail: uz@musoftware.de */ ;* */ ;* */ ;* This software is provided 'as-is', without any expressed or implied */ ;* warranty. In no event will the authors be held liable for any damages */ ;* arising from the use of this software. */ ;* */ ;* Permission is granted to anyone to use this software for any purpose, */ ;* including commercial applications, and to alter it and redistribute it */ ;* freely, subject to the following restrictions: */ ;* */ ;* 1. The origin of this software must not be misrepresented; you must not */ ;* claim that you wrote the original software. If you use this software */ ;* in a product, an acknowledgment in the product documentation would be */ ;* appreciated but is not required. */ ;* 2. Altered source versions must be plainly marked as such, and must not */ ;* be misrepresented as being the original software. */ ;* 3. This notice may not be removed or altered from any source */ ;* distribution. */ ;* */ ;*****************************************************************************/ .export _mod_free .import _free _mod_free = _free ; Just free the memory
wagiminator/C64-Collection
4,221
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/vsscanf.s
; ; int __fastcall__ vsscanf (const char* str, const char* format, va_list ap); ; /* Standard C function */ ; ; 2004-11-28, Ullrich von Bassewitz ; 2004-12-21, Greg King ; .export _vsscanf .import popax, __scanf .importzp sp, ptr1, ptr2 .macpack generic ; ---------------------------------------------------------------------------- ; Structure used to pass data to the callback functions .struct SSCANFDATA STR .addr INDEX .word .endstruct ; ---------------------------------------------------------------------------- ; static int __fastcall__ get (struct sscanfdata* d) ; /* Read a character from the input string and return it */ ; { ; char C = d->str[d->index]; ; if (C == '\0') { ; return EOF; ; } ; /* Increment index only if end not reached */ ; ++d->index; ; return C; ; } ; .code .proc get sta ptr1 stx ptr1+1 ; Save d ; Get d->str adding the high byte of index to the pointer, so we can access ; the byte in the string with just the low byte as index ldy #SSCANFDATA::STR lda (ptr1),y sta ptr2 iny lda (ptr1),y ldy #SSCANFDATA::INDEX+1 add (ptr1),y sta ptr2+1 ; Load the low byte of the index and fetch the byte from the string dey ; = SSCANFDATA::INDEX lda (ptr1),y tay lda (ptr2),y ; Return EOF if we are at the end of the string bne L1 lda #$FF tax rts ; Bump the index (beware: A contains the char we must return) L1: tax ; Save return value tya ; Low byte of index ldy #SSCANFDATA::INDEX add #<1 sta (ptr1),y iny lda (ptr1),y adc #>1 sta (ptr1),y ; Return the char just read txa ldx #>0 rts .endproc ; ---------------------------------------------------------------------------- ; static int __fastcall__ unget (int c, struct sscanfdata* d) ; /* Push back a character onto the input stream */ ; { ; /* We do assume here that the _scanf routine will not push back anything ; * not read, so we can ignore c safely and won't check the index. ; */ ; --d->index; ; return c; ; } ; .code .proc unget sta ptr1 stx ptr1+1 ; Save d ; Decrement the index ldy #SSCANFDATA::INDEX lda (ptr1),y sub #<1 sta (ptr1),y iny lda (ptr1),y sbc #>1 sta (ptr1),y ; Return c jmp popax .endproc ; ---------------------------------------------------------------------------- ; int __fastcall__ vsscanf (const char* str, const char* format, va_list ap) ; /* Standard C function */ ; { ; /* Initialize the data structs. The sscanfdata struct will be passed back ; * to the get and unget functions by _scanf(). ; */ ; static struct sscanfdata sd; ; static const struct scanfdata d = { ; ( getfunc) get, ; (ungetfunc) unget, ; (void*) &sd ; }; ; ; sd.str = str; ; sd.index = 0; ; ; /* Call the internal function and return the result */ ; return _scanf (&d, format, ap); ; } ; .bss sd: .tag SSCANFDATA .rodata d: .addr get .addr unget .addr sd .code .proc _vsscanf ; Save the low byte of ap (which is passed in a/x) pha ; Initialize sd and at the same time replace str on the stack by a pointer ; to d ldy #2 ; Stack offset of str lda (sp),y sta sd + SSCANFDATA::STR lda #<d sta (sp),y iny lda (sp),y sta sd + SSCANFDATA::STR+1 lda #>d sta (sp),y lda #$00 sta sd + SSCANFDATA::INDEX sta sd + SSCANFDATA::INDEX+1 ; Restore the low byte of ap, and jump to _scanf() which will clean up the stack pla jmp __scanf .endproc
wagiminator/C64-Collection
1,131
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/_heapadd.s
; ; Ullrich von Bassewitz, 21.7.2000 ; ; Add a block to the heap free list ; ; void __fastcall__ _heapadd (void* mem, size_t size); ; ; .importzp ptr1, ptr2 .import popax .import heapadd .export __heapadd .include "_heap.inc" .macpack generic ;----------------------------------------------------------------------------- ; Code __heapadd: sta ptr1 ; Store size in ptr1 stx ptr1+1 jsr popax ; Get the block pointer sta ptr2 stx ptr2+1 ; Store block pointer in ptr2 ; Check if size is greater or equal than min_size. Otherwise we don't care ; about the block (this may only happen for user supplied blocks, blocks ; from the heap are always large enough to hold a freeblock structure). lda ptr1 ; Load low byte ldx ptr1+1 ; Load/check high byte bne @L1 cmp #HEAP_MIN_BLOCKSIZE bcs @L1 rts ; Block not large enough ; The block is large enough. Set the size field in the block. @L1: ldy #usedblock::size sta (ptr2),y iny txa sta (ptr2),y ; Call the internal function since variables are now setup correctly jmp heapadd
wagiminator/C64-Collection
1,832
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/fscanf.s
; ; int fscanf (FILE* f, const char* format, ...); ; ; Ullrich von Bassewitz, 2004-11-28 ; .export _fscanf .import addysp, decsp4, _vfscanf .importzp sp, ptr1 .macpack generic ; ---------------------------------------------------------------------------- ; Data .bss ParamSize: .res 1 ; Number of parameter bytes ; ---------------------------------------------------------------------------- ; int fscanf (FILE* f, const char* format, ...) ; /* Standard C function */ ; { ; va_list ap; ; ; /* Setup for variable arguments */ ; va_start (ap, format); ; ; /* Call vfscanf(). Since we know that va_end won't do anything, we will ; * save the call and return the value directly. ; */ ; return vfscanf (f, format, ap); ; } ; .code _fscanf: sty ParamSize ; Number of param bytes passed in Y ; We have to push f and format, both in the order they already have on stack. ; To make this somewhat more efficient, we will create space on the stack and ; then do a copy of the complete block instead of pushing each parameter ; separately. Since the size of the arguments passed is the same as the size ; of the fixed arguments, this will allow us to calculate the pointer to the ; fixed size arguments easier (they're just ParamSize bytes away). jsr decsp4 ; Calculate a pointer to the Format argument lda ParamSize add sp sta ptr1 ldx sp+1 bcc @L1 inx @L1: stx ptr1+1 ; Now copy both, f and format ldy #4-1 @L2: lda (ptr1),y sta (sp),y dey bpl @L2 ; Load va_list (last and __fastcall__ parameter to vfscanf) lda ptr1 ldx ptr1+1 ; Call vfscanf jsr _vfscanf ; Cleanup the stack. We will return what we got from vfscanf ldy ParamSize jmp addysp
wagiminator/C64-Collection
1,444
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/_heapmemavail.s
; ; Ullrich von Bassewitz, 2003-02-01 ; ; Return the amount of free memory on the heap. ; ; size_t __fastcall__ _heapmemavail (void); ; ; .importzp ptr1, ptr2 .export __heapmemavail .include "_heap.inc" .macpack generic ;----------------------------------------------------------------------------- ; Code __heapmemavail: ; size_t Size = 0; lda #0 sta ptr2 sta ptr2+1 ; struct freeblock* F = _heapfirst; lda __heapfirst sta ptr1 lda __heapfirst+1 @L1: sta ptr1+1 ; while (F) { ora ptr1 beq @L2 ; Jump if end of free list reached ; Size += F->size; ldy #freeblock::size lda (ptr1),y add ptr2 sta ptr2 iny lda (ptr1),y adc ptr2+1 sta ptr2+1 ; F = F->next; iny ; Points to F->next lda (ptr1),y tax iny lda (ptr1),y stx ptr1 jmp @L1 ; return Size + (_heapend - _heapptr) * sizeof (*_heapend); @L2: lda ptr2 add __heapend sta ptr2 lda ptr2+1 adc __heapend+1 tax lda ptr2 sub __heapptr sta ptr2 txa sbc __heapptr+1 tax lda ptr2 rts
wagiminator/C64-Collection
2,506
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/vfscanf.s
; ; int __fastcall__ vfscanf (FILE* f, const char* format, va_list ap); ; ; 2004-11-27, Ullrich von Bassewitz ; 2004-12-21, Greg King ; .export _vfscanf .import _fgetc, _ungetc, _ferror .include "zeropage.inc" .include "_scanf.inc" .include "stdio.inc" count := ptr3 ; Result of scan ; ---------------------------------------------------------------------------- ; Static scanfdata structure for the _vfscanf routine ; .data d: .addr _fgetc ; GET .addr _ungetc ; UNGET .addr 0 ; data ; ---------------------------------------------------------------------------- ; int __fastcall__ vfscanf (FILE* f, const char* format, va_list ap) ; /* Standard C function */ ; { ; /* Initialize the data struct. We do only need the given file as user data, ; * because the (getfunc) and (ungetfunc) functions are crafted so that they ; * match the standard-I/O fgetc() and ungetc(). ; */ ; static struct scanfdata d = { ; ( getfunc) fgetc, ; (ungetfunc) ungetc ; }; ; static int count; ; ; d.data = (void*) f; ; ; /* Call the internal function */ ; count = _scanf (&d, format, ap); ; ; /* And, return the result */ ; return ferror (f) ? EOF : count; ; } ; ; Because _scanf() has the same parameter stack as vfscanf(), with f replaced ; by &d, we will do exactly that. _scanf() then will clean up the stack. ; Beware: Since ap is a fastcall parameter, we must not destroy a/x. ; .code _vfscanf: pha ; Save low byte of ap ; Swap f against &d on the stack, placing f into d.data ldy #2 ; Offset of f on the stack lda (sp),y sta d + SCANFDATA::DATA lda #<d sta (sp),y iny ; High byte lda (sp),y sta d + SCANFDATA::DATA + 1 lda #>d sta (sp),y ; Restore the low byte of ap, and call the _scanf function pla jsr __scanf sta count stx count+1 ; Return -1 if there was a read error during the scan lda d + SCANFDATA::DATA ; Get f ldx d + SCANFDATA::DATA+1 jsr _ferror tay beq L1 lda #<EOF tax rts ; Or, return the result of the scan L1: lda count ldx count+1 rts
wagiminator/C64-Collection
1,217
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/raise.s
; ; Ullrich von Bassewitz, 2003-03-14 ; ; int __fastcall__ raise (int sig); ; .import jmpvec .include "signal.inc" ;---------------------------------------------------------------------------- ; int __fastcall__ raise (int sig); _raise: cpx #0 bne invalidsig cmp #SIGCOUNT bcs invalidsig ; Save the signal number low byte, then setup the function vector pha asl a tax lda sigtable,x sta jmpvec+1 lda sigtable+1,x sta jmpvec+2 ; Reset the signal handler to SIG_DFL (I don't like this because it may ; introduce race conditions, but it's the simplest way to satisfy the ; standard). lda #<__sig_dfl sta sigtable,x lda #>__sig_dfl sta sigtable+1,x ; Restore the signal number and call the function pla ; Low byte ldx #0 ; High byte jsr jmpvec ; Call signal function ; raise() returns zero on success and any other value on failure lda #0 tax invalidsig: rts
wagiminator/C64-Collection
2,143
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/memcpy.s
; ; Ullrich von Bassewitz, 2003-08-20 ; Performance increase (about 20%) by ; Christian Krueger, 2009-09-13 ; ; void* __fastcall__ memcpy (void* dest, const void* src, size_t n); ; ; NOTE: This function contains entry points for memmove, which will ressort ; to memcpy for an upwards copy. Don't change this module without looking ; at memmove! ; .export _memcpy, memcpy_upwards, memcpy_getparams .import popax .importzp sp, ptr1, ptr2, ptr3 ; ---------------------------------------------------------------------- _memcpy: jsr memcpy_getparams memcpy_upwards: ; assert Y = 0 ldx ptr3+1 ; Get high byte of n beq L2 ; Jump if zero L1: .repeat 2 ; Unroll this a bit to make it faster... lda (ptr1),Y ; copy a byte sta (ptr2),Y iny .endrepeat bne L1 inc ptr1+1 inc ptr2+1 dex ; Next 256 byte block bne L1 ; Repeat if any ; the following section could be 10% faster if we were able to copy ; back to front - unfortunately we are forced to copy strict from ; low to high since this function is also used for ; memmove and blocks could be overlapping! ; { L2: ; assert Y = 0 ldx ptr3 ; Get the low byte of n beq done ; something to copy L3: lda (ptr1),Y ; copy a byte sta (ptr2),Y iny dex bne L3 ; } done: jmp popax ; Pop ptr and return as result ; ---------------------------------------------------------------------- ; Get the parameters from stack as follows: ; ; size --> ptr3 ; src --> ptr1 ; dest --> ptr2 ; First argument (dest) will remain on stack and is returned in a/x! memcpy_getparams: ; IMPORTANT! Function has to leave with Y=0! sta ptr3 stx ptr3+1 ; save n to ptr3 jsr popax sta ptr1 stx ptr1+1 ; save src to ptr1 ; save dest to ptr2 ldy #1 ; (direct stack access is three cycles faster ; (total cycle count with return)) lda (sp),y tax stx ptr2+1 ; save high byte of ptr2 dey ; Y = 0 lda (sp),y ; Get ptr2 low sta ptr2 rts
wagiminator/C64-Collection
1,894
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/utscopy.s
; ; Ullrich von Bassewitz, 2003-08-12 ; ; This module contains a utility function for the machine dependent parts ; of uname (__sysuname): It copies a packed struct utsname (just the strings ; without padding) into a struct utsname. The source of the data is an ; external symbol named "utsdata", the target is passed in a/x. ; The function is written so that it is a direct replacement for __sysuname ; for systems where utsdata is fixed. It may also be called via jump or ; subroutine on systems where utsdata must be changed at runtime. ; .export utscopy .import utsdata .importzp ptr1, tmp1 .include "utsname.inc" ;-------------------------------------------------------------------------- ; Data. .rodata ; Table with offsets into struct utsname fieldoffs: .byte utsname::sysname .byte utsname::nodename .byte utsname::release .byte utsname::version .byte utsname::machine fieldcount = * - fieldoffs ;-------------------------------------------------------------------------- .code .proc utscopy sta ptr1 stx ptr1+1 ; Save buf ldx #0 stx tmp1 ; Field number next: ldy tmp1 cpy #fieldcount beq done inc tmp1 ; Bump field counter lda fieldoffs,y ; Get next field offset tay ; Field offset -> y loop: lda utsdata,x sta (ptr1),y inx ; Next char in utsdata cmp #$00 ; Check for end of string beq next ; Jump for next field iny ; Next char in utsname struct bne loop ; Copy string done: lda #$00 ; Always successful rts .endproc
wagiminator/C64-Collection
2,953
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/_fopen.s
; ; Ullrich von Bassewitz, 22.11.2002 ; ; FILE* __fastcall__ _fopen (const char* name, const char* mode, FILE* f); ; /* Open the specified file and fill the descriptor values into f */ ; .export __fopen .import _open .import pushax, incsp4, return0 .importzp sp, ptr1 .include "errno.inc" .include "fcntl.inc" .include "_file.inc" ; ------------------------------------------------------------------------ ; Code .proc __fopen sta file stx file+1 ; Save f ; Get a pointer to the mode string ldy #1 lda (sp),y sta ptr1+1 dey lda (sp),y sta ptr1 ; Look at the first character in mode ldx #$00 ; Mode will be in X lda (ptr1),y ; Get first char from mode cmp #'w' bne @L1 ldx #(O_WRONLY | O_CREAT | O_TRUNC) bne @L3 @L1: cmp #'r' bne @L2 ldx #O_RDONLY bne @L3 @L2: cmp #'a' bne invmode ldx #(O_WRONLY | O_CREAT | O_APPEND) ; Look at more chars from the mode string @L3: iny ; Next char beq invmode lda (ptr1),y beq modeok ; End of mode string reached cmp #'+' bne @L4 txa ora #O_RDWR ; Always do r/w in addition to anything else tax bne @L3 @L4: cmp #'b' beq @L3 ; Binary mode is ignored ; Invalid mode invmode: lda #EINVAL jsr __seterrno ; Set __errno, returns zero in A tax ; a/x = 0 jmp incsp4 ; Mode string successfully parsed. Store the binary mode onto the stack in ; the same place where the mode string pointer was before. Then call open() modeok: ldy #$00 txa ; Mode -> A sta (sp),y tya iny sta (sp),y ldy #4 ; Size of arguments in bytes jsr _open ; Will cleanup the stack ; Check the result of the open() call cpx #$FF bne openok cmp #$FF bne openok jmp return0 ; Failure, errno/_oserror already set ; Open call succeeded openok: ldy file sty ptr1 ldy file+1 sty ptr1+1 ldy #_FILE::f_fd sta (ptr1),y ; file->f_fd = fd; ldy #_FILE::f_flags lda #_FOPEN sta (ptr1),y ; file->f_flags = _FOPEN; ; Return the pointer to the file structure lda ptr1 ldx ptr1+1 rts .endproc ; ------------------------------------------------------------------------ ; Data .bss file: .res 2
wagiminator/C64-Collection
2,993
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/fwrite.s
; ; Ullrich von Bassewitz, 22.11.2002 ; ; size_t __fastcall__ fwrite (const void* buf, size_t size, size_t count, FILE* file); ; /* Write to a file */ ; .export _fwrite .import _write .import pushax, incsp6, addysp, ldaxysp, pushwysp, return0 .import tosumulax, tosudivax .importzp ptr1 .include "errno.inc" .include "_file.inc" ; ------------------------------------------------------------------------ ; Code .proc _fwrite ; Save file and place it into ptr1 sta file sta ptr1 stx file+1 stx ptr1+1 ; Check if the file is open ldy #_FILE::f_flags lda (ptr1),y and #_FOPEN ; Is the file open? bne @L2 ; Branch if yes ; File not open lda #EINVAL jsr __seterrno @L1: jsr incsp6 jmp return0 ; Check if the stream is in an error state @L2: lda (ptr1),y ; get file->f_flags again and #_FERROR bne @L1 ; Build the stackframe for write() ldy #_FILE::f_fd lda (ptr1),y ldx #$00 jsr pushax ; file->f_fd ldy #9 jsr pushwysp ; buf ; Stack is now: buf/size/count/file->fd/buf ; Calculate the number of bytes to write: count * size ldy #7 jsr pushwysp ; count ldy #9 jsr ldaxysp ; Get size jsr tosumulax ; count * size -> a/x ; Check if the number of bytes is zero. Don't call write in this case cpx #0 bne @L3 cmp #0 bne @L3 ; The number of bytes to write is zero, just return count ldy #5 jsr ldaxysp ; Get count ldy #10 jmp addysp ; Drop params, return ; Call write(). This will leave the original 3 params on the stack @L3: jsr _write ; Check for errors in write cpx #$FF bne @L4 cmp #$FF bne @L4 ; Error in write. Set the stream error flag and bail out. _oserror and/or ; errno are already set by write(). lda file sta ptr1 lda file+1 sta ptr1+1 ldy #_FILE::f_flags lda (ptr1),y ora #_FERROR sta (ptr1),y bne @L1 ; Return zero ; Write was ok. Return the number of items successfully written. Since we've ; checked for bytes == 0 above, size cannot be zero here, so the division is ; safe. @L4: jsr pushax ; Push number of bytes written ldy #5 jsr ldaxysp ; Get size jsr tosudivax ; bytes / size -> a/x jmp incsp6 ; Drop params, return .endproc ; ------------------------------------------------------------------------ ; Data .bss file: .res 2
wagiminator/C64-Collection
1,098
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/calloc.s
; ; Ullrich von Bassewitz, 15.11.2001 ; ; Allocate a block and zero it. ; ; void* __fastcall__ calloc (size_t count, size_t size); ; .export _calloc .import _malloc, _memset .import tosumulax, pushax, push0 ; ------------------------------------------------------------------------- .proc _calloc ; We have the first argument in a/x and the second on the stack. Calling ; tosumulax will give the product of both in a/x. jsr tosumulax ; Save size for later sta Size stx Size+1 ; malloc() is a fastcall function, so we do already have the argument in ; the right place jsr _malloc ; Check for a NULL pointer cpx #0 bne ClearBlock cmp #0 beq ClearBlock ; We have a NULL pointer, bail out rts ; No NULL pointer, clear the block. memset will return a pointer to the ; block which is exactly what we want. ClearBlock: jsr pushax ; ptr jsr push0 ; #0 lda Size ldx Size+1 ; Size jmp _memset .endproc ; ------------------------------------------------------------------------- ; Data .bss Size: .res 2
wagiminator/C64-Collection
1,169
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/vscanf.s
; ; int __fastcall__ vscanf (const char* format, va_list ap); ; ; Ullrich von Bassewitz, 2004-11-27 ; .export _vscanf .import _vfscanf .import _stdin .import decsp2 .include "zeropage.inc" ; ---------------------------------------------------------------------------- ; int __fastcall__ vscanf (const char* format, va_list ap) ; /* Standard C function */ ; { ; return vfscanf (stdin, format, ap); ; } ; .code _vscanf: pha ; Save low byte of ap ; Decrement the stack pointer by two for the additional parameter. jsr decsp2 ; Won't touch X ; Move the format down ldy #2 lda (sp),y ; Load byte of format ldy #0 sta (sp),y ldy #3 lda (sp),y ldy #1 sta (sp),y ; Store stdin into the stack frame iny lda _stdin sta (sp),y iny lda _stdin+1 sta (sp),y ; Restore the low byte of ap and jump to vfscanf, which will cleanup the stack pla jmp _vfscanf
wagiminator/C64-Collection
1,720
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/signal.s
; ; Ullrich von Bassewitz, 2002-12-16 ; ; __sigfunc __fastcall__ signal (int sig, __sigfunc func); ; .import popax .importzp ptr1 .include "signal.inc" .include "errno.inc" ; Default signal functions: The standard specifies explicitly that the values ; for SIG_IGN and SIG_DFL must be distinct, so we make them so by using both ; rts exits we have. This works because signal functions are __fastcall__, so ; we don't have arguments on the stack. ;---------------------------------------------------------------------------- ; __sigfunc __fastcall__ signal (int sig, __sigfunc func); _signal: sta ptr1 stx ptr1+1 ; Remember func jsr popax ; Get sig cpx #0 bne invalidsig cmp #SIGCOUNT bcs invalidsig ; Signal number is valid. Replace the pointer in the table saving the old ; value temporarily on the stack. asl a ; Prepare for word access tax sei ; Disable interrupts in case of async signals lda sigtable,x pha lda ptr1 sta sigtable,x lda sigtable+1,x pha lda ptr1+1 sta sigtable+1,x cli ; Reenable interrupts ; Get the old value from the stack and return it pla tax pla __sig_ign: rts ; Error entry: We use our knowledge that SIG_ERR is zero here to save a byte invalidsig: lda #<EINVAL sta __errno lda #>EINVAL ; A = 0 sta __errno+1 tax ; A/X = 0 __sig_dfl: rts
wagiminator/C64-Collection
1,769
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/sscanf.s
; ; int sscanf (const char* str, const char* format, ...); ; ; Ullrich von Bassewitz, 2004-11-28 ; .export _sscanf .import addysp, decsp4, _vsscanf .importzp sp, ptr1 .macpack generic ; ---------------------------------------------------------------------------- ; Data .bss ParamSize: .res 1 ; Number of parameter bytes ; ---------------------------------------------------------------------------- ; Code ; int sscanf (const char* str, const char* format, ...) ; /* Standard C function */ ; { ; va_list ap; ; ; /* Setup for variable arguments */ ; va_start (ap, format); ; ; /* Call vsscanf(). Since we know that va_end won't do anything, we will ; * save the call and return the value directly. ; */ ; return vsscanf (str, format, ap); ; } ; .code _sscanf: sty ParamSize ; Number of param bytes passed in Y ; We have to push buf and format, both in the order they already have on stack. ; To make this somewhat more efficient, we will create space on the stack and ; then do a copy of the complete block instead of pushing each parameter ; separately. Since the size of the arguments passed is the same as the size ; of the fixed arguments, this will allow us to calculate the pointer to the ; fixed size arguments easier (they're just ParamSize bytes away). jsr decsp4 ; Calculate a pointer to the fixed parameters lda ParamSize add sp sta ptr1 ldx sp+1 bcc @L1 inx @L1: stx ptr1+1 ; Now copy both, str and format ldy #4-1 @L2: lda (ptr1),y sta (sp),y dey bpl @L2 ; Load va_list (last and __fastcall__ parameter to vsscanf) lda ptr1 ldx ptr1+1 ; Call vsscanf jsr _vsscanf ; Cleanup the stack. We will return what we got from vsscanf ldy ParamSize jmp addysp
wagiminator/C64-Collection
5,491
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/fread.s
; ; Ullrich von Bassewitz, 2002-11-22, 2004-05-14 ; ; size_t __fastcall__ fread (void* buf, size_t size, size_t count, FILE* file); ; /* Read from a file */ ; .export _fread .import _read .import pusha0, pushax .import incsp4, incsp6 .import ldaxysp, ldax0sp .import pushwysp .import tosumulax, tosudivax .importzp ptr1, sp .include "errno.inc" .include "_file.inc" .macpack generic ; ------------------------------------------------------------------------ ; Code .proc _fread ; We will place a pointer to the file descriptor into the register bank .import regbank: zp file = regbank ; Save two bytes from the register bank ldy file+0 sty save ldy file+1 sty save+1 ; Save the file pointer into the register bank sta file stx file+1 ; Check if the file is open ldy #_FILE::f_flags lda (file),y and #_FOPEN ; Is the file open? beq @L1 ; Branch if no ; Check if the stream is in an error state lda (file),y ; get file->f_flags again and #_FERROR beq @L2 ; File not open or in error state @L1: lda #EINVAL jsr __seterrno ; Set __errno, return zero in A tax ; a/x = 0 jmp @L99 ; Bail out ; Remember if we have a pushed back character and reset the flag. @L2: tax ; X = 0 lda (file),y and #_FPUSHBACK beq @L3 lda (file),y and #<~_FPUSHBACK sta (file),y ; file->f_flags &= ~_FPUSHBACK; inx ; X = 1 @L3: stx pb ; Build the stackframe for read() ldy #_FILE::f_fd lda (file),y jsr pusha0 ; file->f_fd ldy #9 jsr pushwysp ; buf ; Stack is now: buf/size/count/file->fd/buf ; Calculate the number of bytes to read: count * size ldy #7 jsr pushwysp ; count ldy #9 jsr ldaxysp ; Get size jsr tosumulax ; count * size -> a/x ; Check if count is zero. cmp #0 bne @L4 cpx #0 bne @L4 ; Count is zero, drop the stack frame just built and return count jsr incsp4 ; Drop file->fd/buf jsr ldax0sp ; Get count jmp @L99 ; Bail out ; Check if we have a buffered char from ungetc @L4: ldy pb beq @L6 ; We have a buffered char from ungetc. Save the low byte from count pha ; Copy the buffer pointer into ptr1, and increment the pointer value passed ; to read() by one, so read() starts to store data at buf+1. ldy #0 lda (sp),y sta ptr1 add #1 sta (sp),y iny lda (sp),y sta ptr1+1 adc #0 sta (sp),y ; ptr1 = buf++; ; Get the buffered character and place it as first character into the read ; buffer. ldy #_FILE::f_pushback lda (file),y ldy #0 sta (ptr1),y ; *buf = file->f_pushback; ; Restore the low byte of count and decrement count by one. This may result ; in count being zero, so check for that. pla sub #1 bcs @L5 dex @L5: cmp #0 bne @L6 cpx #0 beq @L8 ; Call read(). This will leave the original 3 params on the stack @L6: jsr _read ; Check for errors in read cpx #$FF bne @L8 cmp #$FF bne @L8 ; Error in read. Set the stream error flag and bail out. _oserror and/or ; errno are already set by read(). On entry to label @L7, X must be zero. inx ; X = 0 lda #_FERROR @L7: ldy #_FILE::f_flags ; X must be zero here! ora (file),y sta (file),y txa ; a/x = 0 beq @L99 ; Return zero ; Read was ok, account for the pushed back character (if any). @L8: add pb bcc @L9 inx ; Check for end of file. @L9: cmp #0 ; Zero bytes read? bne @L10 cpx #0 bne @L10 ; Zero bytes read. Set the EOF flag lda #_FEOF bne @L7 ; Set flag and return zero ; Return the number of items successfully read. Since we've checked for ; bytes == 0 above, size cannot be zero here, so the division is safe. @L10: jsr pushax ; Push number of bytes read ldy #5 jsr ldaxysp ; Get size jsr tosudivax ; bytes / size -> a/x @L99: ldy save ; Restore zp register sty file ldy save+1 sty file+1 jmp incsp6 ; Drop params, return .endproc ; ------------------------------------------------------------------------ ; Data .bss save: .res 2 pb: .res 1
wagiminator/C64-Collection
2,041
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/strstr.s
; ; Ullrich von Bassewitz, 11.12.1998 ; ; char* strstr (const char* haystack, const char* needle); ; .export _strstr .import popax .importzp ptr1, ptr2, ptr3, ptr4, tmp1 _strstr: sta ptr2 ; Save needle stx ptr2+1 sta ptr4 ; Setup temp copy for later jsr popax ; Get haystack sta ptr1 stx ptr1+1 ; Save haystack ; If needle is empty, return haystack ldy #$00 lda (ptr2),y ; Get first byte of needle beq @Found ; Needle is empty --> we're done ; Search for the beginning of the string (this is not an optimal search ; strategy [in fact, it's pretty dumb], but it's simple to implement). sta tmp1 ; Save start of needle @L1: lda (ptr1),y ; Get next char from haystack beq @NotFound ; Jump if end cmp tmp1 ; Start of needle found? beq @L2 ; Jump if so iny ; Next char bne @L1 inc ptr1+1 ; Bump high byte bne @L1 ; Branch always ; We found the start of needle in haystack @L2: tya ; Get offset clc adc ptr1 sta ptr1 ; Make ptr1 point to start bcc @L3 inc ptr1+1 ; ptr1 points to the start of needle now. Setup temporary pointers for the ; search. The low byte of ptr4 is already set. @L3: sta ptr3 lda ptr1+1 sta ptr3+1 lda ptr2+1 sta ptr4+1 ldy #1 ; First char is identical, so start on second ; Do the compare @L4: lda (ptr4),y ; Get char from needle beq @Found ; Jump if end of needle (-> found) cmp (ptr3),y ; Compare with haystack bne @L5 ; Jump if not equal iny ; Next char bne @L4 inc ptr3+1 inc ptr4+1 ; Bump hi byte of pointers bne @L4 ; Next char (branch always) ; The strings did not compare equal, search next start of needle @L5: ldy #1 ; Start after this char bne @L1 ; Branch always ; We found the start of needle @Found: lda ptr1 ldx ptr1+1 rts ; We reached end of haystack without finding needle @NotFound: lda #$00 ; return NULL tax rts
wagiminator/C64-Collection
1,363
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/ungetc.s
; ; Ullrich von Bassewitz, 2004-05-12 ; ; int __fastcall__ ungetc (int c, FILE* f); ; /* Push back a character into a file stream. */ ; .export _ungetc .import popax .import ptr1: zp, tmp1: zp .include "_file.inc" .include "errno.inc" ; ------------------------------------------------------------------------ ; Code .proc _ungetc ; Save the file pointer to ptr1 sta ptr1 stx ptr1+1 ; Get c from stack and save the lower byte in tmp1 jsr popax sta tmp1 ; c must be in char range txa bne error ; Check if the file is open ldy #_FILE::f_flags lda (ptr1),y and #_FOPEN ; Is the file open? beq error ; Branch if no ; Set the pushback flag and reset the end-of-file indicator lda (ptr1),y ora #_FPUSHBACK and #<~_FEOF sta (ptr1),y ; Store the character into the pushback buffer ldy #_FILE::f_pushback lda tmp1 sta (ptr1),y ; Done, return c ldx #0 rts ; File is not open or the character is invalid error: lda #EINVAL jsr __seterrno lda #$FF ; Return -1 tax rts .endproc
wagiminator/C64-Collection
1,142
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/getenv.s
; ; Ullrich von Bassewitz, 2005-04-21 ; ; char* __fastcall__ getenv (const char* name); ; ; Beware: putenv() knows about zero page usage in this module! ; .export _getenv .import __environ, __envcount .import searchenv .import return0 .import ptr1:zp, ptr3:zp, tmp1:zp .code ;---------------------------------------------------------------------------- ; getenv() .proc _getenv sta ptr1 stx ptr1+1 ; Save name ; Search for the string in the environment. searchenv will set the N flag if ; the string is not found, otherwise X contains the index of the entry, ptr3 ; contains the entry and Y the offset of the '=' in the string. jsr searchenv bpl found jmp return0 ; Not found, return NULL ; Found the entry. Calculate the pointer to the right side of the environment ; variable. Because we want to skip the '=', we will set the carry. found: ldx ptr3+1 ; High byte of result tya sec adc ptr3 bcc @L9 inx @L9: rts .endproc
wagiminator/C64-Collection
1,786
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/_heapblocksize.s
; ; Ullrich von Bassewitz, 2004-07-17 ; ; size_t __fastcall__ _heapblocksize (const void* ptr); ; ; Return the size of an allocated block. ; .importzp ptr1, ptr2 .export __heapblocksize .include "_heap.inc" .macpack generic .macpack cpu ;----------------------------------------------------------------------------- ; Code __heapblocksize: ; Below the user data is a pointer that points to the start of the real ; (raw) memory block. The first word of this block is the size. To access ; the raw block pointer, we will decrement the high byte of the pointer, ; the pointer is then at offset 254/255. sta ptr1 dex stx ptr1+1 ldy #$FE lda (ptr1),y sta ptr2 ; Place the raw block pointer into ptr2 iny lda (ptr1),y sta ptr2+2 ; Load the size from the raw block ldy #usedblock::size+1 lda (ptr2),y tax .if (.cpu .bitand CPU_ISET_65SC02) lda (ptr2) .else dey lda (ptr2),y .endif ; Correct the raw block size so that is shows the user visible portion. To ; do that, we must decrease the size by the amount of unused memory, which is ; the difference between the user space pointer and the raw memory block ; pointer. Since we have decremented the user space pointer by 256, we will ; have to correct the result. ; ; return size - (ptr1 + 256 - ptr2) ; return size - ptr1 - 256 + ptr2 dex ; - 256 add ptr2 pha txa adc ptr2+1 tax pla sub ptr1 pha txa sbc ptr1+1 tax pla ; Done rts
wagiminator/C64-Collection
9,363
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/malloc.s
; ; Ullrich von Bassewitz, 17.7.2000 ; ; Allocate a block from the heap. ; ; void* __fastcall__ malloc (size_t size); ; ; ; C implementation was: ; ; void* malloc (size_t size) ; /* Allocate memory from the given heap. The function returns a pointer to the ; * allocated memory block or a NULL pointer if not enough memory is available. ; * Allocating a zero size block is not allowed. ; */ ; { ; struct freeblock* f; ; unsigned* p; ; ; ; /* Check for a size of zero, then add the administration space and round ; * up the size if needed. ; */ ; if (size == 0) { ; return 0; ; } ; size += HEAP_ADMIN_SPACE; ; if (size < sizeof (struct freeblock)) { ; size = sizeof (struct freeblock); ; } ; ; /* Search the freelist for a block that is big enough */ ; f = _hfirst; ; while (f && f->size < size) { ; f = f->next; ; } ; ; /* Did we find one? */ ; if (f) { ; ; /* We found a block big enough. If the block can hold just the ; * requested size, use the block in full. Beware: When slicing blocks, ; * there must be space enough to create a new one! If this is not the ; * case, then use the complete block. ; */ ; if (f->size - size < sizeof (struct freeblock)) { ; ; /* Use the actual size */ ; size = f->size; ; ; /* Remove the block from the free list */ ; if (f->prev) { ; /* We have a previous block */ ; f->prev->next = f->next; ; } else { ; /* This is the first block, correct the freelist pointer */ ; _hfirst = f->next; ; } ; if (f->next) { ; /* We have a next block */ ; f->next->prev = f->prev; ; } else { ; /* This is the last block, correct the freelist pointer */ ; _hlast = f->prev; ; } ; ; } else { ; ; /* We must slice the block found. Cut off space from the upper ; * end, so we can leave the actual free block chain intact. ; */ ; ; /* Decrement the size of the block */ ; f->size -= size; ; ; /* Set f to the now unused space above the current block */ ; f = (struct freeblock*) (((unsigned) f) + f->size); ; ; } ; ; /* Setup the pointer for the block */ ; p = (unsigned*) f; ; ; } else { ; ; /* We did not find a block big enough. Try to use new space from the ; * heap top. ; */ ; if (((unsigned) _hend) - ((unsigned) _hptr) < size) { ; /* Out of heap space */ ; return 0; ; } ; ; ; /* There is enough space left, take it from the heap top */ ; p = _hptr; ; _hptr = (unsigned*) (((unsigned) _hptr) + size); ; ; } ; ; /* New block is now in p. Fill in the size and return the user pointer */ ; *p++ = size; ; return p; ; } ; .importzp ptr1, ptr2, ptr3 .export _malloc .include "_heap.inc" .macpack generic ;----------------------------------------------------------------------------- ; Code _malloc: sta ptr1 ; Store size in ptr1 stx ptr1+1 ; Check for a size of zero, if so, return NULL ora ptr1+1 beq Done ; a/x already contains zero ; Add the administration space and round up the size if needed lda ptr1 add #HEAP_ADMIN_SPACE sta ptr1 bcc @L1 inc ptr1+1 @L1: ldx ptr1+1 bne @L2 cmp #HEAP_MIN_BLOCKSIZE+1 bcs @L2 lda #HEAP_MIN_BLOCKSIZE sta ptr1 ; High byte is already zero ; Load a pointer to the freelist into ptr2 @L2: lda __heapfirst sta ptr2 lda __heapfirst+1 sta ptr2+1 ; Search the freelist for a block that is big enough. We will calculate ; (f->size - size) here and keep it, since we need the value later. jmp @L4 @L3: ldy #freeblock::size lda (ptr2),y sub ptr1 tax ; Remember low byte for later iny ; Y points to freeblock::size+1 lda (ptr2),y sbc ptr1+1 bcs BlockFound ; Beware: Contents of a/x/y are known! ; Next block in list iny ; Points to freeblock::next lda (ptr2),y tax iny ; Points to freeblock::next+1 lda (ptr2),y stx ptr2 sta ptr2+1 @L4: ora ptr2 bne @L3 ; We did not find a block big enough. Try to use new space from the heap top. lda __heapptr add ptr1 ; _heapptr + size tay lda __heapptr+1 adc ptr1+1 bcs OutOfHeapSpace ; On overflow, we're surely out of space cmp __heapend+1 bne @L5 cpy __heapend @L5: bcc TakeFromTop beq TakeFromTop ; Out of heap space OutOfHeapSpace: lda #0 tax Done: rts ; There is enough space left, take it from the heap top TakeFromTop: ldx __heapptr ; p = _heapptr; stx ptr2 ldx __heapptr+1 stx ptr2+1 sty __heapptr ; _heapptr += size; sta __heapptr+1 jmp FillSizeAndRet ; Done ; We found a block big enough. If the block can hold just the ; requested size, use the block in full. Beware: When slicing blocks, ; there must be space enough to create a new one! If this is not the ; case, then use the complete block. ; On input, x/a do contain the remaining size of the block. The zero ; flag is set if the high byte of this remaining size is zero. BlockFound: bne SliceBlock ; Block is large enough to slice cpx #HEAP_MIN_BLOCKSIZE ; Check low byte bcs SliceBlock ; Jump if block is large enough to slice ; The block is too small to slice it. Use the block in full. The block ; does already contain the correct size word, all we have to do is to ; remove it from the free list. ldy #freeblock::prev+1 ; Load f->prev lda (ptr2),y sta ptr3+1 dey lda (ptr2),y sta ptr3 dey ; Points to freeblock::next+1 ora ptr3+1 beq @L1 ; Jump if f->prev zero ; We have a previous block, ptr3 contains its address. ; Do f->prev->next = f->next lda (ptr2),y ; Load high byte of f->next sta (ptr3),y ; Store high byte of f->prev->next dey ; Points to next lda (ptr2),y ; Load low byte of f->next sta (ptr3),y ; Store low byte of f->prev->next jmp @L2 ; This is the first block, correct the freelist pointer ; Do _hfirst = f->next @L1: lda (ptr2),y ; Load high byte of f->next sta __heapfirst+1 dey ; Points to next lda (ptr2),y ; Load low byte of f->next sta __heapfirst ; Check f->next. Y points always to next if we come here @L2: lda (ptr2),y ; Load low byte of f->next sta ptr3 iny ; Points to next+1 lda (ptr2),y ; Load high byte of f->next sta ptr3+1 iny ; Points to prev ora ptr3 beq @L3 ; Jump if f->next zero ; We have a next block, ptr3 contains its address. ; Do f->next->prev = f->prev lda (ptr2),y ; Load low byte of f->prev sta (ptr3),y ; Store low byte of f->next->prev iny ; Points to prev+1 lda (ptr2),y ; Load high byte of f->prev sta (ptr3),y ; Store high byte of f->prev->next jmp RetUserPtr ; Done ; This is the last block, correct the freelist pointer. ; Do _hlast = f->prev @L3: lda (ptr2),y ; Load low byte of f->prev sta __heaplast iny ; Points to prev+1 lda (ptr2),y ; Load high byte of f->prev sta __heaplast+1 jmp RetUserPtr ; Done ; We must slice the block found. Cut off space from the upper end, so we ; can leave the actual free block chain intact. SliceBlock: ; Decrement the size of the block. Y points to size+1. dey ; Points to size lda (ptr2),y ; Low byte of f->size sub ptr1 sta (ptr2),y tax ; Save low byte of f->size in X iny ; Points to size+1 lda (ptr2),y ; High byte of f->size sbc ptr1+1 sta (ptr2),y ; Set f to the space above the current block, which is the new block returned ; to the caller. txa ; Get low byte of f->size add ptr2 tax lda (ptr2),y ; Get high byte of f->size adc ptr2+1 stx ptr2 sta ptr2+1 ; Fill the size and start address into the admin space of the block ; (struct usedblock) and return the user pointer FillSizeAndRet: ldy #usedblock::size ; p->size = size; lda ptr1 ; Low byte of block size sta (ptr2),y iny ; Points to freeblock::size+1 lda ptr1+1 sta (ptr2),y RetUserPtr: ldy #usedblock::start ; p->start = p lda ptr2 sta (ptr2),y iny lda ptr2+1 sta (ptr2),y ; Return the user pointer, which points behind the struct usedblock lda ptr2 ; return ++p; ldx ptr2+1 add #HEAP_ADMIN_SPACE bcc @L9 inx @L9: rts
wagiminator/C64-Collection
4,503
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/putenv.s
; ; Ullrich von Bassewitz, 2005-04-21 ; ; int putenv (char* s); ; ; Note: The function will place s into the environment, *not* a copy! ; .export _putenv .import _malloc, _free .import searchenv, copyenvptr .import __environ, __envcount, __envsize .import __seterrno, return0 .import ptr1:zp, ptr2:zp, ptr3:zp, tmp1:zp .include "errno.inc" .code ;---------------------------------------------------------------------------- ; putenv() .proc _putenv sta ptr1 sta name stx ptr1+1 ; Save name stx name+1 ; Loop over the name to find the '='. If there is no '=', set errno to EINVAL ; and return an error. ldy #$FF @L0: iny lda (ptr1),y bne @L1 lda #EINVAL jmp error ; End of string without '=' found @L1: cmp #'=' bne @L0 ; Remember the offset of the equal sign and replace it by a zero. sty tmp1 lda #$00 sta (ptr1),y ; Search for the string in the environment. searchenv will set the N flag if ; the string is not found, otherwise X contains the index of the entry, ptr2 ; contains the entry and Y the offset of the '=' in the string. ptr3 will ; point to the environment. jsr searchenv ; Before doing anything else, restore the old environment string. ldy tmp1 lda #'=' sta (ptr1),y ; Check the result of searchenv txa ; Did we find the entry? bpl addentry ; Jump if yes ; We didn't find the entry, so we have to add a new one. Before doing so, we ; must check if the size of the _environ array must be increased. ; Note: There must be one additional slot for the final NULL entry. ldx __envcount inx cpx __envsize bcc addnewentry ; Jump if space enough ; We need to increase the size of the environ array. Calculate the new size. ; We will not support a size larger than 64 entries, double the size with ; each overflow, and the starting size is 8 entries. lda __envsize bne @L2 lda #4 ; Start with 4*2 entries @L2: asl a ; Double current size bmi nomem ; Bail out if > 64 sta newsize ; Remember the new size ; Call malloc() and store the result in ptr2 asl a ; Make words ldx #$00 jsr _malloc sta ptr2 stx ptr2+1 ; Check the result of malloc ora ptr2+1 beq nomem ; Copy the old environment pointer to ptr3, and the new one to __environ. ldx #1 @L3: lda __environ,x sta ptr3,x lda ptr2,x sta __environ,x dex bpl @L3 ; Use the new size. lda newsize sta __envsize ; Copy the old environment data into the new space. lda __envcount asl a tay jmp @L5 @L4: lda (ptr3),y sta (ptr2),y @L5: dey bpl @L4 ; Free the old environment space lda ptr3 ldx ptr3+1 jsr _free ; Since free() has destroyed ptr2, we need another copy ... jsr copyenvptr ; Copy __environ to ptr2 ; Bump the environment count and remember it in X. Add the final NULL entry. addnewentry: inc __envcount ldx __envcount txa asl a tay lda #$00 sta (ptr2),y iny sta (ptr2),y ; The index of the new entry is the old environment count. dex txa ; Add the new entry to the slot with index in X. The pointer to the environment ; is already in ptr2, either by a call to searchenv, or by above code. addentry: asl a tay lda name sta (ptr2),y iny lda name+1 sta (ptr2),y ; Done jmp return0 ; Error entries nomem: lda #ENOMEM error: jsr __seterrno lda #$FF ; Return -1 tax rts .endproc ;---------------------------------------------------------------------------- ; data .bss name: .addr 0 ; Pointer to name newsize: .byte 0 ; New environment size
wagiminator/C64-Collection
1,138
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/strncat.s
; ; Ullrich von Bassewitz, 31.05.1998 ; ; char* strncat (char* dest, const char* src, size_t n); ; .export _strncat .import popax .importzp ptr1, ptr2, ptr3, tmp1, tmp2 _strncat: eor #$FF ; one's complement to count upwards sta tmp1 txa eor #$FF sta tmp2 jsr popax ; get src sta ptr1 stx ptr1+1 jsr popax ; get dest sta ptr2 stx ptr2+1 sta ptr3 ; remember for function return stx ptr3+1 ldy #0 ; find end of dest L1: lda (ptr2),y beq L2 iny bne L1 inc ptr2+1 bne L1 ; end found, get offset in y into pointer L2: tya clc adc ptr2 sta ptr2 bcc L3 inc ptr2+1 ; copy src. We've put the ones complement of the count into the counter, so ; we'll increment the counter on top of the loop L3: ldy #0 ldx tmp1 ; low counter byte L4: inx bne L5 inc tmp2 beq L6 ; jump if done L5: lda (ptr1),y sta (ptr2),y beq L7 iny bne L4 inc ptr1+1 inc ptr2+1 bne L4 ; done, set the trailing zero and return pointer to dest L6: lda #0 sta (ptr2),y L7: lda ptr3 ldx ptr3+1 rts
wagiminator/C64-Collection
2,049
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/itoa.s
; ; Ullrich von Bassewitz, 31.05.1998 ; ; char* itoa (int value, char* s, int radix); ; char* utoa (unsigned value, char* s, int radix); ; .export _itoa, _utoa .import addysp1 .import __hextab .importzp sp, sreg, ptr2, ptr3, tmp1 .rodata specval: .byte '-', '3', '2', '7', '6', '8', 0 .code ; ; Common subroutine to pop the parameters and put them into core ; dopop: sta tmp1 ; will loose high byte ldy #0 lda (sp),y sta ptr2 sta ptr3 iny lda (sp),y sta ptr2+1 sta ptr3+1 iny lda (sp),y sta sreg iny lda (sp),y sta sreg+1 jmp addysp1 ; Bump stack pointer ; ; itoa ; _itoa: jsr dopop ; pop the arguments ; We must handle $8000 in a special way, since it is the only negative ; number that has no positive 16-bit counterpart ldy tmp1 ; get radix cpy #10 bne utoa cmp #$00 bne L2 cpx #$80 bne L2 ldy #6 L1: lda specval,y ; copy -32768 sta (ptr2),y dey bpl L1 jmp L10 ; Check if the value is negative. If so, write a - sign and negate the ; number. L2: lda sreg+1 ; get high byte bpl utoa lda #'-' ldy #0 sta (ptr2),y ; store sign inc ptr2 bne L3 inc ptr2+1 L3: lda sreg eor #$FF clc adc #$01 sta sreg lda sreg+1 eor #$FF adc #$00 sta sreg+1 jmp utoa ; ; utoa ; _utoa: jsr dopop ; pop the arguments ; Convert to string by dividing and push the result onto the stack utoa: lda #$00 pha ; sentinel ; Divide sreg/tmp1 -> sreg, remainder in a L5: ldy #16 ; 16 bit lda #0 ; remainder L6: asl sreg rol sreg+1 rol a cmp tmp1 bcc L7 sbc tmp1 inc sreg L7: dey bne L6 tay ; get remainder into y lda __hextab,y ; get hex character pha ; save char value on stack lda sreg ora sreg+1 bne L5 ; Get the characters from the stack into the string ldy #0 L9: pla sta (ptr2),y beq L10 ; jump if sentinel iny bne L9 ; jump always ; Done! Return the target string L10: lda ptr3 ldx ptr3+1 rts
wagiminator/C64-Collection
2,476
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/ltoa.s
; ; Ullrich von Bassewitz, 11.06.1998 ; ; char* ltoa (long value, char* s, int radix); ; char* ultoa (unsigned long value, char* s, int radix); ; .export _ltoa, _ultoa .import popax .import __hextab .importzp sreg, ptr1, ptr2, ptr3, tmp1 .rodata specval: .byte '-', '2', '1', '4', '7', '4', '8', '3', '6', '4', '8', 0 .code ; ; Common subroutine to pop the parameters and put them into core ; dopop: sta tmp1 ; will loose high byte jsr popax ; get s sta ptr1 stx ptr1+1 sta sreg ; save for return stx sreg+1 jsr popax ; get low word of value sta ptr2 stx ptr2+1 jsr popax ; get high word of value sta ptr3 stx ptr3+1 rts ; ; ltoa ; _ltoa: jsr dopop ; pop the arguments ; We must handle $80000000 in a special way, since it is the only negative ; number that has no positive 32-bit counterpart ldx ptr3+1 ; get high byte ldy tmp1 ; get radix cpy #10 bne ultoa lda ptr3 ora ptr2+1 ora ptr2 bne L2 cpx #$80 bne L2 ldy #11 L1: lda specval,y ; copy -2147483648 sta (ptr1),y dey bpl L1 jmp L10 ; Check if the value is negative. If so, write a - sign and negate the ; number. L2: txa ; get high byte bpl ultoa lda #'-' ldy #0 sta (ptr1),y ; store sign inc ptr1 bne L3 inc ptr1+1 L3: lda ptr2 ; negate val eor #$FF clc adc #$01 sta ptr2 lda ptr2+1 eor #$FF adc #$00 sta ptr2+1 lda ptr3 eor #$FF adc #$00 sta ptr3 lda ptr3+1 eor #$FF adc #$00 sta ptr3+1 jmp ultoa ; ; utoa ; _ultoa: jsr dopop ; pop the arguments ; Convert to string by dividing and push the result onto the stack ultoa: lda #$00 pha ; sentinel ; Divide val/tmp1 -> val, remainder in a L5: ldy #32 ; 32 bit lda #0 ; remainder L6: asl ptr2 rol ptr2+1 rol ptr3 rol ptr3+1 rol a cmp tmp1 bcc L7 sbc tmp1 inc ptr2 L7: dey bne L6 tay ; get remainder into y lda __hextab,y ; get hex character pha ; save char value on stack lda ptr2 ora ptr2+1 ora ptr3 ora ptr3+1 bne L5 ; Get the characters from the stack into the string ldy #0 L9: pla sta (ptr1),y beq L10 ; jump if sentinel iny bne L9 ; jump always ; Done! Return the target string L10: lda sreg ldx sreg+1 rts
wagiminator/C64-Collection
13,983
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/free.s
; ; Ullrich von Bassewitz, 19.03.2000 ; ; Free a block on the heap. ; ; void __fastcall__ free (void* block); ; ; ; C implementation was: ; ; void free (void* block) ; /* Release an allocated memory block. The function will accept NULL pointers ; * (and do nothing in this case). ; */ ; { ; unsigned* b; ; unsigned size; ; struct freeblock* f; ; ; ; /* Allow NULL arguments */ ; if (block == 0) { ; return; ; } ; ; /* Get a pointer to the real memory block, then get the size */ ; b = (unsigned*) block; ; size = *--b; ; ; /* Check if the block is at the top of the heap */ ; if (((int) b) + size == (int) _hptr) { ; ; /* Decrease _hptr to release the block */ ; _hptr = (unsigned*) (((int) _hptr) - size); ; ; /* Check if the last block in the freelist is now at heap top. If so, ; * remove this block from the freelist. ; */ ; if (f = _hlast) { ; if (((int) f) + f->size == (int) _hptr) { ; /* Remove the last block */ ; _hptr = (unsigned*) (((int) _hptr) - f->size); ; if (_hlast = f->prev) { ; /* Block before is now last block */ ; f->prev->next = 0; ; } else { ; /* The freelist is empty now */ ; _hfirst = 0; ; } ; } ; } ; ; } else { ; ; /* Not at heap top, enter the block into the free list */ ; _hadd (b, size); ; ; } ; } ; .importzp ptr1, ptr2, ptr3, ptr4 .export _free, heapadd .include "_heap.inc" .macpack generic ;----------------------------------------------------------------------------- ; Code _free: sta ptr2 stx ptr2+1 ; Save block ; Is the argument NULL? If so, bail out. ora ptr2+1 ; Is the argument NULL? bne @L1 ; Jump if no rts ; Bail out if yes ; There's a pointer below the user space that points to the real start of the ; raw block. We will decrement the high pointer byte and use an offset of 254 ; to save some code. The first word of the raw block is the total size of the ; block. Remember the block size in ptr1. @L1: dec ptr2+1 ; Decrement high pointer byte ldy #$FF lda (ptr2),y ; High byte of real block address tax dey lda (ptr2),y stx ptr2+1 sta ptr2 ; Set ptr2 to start of real block ldy #usedblock::size+1 lda (ptr2),y ; High byte of size sta ptr1+1 ; Save it dey lda (ptr2),y sta ptr1 ; Check if the block is on top of the heap add ptr2 tay lda ptr2+1 adc ptr1+1 cpy __heapptr bne heapadd ; Add to free list cmp __heapptr+1 bne heapadd ; The pointer is located at the heap top. Lower the heap top pointer to ; release the block. @L3: lda ptr2 sta __heapptr lda ptr2+1 sta __heapptr+1 ; Check if the last block in the freelist is now at heap top. If so, remove ; this block from the freelist. lda __heaplast sta ptr1 ora __heaplast+1 beq @L9 ; Jump if free list empty lda __heaplast+1 sta ptr1+1 ; Pointer to last block now in ptr1 ldy #freeblock::size lda (ptr1),y ; Low byte of block size add ptr1 tax iny ; High byte of block size lda (ptr1),y adc ptr1+1 cmp __heapptr+1 bne @L9 ; Jump if last block not on top of heap cpx __heapptr bne @L9 ; Jump if last block not on top of heap ; Remove the last block lda ptr1 sta __heapptr lda ptr1+1 sta __heapptr+1 ; Correct the next pointer of the now last block ldy #freeblock::prev+1 ; Offset of ->prev field lda (ptr1),y sta ptr2+1 ; Remember f->prev in ptr2 sta __heaplast+1 dey lda (ptr1),y sta ptr2 ; Remember f->prev in ptr2 sta __heaplast ora __heaplast+1 ; -> prev == 0? bne @L8 ; Jump if free list not empty ; Free list is now empty (A = 0) sta __heapfirst sta __heapfirst+1 ; Done @L9: rts ; Block before is now last block. ptr2 points to f->prev. @L8: lda #$00 dey ; Points to high byte of ->next sta (ptr2),y dey ; Low byte of f->prev->next sta (ptr2),y rts ; Done ; The block is not on top of the heap. Add it to the free list. This was ; formerly a separate function called __hadd that was implemented in C as ; shown here: ; ; void _hadd (void* mem, size_t size) ; /* Add an arbitrary memory block to the heap. This function is used by ; * free(), but it does also allow usage of otherwise unused memory ; * blocks as heap space. The given block is entered in the free list ; * without any checks, so beware! ; */ ; { ; struct freeblock* f; ; struct freeblock* left; ; struct freeblock* right; ; ; if (size >= sizeof (struct freeblock)) { ; ; /* Set the admin data */ ; f = (struct freeblock*) mem; ; f->size = size; ; ; /* Check if the freelist is empty */ ; if (_hfirst == 0) { ; ; /* The freelist is empty until now, insert the block */ ; f->prev = 0; ; f->next = 0; ; _hfirst = f; ; _hlast = f; ; ; } else { ; ; /* We have to search the free list. As we are doing so, we check ; * if it is possible to combine this block with another already ; * existing block. Beware: The block may be the "missing link" ; * between *two* other blocks. ; */ ; left = 0; ; right = _hfirst; ; while (right && f > right) { ; left = right; ; right = right->next; ; } ; ; ; /* Ok, the current block must be inserted between left and right (but ; * beware: one of the two may be zero!). Also check for the condition ; * that we have to merge two or three blocks. ; */ ; if (right) { ; /* Check if we must merge the block with the right one */ ; if (((unsigned) f) + size == (unsigned) right) { ; /* Merge with the right block */ ; f->size += right->size; ; if (f->next = right->next) { ; f->next->prev = f; ; } else { ; /* This is now the last block */ ; _hlast = f; ; } ; } else { ; /* No merge, just set the link */ ; f->next = right; ; right->prev = f; ; } ; } else { ; f->next = 0; ; /* Special case: This is the new freelist end */ ; _hlast = f; ; } ; if (left) { ; /* Check if we must merge the block with the left one */ ; if ((unsigned) f == ((unsigned) left) + left->size) { ; /* Merge with the left block */ ; left->size += f->size; ; if (left->next = f->next) { ; left->next->prev = left; ; } else { ; /* This is now the last block */ ; _hlast = left; ; } ; } else { ; /* No merge, just set the link */ ; left->next = f; ; f->prev = left; ; } ; } else { ; f->prev = 0; ; /* Special case: This is the new freelist start */ ; _hfirst = f; ; } ; } ; } ; } ; ; ; On entry, ptr2 must contain a pointer to the block, which must be at least ; HEAP_MIN_BLOCKSIZE bytes in size, and ptr1 contains the total size of the ; block. ; ; Check if the free list is empty, storing _hfirst into ptr3 for later heapadd: lda __heapfirst sta ptr3 lda __heapfirst+1 sta ptr3+1 ora ptr3 bne SearchFreeList ; The free list is empty, so this is the first and only block. A contains ; zero if we come here. ldy #freeblock::next-1 @L2: iny ; f->next = f->prev = 0; sta (ptr2),y cpy #freeblock::prev+1 ; Done? bne @L2 lda ptr2 ldx ptr2+1 sta __heapfirst stx __heapfirst+1 ; _heapfirst = f; sta __heaplast stx __heaplast+1 ; _heaplast = f; rts ; Done ; We have to search the free list. As we are doing so, check if it is possible ; to combine this block with another, already existing block. Beware: The ; block may be the "missing link" between two blocks. ; ptr3 contains _hfirst (the start value of the search) when execution reaches ; this point, Y contains size+1. We do also know that _heapfirst (and therefore ; ptr3) is not zero on entry. SearchFreeList: lda #0 sta ptr4 sta ptr4+1 ; left = 0; ldy #freeblock::next+1 ldx ptr3 @Loop: lda ptr3+1 ; High byte of right cmp ptr2+1 bne @L1 cpx ptr2 beq @L2 @L1: bcs CheckRightMerge @L2: stx ptr4 ; left = right; sta ptr4+1 dey ; Points to next lda (ptr3),y ; right = right->next; tax iny ; Points to next+1 lda (ptr3),y stx ptr3 sta ptr3+1 ora ptr3 bne @Loop ; If we come here, the right pointer is zero, so we don't need to check for ; a merge. The new block is the new freelist end. ; A is zero when we come here, Y points to next+1 sta (ptr2),y ; Clear high byte of f->next dey sta (ptr2),y ; Clear low byte of f->next lda ptr2 ; _heaplast = f; sta __heaplast lda ptr2+1 sta __heaplast+1 ; Since we have checked the case that the freelist is empty before, if the ; right pointer is NULL, the left *cannot* be NULL here. So skip the ; pointer check and jump right to the left block merge jmp CheckLeftMerge2 ; The given block must be inserted between left and right, and right is not ; zero. CheckRightMerge: lda ptr2 add ptr1 ; f + size tax lda ptr2+1 adc ptr1+1 cpx ptr3 bne NoRightMerge cmp ptr3+1 bne NoRightMerge ; Merge with the right block. Do f->size += right->size; ldy #freeblock::size lda ptr1 add (ptr3),y sta (ptr2),y iny ; Points to size+1 lda ptr1+1 adc (ptr3),y sta (ptr2),y ; Set f->next = right->next and remember f->next in ptr1 (we don't need the ; size stored there any longer) iny ; Points to next lda (ptr3),y ; Low byte of right->next sta (ptr2),y ; Store to low byte of f->next sta ptr1 iny ; Points to next+1 lda (ptr3),y ; High byte of right->next sta (ptr2),y ; Store to high byte of f->next sta ptr1+1 ora ptr1 beq @L1 ; Jump if f->next zero ; f->next->prev = f; iny ; Points to prev lda ptr2 ; Low byte of f sta (ptr1),y ; Low byte of f->next->prev iny ; Points to prev+1 lda ptr2+1 ; High byte of f sta (ptr1),y ; High byte of f->next->prev jmp CheckLeftMerge ; Done ; f->next is zero, this is now the last block @L1: lda ptr2 ; _heaplast = f; sta __heaplast lda ptr2+1 sta __heaplast+1 jmp CheckLeftMerge ; No right merge, just set the link. NoRightMerge: ldy #freeblock::next ; f->next = right; lda ptr3 sta (ptr2),y iny ; Points to next+1 lda ptr3+1 sta (ptr2),y iny ; Points to prev lda ptr2 ; right->prev = f; sta (ptr3),y iny ; Points to prev+1 lda ptr2+1 sta (ptr3),y ; Check if the left pointer is zero CheckLeftMerge: lda ptr4 ; left == NULL? ora ptr4+1 bne CheckLeftMerge2 ; Jump if there is a left block ; We don't have a left block, so f is actually the new freelist start ldy #freeblock::prev sta (ptr2),y ; f->prev = 0; iny sta (ptr2),y lda ptr2 ; _heapfirst = f; sta __heapfirst lda ptr2+1 sta __heapfirst+1 rts ; Done ; Check if the left block is adjacent to the following one CheckLeftMerge2: ldy #freeblock::size ; Calculate left + left->size lda (ptr4),y ; Low byte of left->size add ptr4 tax iny ; Points to size+1 lda (ptr4),y ; High byte of left->size adc ptr4+1 cpx ptr2 bne NoLeftMerge cmp ptr2+1 bne NoLeftMerge ; Jump if blocks not adjacent ; Merge with the left block. Do left->size += f->size; dey ; Points to size lda (ptr4),y add (ptr2),y sta (ptr4),y iny ; Points to size+1 lda (ptr4),y adc (ptr2),y sta (ptr4),y ; Set left->next = f->next and remember left->next in ptr1. iny ; Points to next lda (ptr2),y ; Low byte of f->next sta (ptr4),y sta ptr1 iny ; Points to next+1 lda (ptr2),y ; High byte of f->next sta (ptr4),y sta ptr1+1 ora ptr1 ; left->next == NULL? beq @L1 ; Do left->next->prev = left iny ; Points to prev lda ptr4 ; Low byte of left sta (ptr1),y iny lda ptr4+1 ; High byte of left sta (ptr1),y rts ; Done ; This is now the last block, do _heaplast = left @L1: lda ptr4 sta __heaplast lda ptr4+1 sta __heaplast+1 rts ; Done ; No merge of the left block, just set the link. Y points to size+1 if ; we come here. Do left->next = f. NoLeftMerge: iny ; Points to next lda ptr2 ; Low byte of left sta (ptr4),y iny lda ptr2+1 ; High byte of left sta (ptr4),y ; Do f->prev = left iny ; Points to prev lda ptr4 sta (ptr2),y iny lda ptr4+1 sta (ptr2),y rts ; Done
wagiminator/C64-Collection
3,263
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/vfprintf.s
; vfprintf.s ; ; int fastcall vfprintf(FILE* f, const char* Format, va_list ap); ; ; 2005-02-08, Ullrich von Bassewitz ; 2005-02-11, Greg King .export _vfprintf .import push1, pushwysp, incsp6 .import _fwrite, __printf .importzp sp, ptr1 .macpack generic .data ; ---------------------------------------------------------------------------- ; Static data for the _vfprintf routine ; outdesc: ; Static outdesc structure ccount: .res 2 .word out ; Output function pointer ptr: .res 2 ; Points to output file .res 2 ; (Not used by this function) .code ; ---------------------------------------------------------------------------- ; Callback routine used for the actual output. ; ; Since we know, that this routine is always called with "our" outdesc, we ; can ignore the passed pointer d, and access the data directly. While this ; is not very clean, it gives better and shorter code. ; ; static void out (struct outdesc* d, const char* buf, unsigned count) ; /* Routine used for writing */ ; { ; register size_t cnt; ; ; /* Write to the file */ ; if ((cnt = fwrite(buf, 1, count, ptr)) == 0) { ; ccount = -1; ; } else { ; ccount += cnt; ; } ; } ; About to call ; ; fwrite (buf, 1, count, ptr); ; out: ldy #5 jsr pushwysp ; Push buf jsr push1 ; Push #1 ldy #7 jsr pushwysp ; Push count lda ptr ldx ptr+1 jsr _fwrite sta ptr1 ; Save function result stx ptr1+1 ; Check the return value. ora ptr1+1 bne @Ok ; We had an error. Store -1 into ccount .ifp02 lda #<-1 .else dec a .endif sta ccount bne @Done ; Branch always ; Result was ok, count bytes written @Ok: lda ptr1 add ccount sta ccount txa adc ccount+1 @Done: sta ccount+1 jmp incsp6 ; Drop stackframe ; ---------------------------------------------------------------------------- ; vfprintf - formatted output ; ; int fastcall vfprintf(FILE* f, const char* format, va_list ap) ; { ; static struct outdesc d = { ; 0, ; out ; }; ; ; /* Setup descriptor */ ; d.ccount = 0; ; d.ptr = f; ; ; /* Do formatting and output */ ; _printf (&d, format, ap); ; ; /* Return bytes written */ ; return d.ccount; ; } ; _vfprintf: pha ; Save low byte of ap ; Setup the outdesc structure lda #0 sta ccount sta ccount+1 ; Clear character-count ; Reorder the stack. Replace f on the stack by &d, so the stack frame is ; exactly as _printf expects it. Parameters will get dropped by _printf. ldy #2 lda (sp),y ; Low byte of f sta ptr lda #<outdesc sta (sp),y iny lda (sp),y ; High byte of f sta ptr+1 lda #>outdesc sta (sp),y ; Restore low byte of ap and call _printf pla jsr __printf ; Return the number of bytes written lda ccount ldx ccount+1 rts
wagiminator/C64-Collection
2,448
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/memset.s
; ; void* __fastcall__ memset (void* ptr, int c, size_t n); ; void* __fastcall__ _bzero (void* ptr, size_t n); ; void __fastcall__ bzero (void* ptr, size_t n); ; ; Ullrich von Bassewitz, 29.05.1998 ; Performance increase (about 20%) by ; Christian Krueger, 12.09.2009 ; ; NOTE: bzero will return it's first argument as memset does. It is no problem ; to declare the return value as void, since it may be ignored. _bzero ; (note the leading underscore) is declared with the proper return type, ; because the compiler will replace memset by _bzero if the fill value ; is zero, and the optimizer looks at the return type to see if the value ; in a/x is of any use. ; .export _memset, _bzero, __bzero .import popax .importzp sp, ptr1, ptr2, ptr3 _bzero: __bzero: sta ptr3 stx ptr3+1 ; Save n ldx #0 ; Fill with zeros beq common _memset: sta ptr3 ; Save n stx ptr3+1 jsr popax ; Get c tax ; Common stuff for memset and bzero from here common: ; Fill value is in X! ldy #1 lda (sp),y sta ptr1+1 ; save high byte of ptr dey ; Y = 0 lda (sp),y ; Get ptr sta ptr1 lsr ptr3+1 ; divide number of ror ptr3 ; bytes by two to increase bcc evenCount ; speed (ptr3 = ptr3/2) oddCount: ; y is still 0 here txa ; restore fill value sta (ptr1),y ; save value and increase inc ptr1 ; dest. pointer bne evenCount inc ptr1+1 evenCount: lda ptr1 ; build second pointer section clc adc ptr3 ; ptr2 = ptr1 + (length/2) <- ptr3 sta ptr2 lda ptr1+1 adc ptr3+1 sta ptr2+1 txa ; restore fill value ldx ptr3+1 ; Get high byte of n beq L2 ; Jump if zero ; Set 256/512 byte blocks ; y is still 0 here L1: .repeat 2 ; Unroll this a bit to make it faster sta (ptr1),y ; Set byte in lower section sta (ptr2),y ; Set byte in upper section iny .endrepeat bne L1 inc ptr1+1 inc ptr2+1 dex ; Next 256 byte block bne L1 ; Repeat if any ; Set the remaining bytes if any L2: ldy ptr3 ; Get the low byte of n bne L3 ; something to set? jmp popax ; no -> Pop ptr and return as result L3a: sta (ptr1),y ; set bytes in low sta (ptr2),y ; and high section L3: dey bne L3a sta (ptr1),y ; Set remaining byte(s) sta (ptr2),y jmp popax ; Pop ptr and return as result
wagiminator/C64-Collection
1,590
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/strncmp.s
; ; Ullrich von Bassewitz, 25.05.2000 ; ; int strncmp (const char* s1, const char* s2, unsigned n); ; .export _strncmp .import popax .importzp ptr1, ptr2, ptr3 _strncmp: ; Convert the given counter value in a/x from a downward counter into an ; upward counter, so we can increment the counter in the loop below instead ; of decrementing it. This adds some overhead now, but is cheaper than ; executing a more complex test in each iteration of the loop. We do also ; correct the value by one, so we can do the test on top of the loop. eor #$FF sta ptr3 txa eor #$FF sta ptr3+1 ; Get the remaining arguments jsr popax ; get s2 sta ptr2 stx ptr2+1 jsr popax ; get s1 sta ptr1 stx ptr1+1 ; Loop setup ldy #0 ; Start of compare loop. Check the counter. Loop: inc ptr3 beq IncHi ; Increment high byte ; Compare a byte from the strings Comp: lda (ptr1),y cmp (ptr2),y bne NotEqual ; Jump if strings different tax ; End of strings? beq Equal1 ; Jump if EOS reached, a/x == 0 ; Increment the pointers iny bne Loop inc ptr1+1 inc ptr2+1 bne Loop ; Branch always ; Increment hi byte IncHi: inc ptr3+1 bne Comp ; Jump if counter not zero ; Exit code if strings are equal. a/x not set Equal: lda #$00 tax Equal1: rts ; Exit code if strings not equal NotEqual: bcs L1 ldx #$FF ; Make result negative rts L1: ldx #$01 ; Make result positive rts
wagiminator/C64-Collection
1,347
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/memcmp.s
; ; Ullrich von Bassewitz, 15.09.2000 ; ; int memcmp (const void* p1, const void* p2, size_t count); ; .export _memcmp .import popax, return0 .importzp ptr1, ptr2, ptr3 _memcmp: ; Calculate (-count-1) and store it into ptr3. This is some overhead here but ; saves time in the compare loop eor #$FF sta ptr3 txa eor #$FF sta ptr3+1 ; Get the pointer parameters jsr popax ; Get p2 sta ptr2 stx ptr2+1 jsr popax ; Get p1 sta ptr1 stx ptr1+1 ; Loop initialization ldx ptr3 ; Load low counter byte into X ldy #$00 ; Initialize pointer ; Head of compare loop: Test for the end condition Loop: inx ; Bump low byte of (-count-1) beq BumpHiCnt ; Jump on overflow ; Do the compare Comp: lda (ptr1),y cmp (ptr2),y bne NotEqual ; Jump if bytes not equal ; Bump the pointers iny ; Increment pointer bne Loop inc ptr1+1 ; Increment high bytes inc ptr2+1 bne Loop ; Branch always (pointer wrap is illegal) ; Entry on low counter byte overflow BumpHiCnt: inc ptr3+1 ; Bump high byte of (-count-1) bne Comp ; Jump if not done jmp return0 ; Count is zero, areas are identical ; Not equal, check which one is greater NotEqual: bcs Greater ldx #$FF ; Make result negative rts Greater: ldx #$01 ; Make result positive rts
wagiminator/C64-Collection
1,402
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/sprintf.s
; ; int sprintf (char* buf, const char* Format, ...); ; ; Ullrich von Bassewitz, 1.12.2000 ; .export _sprintf .import pushax, addysp, decsp4, _vsprintf .importzp sp, ptr1 .macpack generic ; ---------------------------------------------------------------------------- ; Data .bss ParamSize: .res 1 ; Number of parameter bytes ; ---------------------------------------------------------------------------- ; Code .code _sprintf: sty ParamSize ; Number of param bytes passed in Y ; We have to push buf and format, both in the order they already have on stack. ; To make this somewhat more efficient, we will create space on the stack and ; then do a copy of the complete block instead of pushing each parameter ; separately. Since the size of the arguments passed is the same as the size ; of the fixed arguments, this will allow us to calculate the pointer to the ; fixed size arguments easier (they're just ParamSize bytes away). jsr decsp4 ; Calculate a pointer to the Format argument lda ParamSize add sp sta ptr1 ldx sp+1 bcc @L1 inx @L1: stx ptr1+1 ; Now copy both, buf and format ldy #4-1 @L2: lda (ptr1),y sta (sp),y dey bpl @L2 ; Load va_list (last and __fastcall__ parameter to vsprintf) lda ptr1 ldx ptr1+1 ; Call vsprintf jsr _vsprintf ; Cleanup the stack. We will return what we got from vsprintf ldy ParamSize jmp addysp
wagiminator/C64-Collection
1,463
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/printf.s
; ; int printf (const char* Format, ...); ; ; Ullrich von Bassewitz, 1.12.2000 ; .export _printf .import _stdout, pushax, addysp, _vfprintf .importzp sp, ptr1 .macpack generic ; ---------------------------------------------------------------------------- ; Data .bss ParamSize: .res 1 ; Number of parameter bytes ; ---------------------------------------------------------------------------- ; Code .code _printf: sty ParamSize ; Number of param bytes passed in Y ; We are using a (hopefully) clever trick here to reduce code size. On entry, ; the stack pointer points to the last pushed parameter of the variable ; parameter list. Adding the number of parameter bytes, would result in a ; pointer that points *after* the Format parameter. ; Since we have to push stdout anyway, we will do that here, so ; ; * we will save the subtraction of 2 (__fixargs__) later ; * we will have the address of the Format parameter which needs to ; be pushed next. ; lda _stdout ldx _stdout+1 jsr pushax ; Now calculate the va_list pointer, which does points to Format lda sp ldx sp+1 add ParamSize bcc @L1 inx @L1: sta ptr1 stx ptr1+1 ; Push Format ldy #1 lda (ptr1),y tax dey lda (ptr1),y jsr pushax ; Load va_list (last and __fastcall__ parameter to vfprintf) lda ptr1 ldx ptr1+1 ; Call vfprintf jsr _vfprintf ; Cleanup the stack. We will return what we got from vfprintf ldy ParamSize jmp addysp
wagiminator/C64-Collection
4,324
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/vsnprintf.s
; ; int vsnprintf (char* Buf, size_t size, const char* Format, va_list ap); ; ; Ullrich von Bassewitz, 2009-09-26 ; .export _vsnprintf, vsnprintf .import ldaxysp, popax, incsp2, incsp6 .import _memcpy, __printf .importzp sp, ptr1 .macpack generic .data ; ---------------------------------------------------------------------------- ; ; Static data for the _vsnprintf routine ; outdesc: ; Static outdesc structure ccount: .word 0 ; ccount func: .word out ; Output function pointer bufptr: .word 0 ; ptr bufsize:.word 0 ; Buffer size .code ; ---------------------------------------------------------------------------- ; vsprintf - formatted output into a buffer ; ; int __fastcall__ vsnprintf (char* buf, size_t size, const char* format, va_list ap); ; _vsnprintf: pha ; Save ap txa pha ; Setup the outdesc structure. This is also an additional entry point for ; vsprintf with ap on stack vsnprintf: lda #0 sta ccount+0 sta ccount+1 ; Clear ccount ; Get the size parameter and replace it by a pointer to outdesc. This is to ; build a stack frame for the call to _printf. ; If size is zero, there's nothing to do. ldy #2 lda (sp),y sta ptr1 lda #<outdesc sta (sp),y iny lda (sp),y sta ptr1+1 ora ptr1 beq L9 lda #>outdesc sta (sp),y ; Write size-1 to outdesc.uns ldy ptr1+1 ldx ptr1 bne L1 dey L1: dex stx bufsize+0 sty bufsize+1 ; Copy buf to the outdesc.ptr ldy #5 jsr ldaxysp sta bufptr+0 stx bufptr+1 ; Restore ap and call _printf pla tax pla jsr __printf ; Terminate the string. The last char is either at bufptr+ccount or ; bufptr+bufsize, whichever is smaller. lda ccount+0 ldx ccount+1 cpx bufsize+1 bne L2 cmp bufsize+0 L2: bcc L3 lda bufsize+0 ldx bufsize+1 clc L3: adc bufptr+0 sta ptr1 txa adc bufptr+1 sta ptr1+1 lda #0 tay sta (ptr1),y ; Return the number of bytes written and drop buf lda ccount+0 ldx ccount+1 jmp incsp2 ; Bail out if size is zero. L9: pla pla ; Discard ap lda #0 tax jmp incsp6 ; Drop parameters ; ---------------------------------------------------------------------------- ; Callback routine used for the actual output. ; ; static void out (struct outdesc* d, const char* buf, unsigned count) ; /* Routine used for writing */ ; ; Since we know, we're called with a pointer to our static outdesc structure, ; we don't need the pointer passed on the stack. out: ; Calculate the space left in the buffer. If no space is left, don't copy ; any characters lda bufsize+0 ; Low byte of buffer size sec sbc ccount+0 ; Low byte of bytes already written sta ptr1 lda bufsize+1 sbc ccount+1 sta ptr1+1 bcs @L0 ; Branch if space left lda #$00 sta ptr1 sta ptr1+1 ; No space left ; Replace the pointer to d by a pointer to the write position in the buffer ; for the call to memcpy that follows. @L0: lda bufptr+0 clc adc ccount+0 ldy #4 sta (sp),y lda bufptr+1 adc ccount+1 iny sta (sp),y ; Get Count from stack jsr popax ; outdesc.ccount += Count; pha clc adc ccount+0 sta ccount+0 txa adc ccount+1 sta ccount+1 pla ; if (Count > Left) Count = Left; cpx ptr1+1 bne @L1 cmp ptr1 @L1: bcc @L2 lda ptr1 ldx ptr1+1 ; Jump to memcpy, which will cleanup the stack and return to the caller @L2: jmp _memcpy
wagiminator/C64-Collection
2,122
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/searchenv.s
; ; Ullrich von Bassewitz, 2005-04-21 ; ; Search the environment for a string. ; .export searchenv, copyenvptr .import __environ, __envcount .import ptr1:zp, ptr2:zp, ptr3:zp .code ;---------------------------------------------------------------------------- ; searchenv: ; ; ptr1 must contain the string to search for. On exit, the N flag will tell ; if the entry was found, and X will contain the index of the environment ; string in the environment (a negative value if the entry was not found). ; On success, ptr3 will contain the entry and Y the offset of the '=' within ; the string. .proc searchenv ; Copy the pointer to the environment to the zero page jsr copyenvptr ; Loop over all environment entries trying to find the requested one. ldx __envcount @L0: dex bmi @L9 ; Out of entries ; Since the maximum number of entries is 64, the index can only be 63, so ; the following shift cannot overflow and the carry is clear. txa asl a ; Mul by two for word access tay lda (ptr2),y sta ptr3 iny lda (ptr2),y sta ptr3+1 ; ptr1 points to name, ptr3 points to the next environment entry. Compare the ; two. The following loop limits the length of name to 255 bytes. ldy #$00 @L1: lda (ptr1),y beq @L2 ; Jump on end of name cmp (ptr3),y bne @L0 ; Next environment entry iny bne @L1 ; End of name reached, check if the environment entry contains a '=' char @L2: lda (ptr3),y cmp #'=' bne @L0 ; Next environment entry ; Done. The function result is in X and the N flag is set correctly. @L9: rts .endproc ;---------------------------------------------------------------------------- ; copyenvptr: Copy _environ to ptr2 ; .proc copyenvptr lda __environ sta ptr2 lda __environ+1 sta ptr2+1 rts .endproc
wagiminator/C64-Collection
1,640
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/strdup.s
; ; Ullrich von Bassewitz, 18.07.2000 ; ; char* __fastcall__ strdup (const char* S); ; ; Note: The code knowns which zero page locations are used by malloc. ; .importzp sp, tmp1, ptr4 .import pushax, decsp4, incsp4 .import _strlen, _malloc, _memcpy .export _strdup .macpack cpu .macpack generic _strdup: ; Since we need some place to store the intermediate results, allocate a ; stack frame. To make this somewhat more efficient, create the stackframe ; as needed for the final call to the memcpy function. pha ; decsp will destroy A (but not X) jsr decsp4 ; Target/source ; Store the pointer into the source slot ldy #1 txa sta (sp),y pla .if (.cpu .bitand CPU_ISET_65SC02) sta (sp) .else dey sta (sp),y .endif ; Get length of S (which is still in a/x) jsr _strlen ; Calculate strlen(S)+1 (the space needed) add #1 bcc @L1 inx ; Save the space we're about to allocate in ptr4 @L1: sta ptr4 stx ptr4+1 ; Allocate memory. _malloc will not use ptr4 jsr _malloc ; Store the result into the target stack slot ldy #2 sta (sp),y ; Store low byte sta tmp1 txa ; Get high byte iny sta (sp),y ; Store high byte ; Check for a NULL pointer ora tmp1 beq OutOfMemory ; Copy the string. memcpy will return the target string which is exactly ; what we need here. It will also drop the allocated stack frame. lda ptr4 ldx ptr4+1 ; Load size jmp _memcpy ; Copy string, drop stackframe ; Out of memory, return NULL (A = 0) OutOfMemory: tax jmp incsp4 ; Drop stack frame
wagiminator/C64-Collection
2,097
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/memmove.s
; ; Ullrich von Bassewitz, 2003-08-20 ; Performance increase (about 20%) by ; Christian Krueger, 2009-09-13 ; ; void* __fastcall__ memmove (void* dest, const void* src, size_t size); ; ; NOTE: This function uses entry points from memcpy! ; .export _memmove .import memcpy_getparams, memcpy_upwards, popax .importzp ptr1, ptr2, ptr3, ptr4, tmp1 .macpack generic .macpack longbranch ; ---------------------------------------------------------------------- _memmove: jsr memcpy_getparams ; Check for the copy direction. If dest < src, we must copy upwards (start at ; low addresses and increase pointers), otherwise we must copy downwards ; (start at high addresses and decrease pointers). sec sbc ptr1 txa sbc ptr1+1 jcc memcpy_upwards ; Branch if dest < src (upwards copy) ; Copy downwards. Adjust the pointers to the end of the memory regions. lda ptr1+1 add ptr3+1 sta ptr1+1 lda ptr2+1 add ptr3+1 sta ptr2+1 ; handle fractions of a page size first ldy ptr3 ; count, low byte bne @entry ; something to copy? beq PageSizeCopy ; here like bra... @copyByte: lda (ptr1),y sta (ptr2),y @entry: dey bne @copyByte lda (ptr1),y ; copy remaining byte sta (ptr2),y PageSizeCopy: ; assert Y = 0 ldx ptr3+1 ; number of pages beq done ; none? -> done @initBase: dec ptr1+1 ; adjust base... dec ptr2+1 dey ; in entry case: 0 -> FF lda (ptr1),y ; need to copy this 'intro byte' sta (ptr2),y ; to 'land' later on Y=0! (as a result of the '.repeat'-block!) dey ; FF ->FE @copyBytes: .repeat 2 ; Unroll this a bit to make it faster... lda (ptr1),y sta (ptr2),y dey .endrepeat @copyEntry: ; in entry case: 0 -> FF bne @copyBytes lda (ptr1),y ; Y = 0, copy last byte sta (ptr2),y dex ; one page to copy less bne @initBase ; still a page to copy? ; Done, return dest done: jmp popax ; Pop ptr and return as result
wagiminator/C64-Collection
1,223
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/time.s
; ; Ullrich von Bassewitz, 12.11.2002 ; ; time_t __fastcall__ time (time_t* timep); ; .export _time .import __systime .importzp ptr1, sreg, tmp1 .include "errno.inc" .code .proc _time pha txa pha ; Save timep jsr __systime ; Get the time (machine dependent) sta tmp1 ; Save low byte of result ; Restore timep and check if it is NULL pla sta ptr1 pla sta ptr1+1 ; Restore timep ora ptr1 ; timep == 0? beq @L1 ; timep is not NULL, store the result there ldy #3 lda sreg+1 sta (ptr1),y dey lda sreg sta (ptr1),y dey txa sta (ptr1),y dey lda tmp1 sta (ptr1),y ; If the result is less than zero, set ERRNO @L1: ldy sreg+1 bpl @L2 lda #ENOSYS ; Function not implemented jsr __seterrno ; Set __errno ; Reload the low byte of the result and return @L2: lda tmp1 rts .endproc
wagiminator/C64-Collection
1,030
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/getcpu.s
; ; Ullrich von Bassewitz, 02.04.1999 ; ; unsigned char getcpu (void); ; .export _getcpu ; --------------------------------------------------------------------------- ; Subroutine to detect an 816. Returns ; ; - carry clear and 0 in A for a NMOS 6502 CPU ; - carry set and 1 in A for some CMOS 6502 CPU ; - carry set and 2 in A for a 65816 ; ; This function uses a $1A opcode which is a INA on the 816 and ignored ; (interpreted as a NOP) on a NMOS 6502. There are several CMOS versions ; of the 6502, but all of them interpret unknown opcodes as NOP so this is ; just what we want. .p816 ; Enable 65816 instructions _getcpu: lda #0 inc a ; .byte $1A cmp #1 bcc @L9 ; This is at least a 65C02, check for a 65816 xba ; .byte $eb, put $01 in B accu dec a ; .byte $3a, A=$00 if 65C02 xba ; .byte $eb, get $01 back if 65816 inc a ; .byte $1a, make $01/$02 @L9: ldx #0 ; Load high byte of word rts
wagiminator/C64-Collection
1,054
C64_xu1541/software/tools/cc65-2.13.2/libsrc/common/memchr.s
; ; Ullrich von Bassewitz, 2003-05-05 ; ; void* __fastcall__ memchr (const void* p, int c, size_t n); ; .export _memchr .import popax, return0 .importzp ptr1, ptr2 .proc _memchr eor #$FF sta ptr2 txa eor #$FF sta ptr2+1 ; Save ones complement of n jsr popax ; get c pha jsr popax ; get p sta ptr1 stx ptr1+1 ldy #$00 pla ; Get c ldx ptr2 ; Use X as low counter byte L1: inx beq L3 L2: cmp (ptr1),y beq found iny bne L1 inc ptr1+1 bne L1 ; Branch always L3: inc ptr2+1 ; Bump counter high byte bne L2 ; Not found, return NULL notfound: jmp return0 ; Found, return pointer to char found: ldx ptr1+1 ; get high byte of pointer tya ; low byte offset clc adc ptr1 bcc L9 inx L9: rts .endproc
wagiminator/C64-Collection
2,427
C64_xu1541/software/tools/cc65-2.13.2/libsrc/atmos/crt0.s
; ; Startup code for cc65 (Oric version) ; ; By Debrune Jrme <jede@oric.org> and Ullrich von Bassewitz <uz@cc65.org> ; .export _exit .export __STARTUP__ : absolute = 1 ; Mark as startup .import initlib, donelib .import callmain, zerobss .import __RAM_START__, __RAM_SIZE__, __BSS_LOAD__ .include "zeropage.inc" .include "atmos.inc" ; ------------------------------------------------------------------------ ; Oric tape header .segment "TAPEHDR" .byte $16, $16, $16 ; Sync bytes .byte $24 ; End of header marker .byte $00 ; $2B0 .byte $00 ; $2AF .byte $80 ; $2AE Machine code flag .byte $C7 ; $2AD Autoload flag .dbyt __BSS_LOAD__ ; $2AB .dbyt __RAM_START__ ; $2A9 .byte $00 ; $2A8 .byte $00 ; Zero terminated name ; ------------------------------------------------------------------------ ; Place the startup code in a special segment. .segment "STARTUP" ; Save the zero page area we're about to use ldx #zpspace-1 L1: lda sp,x sta zpsave,x ; Save the zero page locations we need dex bpl L1 ; Clear the BSS data jsr zerobss ; Unprotect columns 0 and 1 lda STATUS sta stsave and #%11011111 sta STATUS ; Save system stuff and setup the stack tsx stx spsave ; save system stk ptr lda #<(__RAM_START__ + __RAM_SIZE__) sta sp lda #>(__RAM_START__ + __RAM_SIZE__) sta sp+1 ; Set argument stack ptr ; Call module constructors jsr initlib ; Push arguments and call main() jsr callmain ; Call module destructors. This is also the _exit entry. _exit: jsr donelib ; Run module destructors ; Restore system stuff ldx spsave txs lda stsave sta STATUS ; Copy back the zero page stuff ldx #zpspace-1 L2: lda zpsave,x sta sp,x dex bpl L2 ; Back to BASIC rts ; ------------------------------------------------------------------------ ; Data .segment "ZPSAVE" zpsave: .res zpspace .bss spsave: .res 1 stsave: .res 1
wagiminator/C64-Collection
9,524
C64_xu1541/software/tools/cc65-2.13.2/libsrc/atmos/atmos-240-200-2.s
; ; Graphics driver for the 240x200x2 mode on the Atmos ; ; Stefan Haubenthal <polluks@sdf.lonestar.org> ; .include "zeropage.inc" .include "tgi-kernel.inc" .include "tgi-mode.inc" .include "tgi-error.inc" .include "atmos.inc" .macpack generic ; ------------------------------------------------------------------------ ; Header. Includes jump table and constants. .segment "JUMPTABLE" ; First part of the header is a structure that has a magic and defines the ; capabilities of the driver .byte $74, $67, $69 ; "tgi" .byte TGI_API_VERSION ; TGI API version number .word 240 ; X resolution .word 200 ; Y resolution .byte 2 ; Number of drawing colors .byte 1 ; Number of screens available xsize: .byte 6 ; System font X size .byte 8 ; System font Y size .res 4, $00 ; Reserved for future extensions ; Next comes the jump table. Currently all entries must be valid and may point ; to an RTS for test versions (function not implemented). .addr INSTALL .addr UNINSTALL .addr INIT .addr DONE .addr GETERROR .addr CONTROL .addr CLEAR .addr SETVIEWPAGE .addr SETDRAWPAGE .addr SETCOLOR .addr SETPALETTE .addr GETPALETTE .addr GETDEFPALETTE .addr SETPIXEL .addr GETPIXEL .addr LINE .addr BAR .addr _CIRCLE .addr TEXTSTYLE .addr OUTTEXT .addr 0 ; IRQ entry is unused ; ------------------------------------------------------------------------ ; Data. ; Variables mapped to the zero page segment variables. Some of these are ; used for passing parameters to the driver. X1 = ptr1 Y1 = ptr2 X2 = ptr3 Y2 = ptr4 RADIUS = tmp1 ; Absolute variables used in the code .bss ERROR: .res 1 ; Error code MODE: .res 1 ; Graphics mode ; Constants and tables PARAM1 = $2E1 PARAM2 = $2E3 PARAM3 = $2E5 TEXT = $EC21 HIRES = $EC33 CURSET = $F0C8 CURMOV = $F0FD DRAW = $F110 CHAR = $F12D POINT = $F1C8 PAPER = $F204 INK = $F210 CIRCLE = $F37F .rodata DEFPALETTE: .byte $00, $07 .code ; ------------------------------------------------------------------------ ; INSTALL routine. Is called after the driver is loaded into memory. May ; initialize anything that has to be done just once. Is probably empty ; most of the time. ; ; Must set an error code: NO ; INSTALL: ; ------------------------------------------------------------------------ ; UNINSTALL routine. Is called before the driver is removed from memory. May ; clean up anything done by INSTALL but is probably empty most of the time. ; ; Must set an error code: NO ; UNINSTALL: rts ; ------------------------------------------------------------------------ ; INIT: Changes an already installed device from text mode to graphics ; mode. ; Note that INIT/DONE may be called multiple times while the driver ; is loaded, while INSTALL is only called once, so any code that is needed ; to initializes variables and so on must go here. Setting palette and ; clearing the screen is not needed because this is called by the graphics ; kernel later. ; The graphics kernel will never call INIT when a graphics mode is already ; active, so there is no need to protect against that. ; ; Must set an error code: YES ; INIT: ; Switch into graphics mode jsr HIRES ; Done, reset the error code lda #TGI_ERR_OK sta ERROR rts ; ------------------------------------------------------------------------ ; DONE: Will be called to switch the graphics device back into text mode. ; The graphics kernel will never call DONE when no graphics mode is active, ; so there is no need to protect against that. ; ; Must set an error code: NO ; DONE = TEXT ; ------------------------------------------------------------------------ ; GETERROR: Return the error code in A and clear it. GETERROR: ldx #TGI_ERR_OK lda ERROR stx ERROR rts ; ------------------------------------------------------------------------ ; CONTROL: Platform/driver specific entry point. ; ; Must set an error code: YES ; CONTROL: sta $213 lda #TGI_ERR_OK sta ERROR rts ; ------------------------------------------------------------------------ ; CLEAR: Clears the screen. ; ; Must set an error code: NO ; CLEAR = HIRES ; ------------------------------------------------------------------------ ; SETVIEWPAGE: Set the visible page. Called with the new page in A (0..n). ; The page number is already checked to be valid by the graphics kernel. ; ; Must set an error code: NO (will only be called if page ok) ; SETVIEWPAGE: ; ------------------------------------------------------------------------ ; SETDRAWPAGE: Set the drawable page. Called with the new page in A (0..n). ; The page number is already checked to be valid by the graphics kernel. ; ; Must set an error code: NO (will only be called if page ok) ; SETDRAWPAGE: rts ; ------------------------------------------------------------------------ ; SETCOLOR: Set the drawing color (in A). The new color is already checked ; to be in a valid range (0..maxcolor-1). ; ; Must set an error code: NO (will only be called if color ok) ; SETCOLOR: sta MODE rts ; ------------------------------------------------------------------------ ; SETPALETTE: Set the palette (not available with all drivers/hardware). ; A pointer to the palette is passed in ptr1. Must set an error if palettes ; are not supported ; ; Must set an error code: YES ; SETPALETTE: ldy #0 lda (ptr1),y sta PARAM1 jsr PAPER ldy #1 lda (ptr1),y sta PARAM1 jsr INK lda #TGI_ERR_OK sta ERROR rts ; ------------------------------------------------------------------------ ; GETPALETTE: Return the current palette in A/X. Even drivers that cannot ; set the palette should return the default palette here, so there's no ; way for this function to fail. ; ; Must set an error code: NO ; GETPALETTE: ; ------------------------------------------------------------------------ ; GETDEFPALETTE: Return the default palette for the driver in A/X. All ; drivers should return something reasonable here, even drivers that don't ; support palettes, otherwise the caller has no way to determine the colors ; of the (not changeable) palette. ; ; Must set an error code: NO (all drivers must have a default palette) ; GETDEFPALETTE: lda #<DEFPALETTE ldx #>DEFPALETTE rts ; ------------------------------------------------------------------------ ; SETPIXEL: Draw one pixel at X1/Y1 = ptr1/ptr2 with the current drawing ; color. The coordinates passed to this function are never outside the ; visible screen area, so there is no need for clipping inside this function. ; ; Must set an error code: NO ; SETPIXEL: lda MODE mymode: sta PARAM3 lda X1 sta PARAM1 lda Y1 sta PARAM2 lda #0 sta PARAM1+1 sta PARAM2+1 jmp CURSET ; ------------------------------------------------------------------------ ; GETPIXEL: Read the color value of a pixel and return it in A/X. The ; coordinates passed to this function are never outside the visible screen ; area, so there is no need for clipping inside this function. GETPIXEL: lda X1 sta PARAM1 lda Y1 sta PARAM2 lda #0 sta PARAM1+1 sta PARAM2+1 jsr POINT lda PARAM1 and #%00000001 ldx #0 rts ; ------------------------------------------------------------------------ ; LINE: Draw a line from X1/Y1 to X2/Y2, where X1/Y1 = ptr1/ptr2 and ; X2/Y2 = ptr3/ptr4 using the current drawing color. ; ; Must set an error code: NO ; LINE: jsr SETPIXEL lda X2 sub X1 sta PARAM1 lda X2+1 sbc X1+1 sta PARAM1+1 lda Y2 sub Y1 sta PARAM2 lda Y2+1 sbc Y1+1 sta PARAM2+1 lda MODE sta PARAM3 jmp DRAW ; ------------------------------------------------------------------------ ; BAR: Draw a filled rectangle with the corners X1/Y1, X2/Y2, where ; X1/Y1 = ptr1/ptr2 and X2/Y2 = ptr3/ptr4 using the current drawing color. ; Contrary to most other functions, the graphics kernel will sort and clip ; the coordinates before calling the driver, so on entry the following ; conditions are valid: ; X1 <= X2 ; Y1 <= Y2 ; (X1 >= 0) && (X1 < XRES) ; (X2 >= 0) && (X2 < XRES) ; (Y1 >= 0) && (Y1 < YRES) ; (Y2 >= 0) && (Y2 < YRES) ; ; Must set an error code: NO ; BAR: inc Y2 @L1: lda Y2 pha lda Y1 sta Y2 jsr LINE pla sta Y2 inc Y1 cmp Y1 bne @L1 rts ; ------------------------------------------------------------------------ ; CIRCLE: Draw a circle around the center X1/Y1 (= ptr1/ptr2) with the ; radius in tmp1 and the current drawing color. ; ; Must set an error code: NO ; _CIRCLE: lda #3 jsr mymode lda RADIUS sta PARAM1 lda MODE sta PARAM2 jmp CIRCLE ; ------------------------------------------------------------------------ ; TEXTSTYLE: Set the style used when calling OUTTEXT. Text scaling in X and Y ; direction is passend in X/Y, the text direction is passed in A. ; ; Must set an error code: NO ; TEXTSTYLE: rts ; ------------------------------------------------------------------------ ; OUTTEXT: Output text at X/Y = ptr1/ptr2 using the current color and the ; current text style. The text to output is given as a zero terminated ; string with address in ptr3. ; ; Must set an error code: NO ; OUTTEXT: lda X1 sta PARAM1 lda Y1 sta PARAM2 lda #3 sta PARAM3 jsr CURSET ldy #0 @next: lda (ptr3),y beq @end sta PARAM1 lda #0 sta PARAM2 lda MODE sta PARAM3 tya pha jsr CHAR lda xsize sta PARAM1 lda #0 sta PARAM2 lda #3 sta PARAM3 jsr CURMOV pla tay iny bne @next @end: rts
wagiminator/C64-Collection
1,181
C64_xu1541/software/tools/cc65-2.13.2/libsrc/atmos/chline.s
; ; Ullrich von Bassewitz, 08.08.1998 ; ; void chlinexy (unsigned char x, unsigned char y, unsigned char length); ; void chline (unsigned char length); ; .export _chlinexy, _chline .import setscrptr .import rvs .import popax .importzp ptr2 .include "atmos.inc" _chlinexy: pha ; Save the length jsr popax ; Get X and Y sta CURS_Y ; Store Y stx CURS_X ; Store X pla ; Restore the length and run into _chline _chline: tax ; Is the length zero? beq @L9 ; Jump if done jsr setscrptr ; Set ptr2 to screen, won't use X txa ; Length into A clc adc CURS_X sta CURS_X ; Correct X position by length lda #'-' ; Horizontal line screen code ora rvs @L1: sta (ptr2),y ; Write one char iny ; Next char bne @L2 inc ptr2+1 ; Bump high byte of screen pointer @L2: dex bne @L1 @L9: rts
wagiminator/C64-Collection
2,138
C64_xu1541/software/tools/cc65-2.13.2/libsrc/atmos/cputc.s
; ; Ullrich von Bassewitz, 2003-04-13 ; ; void cputcxy (unsigned char x, unsigned char y, char c); ; void cputc (char c); ; .export _cputcxy, _cputc .export setscrptr, putchar .import rvs .import popax .importzp ptr2 .include "atmos.inc" _cputcxy: pha ; Save C jsr popax ; Get X and Y sta CURS_Y ; Store Y stx CURS_X ; Store X pla ; Restore C ; Plot a character - also used as internal function _cputc: cmp #$0D ; CR? bne L1 lda #0 sta CURS_X ; Carriage return rts L1: cmp #$0A ; LF? bne output inc CURS_Y ; Newline rts ; Output the character, then advance the cursor position output: jsr putchar advance: iny cpy #40 bne L3 inc CURS_Y ; new line ldy #0 ; + cr L3: sty CURS_X rts ; ------------------------------------------------------------------------ ; Set ptr2 to the screen, load the X offset into Y .code .proc setscrptr ldy CURS_Y ; Get line number into Y lda ScrTabLo,y ; Get low byte of line address sta ptr2 lda ScrTabHi,y ; Get high byte of line address sta ptr2+1 ldy CURS_X ; Get X offset rts .endproc ; ------------------------------------------------------------------------ ; Write one character to the screen without doing anything else, return X ; position in Y .code .proc putchar ora rvs ; Set revers bit pha ; And save jsr setscrptr ; Set ptr2 to the screen pla ; Restore the character sta (ptr2),y ; Set char rts .endproc ; ------------------------------------------------------------------------ ; Screen address table .rodata ScrTabLo: .repeat 28, Line .byte <(SCREEN + Line * 40) .endrep ScrTabHi: .repeat 28, Line .byte >(SCREEN + Line * 40) .endrep
wagiminator/C64-Collection
1,438
C64_xu1541/software/tools/cc65-2.13.2/libsrc/atmos/cgetc.s
; ; Ullrich von Bassewitz, 2003-04-13 ; ; char cgetc (void); ; .export _cgetc .constructor initcgetc .import cursor .include "atmos.inc" ; ------------------------------------------------------------------------ ; .proc _cgetc lda KEYBUF ; Do we have a character? bmi @L2 ; Yes: Get it ; No character, enable cursor and wait lda cursor ; Cursor currently off? beq @L1 ; Skip if so lda STATUS ora #%00000001 ; Cursor ON sta STATUS @L1: lda KEYBUF bpl @L1 ; If the cursor was enabled, disable it now ldx cursor beq @L2 ldx #$00 ; Zero high byte dec STATUS ; Clear bit zero ; We have the character, clear avail flag @L2: and #$7F ; Mask out avail flag sta KEYBUF ldy $209 cpy #$A5 bne @L3 ora #$80 ; FUNCT pressed ; Done @L3: rts .endproc ; ------------------------------------------------------------------------ ; Switch the cursor off, disable capslock. Code goes into the INIT segment ; which may be reused after it is run. .segment "INIT" initcgetc: lda STATUS and #%11111110 sta STATUS lda #$7F sta CAPSLOCK rts
wagiminator/C64-Collection
2,752
C64_xu1541/software/tools/cc65-2.13.2/libsrc/atmos/oserrlist.s
; ; Stefan Haubenthal, 2004-05-25 ; Ullrich von Bassewitz, 18.07.2002 ; ; Defines the platform specific error list. ; ; The table is built as a list of entries ; ; .byte entrylen ; .byte errorcode ; .asciiz errormsg ; ; and terminated by an entry with length zero that is returned if the ; error code could not be found. ; .export __sys_oserrlist ;---------------------------------------------------------------------------- ; Macros used to generate the list (may get moved to an include file?) ; Regular entry .macro sys_oserr_entry code, msg .local Start, End Start: .byte End - Start .byte code .asciiz msg End: .endmacro ; Sentinel entry .macro sys_oserr_sentinel msg .byte 0 ; Length is always zero .byte 0 ; Code is unused .asciiz msg .endmacro ;---------------------------------------------------------------------------- ; The error message table .rodata __sys_oserrlist: sys_oserr_entry 1, "File not found" sys_oserr_entry 2, "Invalid command end" sys_oserr_entry 3, "No drive number" sys_oserr_entry 4, "Bad drive number" sys_oserr_entry 5, "Invalid filename" sys_oserr_entry 6, "fderr=(error number)" sys_oserr_entry 7, "Illegal attribute" sys_oserr_entry 8, "Wildcard(s) not allowed" sys_oserr_entry 9, "File already exists" sys_oserr_entry 10, "Insufficient disc space" sys_oserr_entry 11, "File open" sys_oserr_entry 12, "Illegal quantity" sys_oserr_entry 13, "End address missing" sys_oserr_entry 14, "Start address > end address" sys_oserr_entry 15, "Missing 'to'" sys_oserr_entry 16, "Renamed file not on same disc" sys_oserr_entry 17, "Unknown array" sys_oserr_entry 18, "Target drive not source drive" sys_oserr_entry 19, "Destination not specified" sys_oserr_entry 20, "Cannot merge and overwrite" sys_oserr_entry 21, "Single target file illegal" sys_oserr_entry 22, "Syntax" sys_oserr_entry 23, "Filename missing" sys_oserr_entry 24, "Source file missing" sys_oserr_entry 25, "Type mismatch" sys_oserr_entry 26, "Disc write-protected" sys_oserr_entry 27, "Incompatible drives" sys_oserr_entry 28, "File not open" sys_oserr_entry 29, "File end" sys_oserr_sentinel "Unknown error"
wagiminator/C64-Collection
11,953
C64_xu1541/software/tools/cc65-2.13.2/libsrc/atmos/ctype.s
; ; Ullrich von Bassewitz, 2003-04-13 ; ; Character specification table. ; ; The tables are readonly, put them into the rodata segment .rodata ; The following 256 byte wide table specifies attributes for the isxxx type ; of functions. Doing it by a table means some overhead in space, but it ; has major advantages: ; ; * It is fast. If it were'nt for the slow parameter passing of cc65, one ; could even define macros for the isxxx functions (this is usually ; done on other platforms). ; ; * It is highly portable. The only unportable part is the table itself, ; all real code goes into the common library. ; ; * We save some code in the isxxx functions. ; ; ; Bit assignments: ; ; 0 - Lower case char ; 1 - Upper case char ; 2 - Numeric digit ; 3 - Hex digit (both, lower and upper) ; 4 - Control character ; 5 - The space character itself ; 6 - Other whitespace (that is: '\f', '\n', '\r', '\t' and '\v') ; 7 - Space or tab character .export __ctype __ctype: .byte $10 ; 0/00 ___ctrl_@___ .byte $10 ; 1/01 ___ctrl_A___ .byte $10 ; 2/02 ___ctrl_B___ .byte $10 ; 3/03 ___ctrl_C___ .byte $10 ; 4/04 ___ctrl_D___ .byte $10 ; 5/05 ___ctrl_E___ .byte $10 ; 6/06 ___ctrl_F___ .byte $10 ; 7/07 ___ctrl_G___ .byte $10 ; 8/08 ___ctrl_H___ .byte $D0 ; 9/09 ___ctrl_I___ .byte $50 ; 10/0a ___ctrl_J___ .byte $50 ; 11/0b ___ctrl_K___ .byte $50 ; 12/0c ___ctrl_L___ .byte $50 ; 13/0d ___ctrl_M___ .byte $10 ; 14/0e ___ctrl_N___ .byte $10 ; 15/0f ___ctrl_O___ .byte $10 ; 16/10 ___ctrl_P___ .byte $10 ; 17/11 ___ctrl_Q___ .byte $10 ; 18/12 ___ctrl_R___ .byte $10 ; 19/13 ___ctrl_S___ .byte $10 ; 20/14 ___ctrl_T___ .byte $10 ; 21/15 ___ctrl_U___ .byte $10 ; 22/16 ___ctrl_V___ .byte $10 ; 23/17 ___ctrl_W___ .byte $10 ; 24/18 ___ctrl_X___ .byte $10 ; 25/19 ___ctrl_Y___ .byte $10 ; 26/1a ___ctrl_Z___ .byte $10 ; 27/1b ___ctrl_[___ .byte $10 ; 28/1c ___ctrl_\___ .byte $10 ; 29/1d ___ctrl_]___ .byte $10 ; 30/1e ___ctrl_^___ .byte $10 ; 31/1f ___ctrl_____ .byte $A0 ; 32/20 ___SPACE___ .byte $00 ; 33/21 _____!_____ .byte $00 ; 34/22 _____"_____ .byte $00 ; 35/23 _____#_____ .byte $00 ; 36/24 _____$_____ .byte $00 ; 37/25 _____%_____ .byte $00 ; 38/26 _____&_____ .byte $00 ; 39/27 _____'_____ .byte $00 ; 40/28 _____(_____ .byte $00 ; 41/29 _____)_____ .byte $00 ; 42/2a _____*_____ .byte $00 ; 43/2b _____+_____ .byte $00 ; 44/2c _____,_____ .byte $00 ; 45/2d _____-_____ .byte $00 ; 46/2e _____._____ .byte $00 ; 47/2f _____/_____ .byte $0C ; 48/30 _____0_____ .byte $0C ; 49/31 _____1_____ .byte $0C ; 50/32 _____2_____ .byte $0C ; 51/33 _____3_____ .byte $0C ; 52/34 _____4_____ .byte $0C ; 53/35 _____5_____ .byte $0C ; 54/36 _____6_____ .byte $0C ; 55/37 _____7_____ .byte $0C ; 56/38 _____8_____ .byte $0C ; 57/39 _____9_____ .byte $00 ; 58/3a _____:_____ .byte $00 ; 59/3b _____;_____ .byte $00 ; 60/3c _____<_____ .byte $00 ; 61/3d _____=_____ .byte $00 ; 62/3e _____>_____ .byte $00 ; 63/3f _____?_____ .byte $00 ; 64/40 _____@_____ .byte $0A ; 65/41 _____A_____ .byte $0A ; 66/42 _____B_____ .byte $0A ; 67/43 _____C_____ .byte $0A ; 68/44 _____D_____ .byte $0A ; 69/45 _____E_____ .byte $0A ; 70/46 _____F_____ .byte $02 ; 71/47 _____G_____ .byte $02 ; 72/48 _____H_____ .byte $02 ; 73/49 _____I_____ .byte $02 ; 74/4a _____J_____ .byte $02 ; 75/4b _____K_____ .byte $02 ; 76/4c _____L_____ .byte $02 ; 77/4d _____M_____ .byte $02 ; 78/4e _____N_____ .byte $02 ; 79/4f _____O_____ .byte $02 ; 80/50 _____P_____ .byte $02 ; 81/51 _____Q_____ .byte $02 ; 82/52 _____R_____ .byte $02 ; 83/53 _____S_____ .byte $02 ; 84/54 _____T_____ .byte $02 ; 85/55 _____U_____ .byte $02 ; 86/56 _____V_____ .byte $02 ; 87/57 _____W_____ .byte $02 ; 88/58 _____X_____ .byte $02 ; 89/59 _____Y_____ .byte $02 ; 90/5a _____Z_____ .byte $00 ; 91/5b _____[_____ .byte $00 ; 92/5c _____\_____ .byte $00 ; 93/5d _____]_____ .byte $00 ; 94/5e _____^_____ .byte $00 ; 95/5f _UNDERLINE_ .byte $00 ; 96/60 ___grave___ .byte $09 ; 97/61 _____a_____ .byte $09 ; 98/62 _____b_____ .byte $09 ; 99/63 _____c_____ .byte $09 ; 100/64 _____d_____ .byte $09 ; 101/65 _____e_____ .byte $09 ; 102/66 _____f_____ .byte $01 ; 103/67 _____g_____ .byte $01 ; 104/68 _____h_____ .byte $01 ; 105/69 _____i_____ .byte $01 ; 106/6a _____j_____ .byte $01 ; 107/6b _____k_____ .byte $01 ; 108/6c _____l_____ .byte $01 ; 109/6d _____m_____ .byte $01 ; 110/6e _____n_____ .byte $01 ; 111/6f _____o_____ .byte $01 ; 112/70 _____p_____ .byte $01 ; 113/71 _____q_____ .byte $01 ; 114/72 _____r_____ .byte $01 ; 115/73 _____s_____ .byte $01 ; 116/74 _____t_____ .byte $01 ; 117/75 _____u_____ .byte $01 ; 118/76 _____v_____ .byte $01 ; 119/77 _____w_____ .byte $01 ; 120/78 _____x_____ .byte $01 ; 121/79 _____y_____ .byte $01 ; 122/7a _____z_____ .byte $00 ; 123/7b _____{_____ .byte $00 ; 124/7c _____|_____ .byte $00 ; 125/7d _____}_____ .byte $00 ; 126/7e _____~_____ .byte $40 ; 127/7f ____DEL____ .byte $00 ; 128/80 ___________ .byte $00 ; 129/81 ___________ .byte $00 ; 130/82 ___________ .byte $00 ; 131/83 ___________ .byte $00 ; 132/84 ___________ .byte $00 ; 133/85 ___________ .byte $00 ; 134/86 ___________ .byte $00 ; 135/87 ___________ .byte $00 ; 136/88 ___________ .byte $00 ; 137/89 ___________ .byte $00 ; 138/8a ___________ .byte $00 ; 139/8b ___________ .byte $00 ; 140/8c ___________ .byte $00 ; 141/8d ___________ .byte $00 ; 142/8e ___________ .byte $00 ; 143/8f ___________ .byte $00 ; 144/90 ___________ .byte $00 ; 145/91 ___________ .byte $00 ; 146/92 ___________ .byte $10 ; 147/93 ___________ .byte $00 ; 148/94 ___________ .byte $00 ; 149/95 ___________ .byte $00 ; 150/96 ___________ .byte $00 ; 151/97 ___________ .byte $00 ; 152/98 ___________ .byte $00 ; 153/99 ___________ .byte $00 ; 154/9a ___________ .byte $00 ; 155/9b ___________ .byte $00 ; 156/9c ___________ .byte $00 ; 157/9d ___________ .byte $00 ; 158/9e ___________ .byte $00 ; 159/9f ___________ .byte $00 ; 160/a0 ___________ .byte $00 ; 161/a1 ___________ .byte $00 ; 162/a2 ___________ .byte $00 ; 163/a3 ___________ .byte $00 ; 164/a4 ___________ .byte $00 ; 165/a5 ___________ .byte $00 ; 166/a6 ___________ .byte $00 ; 167/a7 ___________ .byte $00 ; 168/a8 ___________ .byte $00 ; 169/a9 ___________ .byte $00 ; 170/aa ___________ .byte $00 ; 171/ab ___________ .byte $00 ; 172/ac ___________ .byte $00 ; 173/ad ___________ .byte $00 ; 174/ae ___________ .byte $00 ; 175/af ___________ .byte $00 ; 176/b0 ___________ .byte $00 ; 177/b1 ___________ .byte $00 ; 178/b2 ___________ .byte $00 ; 179/b3 ___________ .byte $00 ; 180/b4 ___________ .byte $00 ; 181/b5 ___________ .byte $00 ; 182/b6 ___________ .byte $00 ; 183/b7 ___________ .byte $00 ; 184/b8 ___________ .byte $00 ; 185/b9 ___________ .byte $00 ; 186/ba ___________ .byte $00 ; 187/bb ___________ .byte $00 ; 188/bc ___________ .byte $00 ; 189/bd ___________ .byte $00 ; 190/be ___________ .byte $00 ; 191/bf ___________ .byte $02 ; 192/c0 ___________ .byte $02 ; 193/c1 ___________ .byte $02 ; 194/c2 ___________ .byte $02 ; 195/c3 ___________ .byte $02 ; 196/c4 ___________ .byte $02 ; 197/c5 ___________ .byte $02 ; 198/c6 ___________ .byte $02 ; 199/c7 ___________ .byte $02 ; 200/c8 ___________ .byte $02 ; 201/c9 ___________ .byte $02 ; 202/ca ___________ .byte $02 ; 203/cb ___________ .byte $02 ; 204/cc ___________ .byte $02 ; 205/cd ___________ .byte $02 ; 206/ce ___________ .byte $02 ; 207/cf ___________ .byte $02 ; 208/d0 ___________ .byte $02 ; 209/d1 ___________ .byte $02 ; 210/d2 ___________ .byte $02 ; 211/d3 ___________ .byte $02 ; 212/d4 ___________ .byte $02 ; 213/d5 ___________ .byte $02 ; 214/d6 ___________ .byte $02 ; 215/d7 ___________ .byte $02 ; 216/d8 ___________ .byte $02 ; 217/d9 ___________ .byte $02 ; 218/da ___________ .byte $02 ; 219/db ___________ .byte $02 ; 220/dc ___________ .byte $02 ; 221/dd ___________ .byte $02 ; 222/de ___________ .byte $00 ; 223/df ___________ .byte $01 ; 224/e0 ___________ .byte $01 ; 225/e1 ___________ .byte $01 ; 226/e2 ___________ .byte $01 ; 227/e3 ___________ .byte $01 ; 228/e4 ___________ .byte $01 ; 229/e5 ___________ .byte $01 ; 230/e6 ___________ .byte $01 ; 231/e7 ___________ .byte $01 ; 232/e8 ___________ .byte $01 ; 233/e9 ___________ .byte $01 ; 234/ea ___________ .byte $01 ; 235/eb ___________ .byte $01 ; 236/ec ___________ .byte $01 ; 237/ed ___________ .byte $01 ; 238/ee ___________ .byte $01 ; 239/ef ___________ .byte $01 ; 240/f0 ___________ .byte $01 ; 241/f1 ___________ .byte $01 ; 242/f2 ___________ .byte $01 ; 243/f3 ___________ .byte $01 ; 244/f4 ___________ .byte $01 ; 245/f5 ___________ .byte $01 ; 246/f6 ___________ .byte $01 ; 247/f7 ___________ .byte $01 ; 248/f8 ___________ .byte $01 ; 249/f9 ___________ .byte $01 ; 250/fa ___________ .byte $01 ; 251/fb ___________ .byte $01 ; 252/fc ___________ .byte $01 ; 253/fd ___________ .byte $01 ; 254/fe ___________ .byte $00 ; 255/ff ___________
wagiminator/C64-Collection
26,341
C64_xu1541/software/tools/cc65-2.13.2/libsrc/lynx/lynx-160-102-16.s
; ; Graphics driver for the 160x102x16 mode on the Lynx. ; ; All the drawing functions are simply done by sprites as the sprite ; engine is the only way to do fast graphics on a Lynx. ; ; So the code is not really based on any algorithms done by somebody. ; I have looked at other routines in the cc65 libs to see what kind of ; entry points we need. And I have looked at the old cc65 libs by ; Bastian Schick to see how things worked in the past. ; ; This code is written by Karri Kaksonen, 2004 for the cc65 compiler. ; .include "zeropage.inc" .include "extzp.inc" .include "tgi-kernel.inc" .include "tgi-mode.inc" .include "tgi-error.inc" .include "lynx.inc" .macpack generic ; ------------------------------------------------------------------------ ; Header. Includes jump table and constants. .segment "JUMPTABLE" ; First part of the header is a structure that has a magic and defines the ; capabilities of the driver .byte $74, $67, $69 ; "tgi" .byte TGI_API_VERSION ; TGI API version number .word 160 ; X resolution .word 102 ; Y resolution .byte 16 ; Number of drawing colors .byte 2 ; Number of screens available .byte 8 ; System font X size .byte 8 ; System font Y size .res 4, $00 ; Reserved for future extensions ; Next comes the jump table. Currently all entries must be valid and may point ; to an RTS for test versions (function not implemented). A future version may ; allow for emulation: In this case the vector will be zero. Emulation means ; that the graphics kernel will emulate the function by using lower level ; primitives - for example ploting a line by using calls to SETPIXEL. .addr INSTALL .addr UNINSTALL .addr INIT .addr DONE .addr GETERROR .addr CONTROL .addr CLEAR .addr SETVIEWPAGE .addr SETDRAWPAGE .addr SETCOLOR .addr SETPALETTE .addr GETPALETTE .addr GETDEFPALETTE .addr SETPIXEL .addr GETPIXEL .addr LINE .addr BAR .addr CIRCLE .addr TEXTSTYLE .addr OUTTEXT .addr IRQ ; ------------------------------------------------------------------------ ; Data. ; Variables mapped to the zero page segment variables. Some of these are ; used for passing parameters to the driver. X1 := ptr1 Y1 := ptr2 X2 := ptr3 Y2 := ptr4 STRPTR := ptr3 FONTOFF := ptr4 STROFF := tmp3 STRLEN := tmp4 ; Absolute variables used in the code .bss ERROR: .res 1 ; Error code DRAWINDEX: .res 1 ; Pen to use for drawing VIEWPAGEL: .res 1 VIEWPAGEH: .res 1 DRAWPAGEL: .res 1 DRAWPAGEH: .res 1 ; Text output stuff TEXTMAGX: .res 1 TEXTMAGY: .res 1 TEXTDIR: .res 1 BGINDEX: .res 1 ; Pen to use for text background ; Double buffer IRQ stuff DRAWPAGE: .res 1 SWAPREQUEST: .res 1 text_bitmap: .res 8*(1+20+1)+1 ; 8 rows with (one offset-byte plus 20 character bytes plus one fill-byte) plus one 0-offset-byte ; Constants and tables .rodata DEFPALETTE: .byte >$000 .byte >$007 .byte >$070 .byte >$700 .byte >$077 .byte >$770 .byte >$707 .byte >$777 .byte >$333 .byte >$00F .byte >$0F0 .byte >$F00 .byte >$0FF .byte >$FF0 .byte >$F0F .byte >$FFF .byte <$000 .byte <$007 .byte <$070 .byte <$700 .byte <$077 .byte <$770 .byte <$707 .byte <$777 .byte <$333 .byte <$00F .byte <$0F0 .byte <$F00 .byte <$0FF .byte <$FF0 .byte <$F0F .byte <$FFF PALETTESIZE = * - DEFPALETTE .code ; ------------------------------------------------------------------------ ; INSTALL routine. Is called after the driver is loaded into memory. May ; initialize anything that has to be done just once. Is probably empty ; most of the time. ; ; Must set an error code: NO ; INSTALL: lda #1 sta TEXTMAGX sta TEXTMAGY stz BGINDEX stz DRAWPAGE stz SWAPREQUEST rts ; ------------------------------------------------------------------------ ; UNINSTALL routine. Is called before the driver is removed from memory. May ; clean up anything done by INSTALL but is probably empty most of the time. ; ; Must set an error code: NO ; UNINSTALL: rts ; ------------------------------------------------------------------------ ; INIT: Changes an already installed device from text mode to graphics ; mode. ; Note that INIT/DONE may be called multiple times while the driver ; is loaded, while INSTALL is only called once, so any code that is needed ; to initializes variables and so on must go here. Setting palette and ; clearing the screen is not needed because this is called by the graphics ; kernel later. ; The graphics kernel will never call INIT when a graphics mode is already ; active, so there is no need to protect against that. ; ; Must set an error code: YES ; INIT: ; Enable interrupts for VBL lda #$80 tsb VTIMCTLA ; Done, reset the error code lda #TGI_ERR_OK sta ERROR rts ; ------------------------------------------------------------------------ ; DONE: Will be called to switch the graphics device back into text mode. ; The graphics kernel will never call DONE when no graphics mode is active, ; so there is no need to protect against that. ; ; Must set an error code: NO ; DONE: rts ; ------------------------------------------------------------------------ ; GETERROR: Return the error code in A and clear it. GETERROR: ldx #TGI_ERR_OK lda ERROR stx ERROR rts ; ------------------------------------------------------------------------ ; CONTROL: Platform/driver specific entry point. ; ; Must set an error code: YES ; ; The TGI lacks a way to draw sprites. As that functionality is vital to ; Lynx games we borrow this CONTROL function to implement the still ; missing tgi_draw_sprite funtion. To use this in your C-program ; do a #define tgi_draw_sprite(spr) tgi_ioctl(0, spr) ; ; To do a flip-screen call tgi_ioctl(1, 0) ; ; To set the background index for text outputs call tgi_ioctl(2, bgindex) ; ; To set the frame rate for the display hardware call tgi_ioctl(3, rate) ; ; To check if the drawing engine is busy with the previous swap you can ; call tgi_ioctl(4, 0). It returns 0 if idle and 1 if busy ; ; To update displays you can call tgi_ioctl(4, 1) it will wait for the ; next VBL interrupt and swap draw and view buffers. CONTROL: pha ; Almost all control routines succeed lda #TGI_ERR_OK sta ERROR pla cmp #4 bne ControlFramerate lda ptr1 ; Swap request bne @L0 lda SWAPREQUEST rts @L0: sta SWAPREQUEST rts ControlFramerate: cmp #3 bne ControlTextBG lda ptr1 cmp #75 ; Set framerate beq rate75 cmp #60 beq rate60 cmp #50 beq rate50 lda #TGI_ERR_INV_ARG sta ERROR rts rate50: lda #$bd ; 50 Hz ldx #$31 bra setRate rate60: lda #$9e ; 60 Hz ldx #$29 bra setRate rate75: lda #$7e ; 75 Hz ldx #$20 setRate: sta HTIMBKUP stx PBKUP rts ControlTextBG: cmp #2 bne ControlFlipScreen lda ptr1 ; Set text background color sta BGINDEX rts ControlFlipScreen: cmp #1 bne ControlDrawSprite lda __sprsys ; Flip screen eor #8 sta __sprsys sta SPRSYS lda __viddma eor #2 sta __viddma sta DISPCTL ldy VIEWPAGEL ldx VIEWPAGEH and #2 beq NotFlipped clc tya adc #<8159 tay txa adc #>8159 tax NotFlipped: sty DISPADRL stx DISPADRH rts ControlDrawSprite: lda ptr1 ; Get the sprite address ldx ptr1+1 draw_sprite: ; Draw it in render buffer sta SCBNEXTL stx SCBNEXTH lda DRAWPAGEL ldx DRAWPAGEH sta VIDBASL stx VIDBASH lda #1 sta SPRGO stz SDONEACK @L0: stz CPUSLEEP lda SPRSYS lsr bcs @L0 stz SDONEACK lda #TGI_ERR_OK sta ERROR rts ; ------------------------------------------------------------------------ ; CLEAR: Clears the screen. ; ; Must set an error code: NO ; .rodata pixel_bitmap: .byte 3,%10000100,%00000000, $0 ; A pixel bitmap cls_sprite: .byte %00000001 ; A pixel sprite .byte %00010000 .byte %00100000 .addr 0,pixel_bitmap .word 0 .word 0 .word $a000 ; 160 .word $6600 ; 102 .byte $00 .code CLEAR: lda #<cls_sprite ldx #>cls_sprite bra draw_sprite ; ------------------------------------------------------------------------ ; SETVIEWPAGE: Set the visible page. Called with the new page in A (0..n). ; The page number is already checked to be valid by the graphics kernel. ; ; Must set an error code: NO (will only be called if page ok) ; ; It is a good idea to call this function during the vertical blanking ; period. If you call it in the middle of the screen update then half of ; the drawn frame will be from the old buffer and the other half is ; from the new buffer. This is usually noticed by the user. SETVIEWPAGE: cmp #1 beq @L1 ; page == maxpages-1 ldy #<$e018 ; page 0 ldx #>$e018 bra @L2 @L1: ldy #<$c038 ; page 1 ldx #>$c038 @L2: sty VIEWPAGEL ; Save viewpage for getpixel stx VIEWPAGEH lda __viddma ; Process flipped displays and #2 beq @L3 clc tya adc #<8159 tay txa adc #>8159 tax @L3: sty DISPADRL ; $FD94 stx DISPADRH ; $FD95 rts ; ------------------------------------------------------------------------ ; SETDRAWPAGE: Set the drawable page. Called with the new page in A (0..n). ; The page number is already checked to be valid by the graphics kernel. ; ; Must set an error code: NO (will only be called if page ok) ; SETDRAWPAGE: cmp #1 beq @L1 ; page == maxpages-1 lda #<$e018 ; page 0 ldx #>$e018 bra @L2 @L1: lda #<$c038 ; page 1 ldx #>$c038 @L2: sta DRAWPAGEL stx DRAWPAGEH rts ; ------------------------------------------------------------------------ ; IRQ: VBL interrupt handler ; IRQ: lda INTSET ; Poll all pending interrupts and #VBL_INTERRUPT beq IRQEND ; Exit if not a VBL interrupt lda SWAPREQUEST beq @L0 lda DRAWPAGE jsr SETVIEWPAGE lda DRAWPAGE eor #1 sta DRAWPAGE jsr SETDRAWPAGE stz SWAPREQUEST @L0: IRQEND: clc rts ; ------------------------------------------------------------------------ ; SETCOLOR: Set the drawing color (in A). The new color is already checked ; to be in a valid range (0..maxcolor-1). ; ; Must set an error code: NO (will only be called if color ok) ; SETCOLOR: sta DRAWINDEX rts ; ------------------------------------------------------------------------ ; SETPALETTE: Set the palette (not available with all drivers/hardware). ; A pointer to the palette is passed in ptr1. Must set an error if palettes ; are not supported ; ; Must set an error code: YES ; SETPALETTE: ldy #31 @L1: lda (ptr1),y sta GCOLMAP,y ; $FDA0 dey bpl @L1 ; Done, reset the error code lda #TGI_ERR_OK sta ERROR rts ; ------------------------------------------------------------------------ ; GETPALETTE: Return the current palette in A/X. Even drivers that cannot ; set the palette should return the default palette here, so there's no ; way for this function to fail. ; ; Must set an error code: NO ; GETPALETTE: lda #<GCOLMAP ; $FDA0 ldx #>GCOLMAP rts ; ------------------------------------------------------------------------ ; GETDEFPALETTE: Return the default palette for the driver in A/X. All ; drivers should return something reasonable here, even drivers that don't ; support palettes, otherwise the caller has no way to determine the colors ; of the (not changeable) palette. ; ; Must set an error code: NO (all drivers must have a default palette) ; GETDEFPALETTE: lda #<DEFPALETTE ldx #>DEFPALETTE rts ; ------------------------------------------------------------------------ ; SETPIXEL: Draw one pixel at X1/Y1 = ptr1/ptr2 with the current drawing ; color. The coordinates passed to this function are never outside the ; visible screen area, so there is no need for clipping inside this function. ; ; Must set an error code: NO ; .data pixel_sprite: .byte %00000001 ; A pixel sprite .byte %00010000 .byte %00100000 .addr 0,pixel_bitmap pix_x: .word 0 pix_y: .word 0 .word $100 .word $100 pix_c: .byte $00 .code SETPIXEL: lda X1 sta pix_x lda Y1 sta pix_y lda DRAWINDEX sta pix_c lda #<pixel_sprite ldx #>pixel_sprite jmp draw_sprite ; ------------------------------------------------------------------------ ; GETPIXEL: Read the color value of a pixel and return it in A/X. The ; coordinates passed to this function are never outside the visible screen ; area, so there is no need for clipping inside this function. GETPIXEL: lda Y1 sta MATHD ; Hardware multiply stz MATHC lda #80 sta MATHB stz MATHA lda X1 lsr A php tay clc lda VIEWPAGEL adc MATHH sta ptr1 lda VIEWPAGEH adc MATHG sta ptr1+1 ldx #0 lda #15 sta MAPCTL lda (ptr1),y tay lda #$0c sta MAPCTL tya plp bcc @L1 and #$f rts @L1: lsr A lsr A lsr A lsr A rts ; ------------------------------------------------------------------------ ; LINE: Draw a line from X1/Y1 to X2/Y2, where X1/Y1 = ptr1/ptr2 and ; X2/Y2 = ptr3/ptr4 using the current drawing color. ; ; Must set an error code: NO ; .data line_sprite: .byte 0 ; Will be replaced by the code .byte %00110000 .byte %00100000 .word 0,pixel_bitmap line_x: .word 0 line_y: .word 0 line_sx: .word $100 line_sy: .word $100 .word 0 line_tilt: .word 0 line_c: .byte $e .code LINE: lda DRAWINDEX sta line_c stz line_sx stz line_sy sec lda X2 sbc X1 lda X2+1 sbc X1+1 bpl @L1 lda X1 ldx X2 sta X2 stx X1 lda X1+1 ldx X2+1 sta X2+1 stx X1+1 lda Y1 ldx Y2 sta Y2 stx Y1 lda Y1+1 ldx Y2+1 sta Y2+1 stx Y1+1 @L1: lda #%00000000 ; Not flipped sta line_sprite sec lda Y2 sbc Y1 sta Y2 lda Y2+1 sbc Y1+1 sta Y2+1 bpl @L2 sec lda #0 sbc Y2 sta Y2 lda #0 sbc Y2+1 sta Y2+1 lda #%00010000 ; Vertical flip sta line_sprite @L2: lda X1 sta line_x lda X1+1 sta line_x+1 lda Y1 sta line_y lda Y1+1 sta line_y+1 lda Y2 ina sta line_sy+1 sta MATHP ; hardware divide stz MATHN stz MATHH stz MATHG sec lda X2 sbc X1 ina sta MATHF stz MATHE @L3: lda SPRSYS bmi @L3 ; wait for math done (bit 7 of sprsys) lda MATHC sta line_tilt lda MATHB sta line_tilt+1 bne @L4 lda #1 sta line_sx+1 bra @L6 @L4: bit line_tilt bpl @L5 ina @L5: sta line_sx+1 @L6: lda #<line_sprite ldx #>line_sprite jmp draw_sprite ; ------------------------------------------------------------------------ ; BAR: Draw a filled rectangle with the corners X1/Y1, X2/Y2, where ; X1/Y1 = ptr1/ptr2 and X2/Y2 = ptr3/ptr4 using the current drawing color. ; Contrary to most other functions, the graphics kernel will sort and clip ; the coordinates before calling the driver, so on entry the following ; conditions are valid: ; X1 <= X2 ; Y1 <= Y2 ; (X1 >= 0) && (X1 < XRES) ; (X2 >= 0) && (X2 < XRES) ; (Y1 >= 0) && (Y1 < YRES) ; (Y2 >= 0) && (Y2 < YRES) ; ; Must set an error code: NO ; .data bar_sprite: .byte %00000001 ; A pixel sprite .byte %00010000 .byte %00100000 .addr 0,pixel_bitmap bar_x: .word 0 bar_y: .word 0 bar_sx: .word $0100 bar_sy: .word $0100 bar_c: .byte $00 .code BAR: lda X1 sta bar_x lda Y1 sta bar_y lda X2 sec sbc X1 ina sta bar_sx+1 lda Y2 sec sbc Y1 ina sta bar_sy+1 lda DRAWINDEX sta bar_c lda #<bar_sprite ldx #>bar_sprite jmp draw_sprite ; ------------------------------------------------------------------------ ; CIRCLE: Draw a circle around the center X1/Y1 (= ptr1/ptr2) with the ; radius in tmp1 and the current drawing color. ; ; Must set an error code: NO ; ; There is no sensible way of drawing a circle on a Lynx. As I would ; have to use line elements to do the circle I rather do it in C than ; create it here in the driver. ; To do a circle please add this to your C program ;int sintbl[9] = { ; 0, // 0 degrees ; 3196, // 11.25 degrees ; 6270, // 22.5 degrees ; 9102, // 33.75 degrees ; 11585, // 45 degrees ; 13623, // 56.25 degrees ; 15137, // 67.5 degrees ; 16069, // 78.75 degrees ; 16384 // 90 degrees ;}; ;int sin(char d) ;{ ; char neg; ; d = d & 31; ; neg = d > 16; ; d = d & 15; ; if (d > 8) ; d = 16 - d; ; if (neg) ; return -sintbl[d]; ; else ; return sintbl[d]; ;} ;void tgi_Circle(int x0, int y0, unsigned char r) ;{ ; char i; ; int x1, y1, x2, y2; ; ; x1 = ((long)sin(0) * r + 8192) / 16384 + x0; ; y1 = ((long)sin(8) * r + 8192) / 16384 + y0; ; for (i = 1; i <= 32; i++) { ; x2 = ((long)sin(i) * r + 8192) / 16384 + x0; ; y2 = ((long)sin(i+8) * r + 8192) / 16384 + y0; ; tgi_line(x1, y1, x2, y2); ; x1 = x2; ; y1 = y2; ; } ;} ;#define tgi_circle tgi_Circle CIRCLE: rts ; ------------------------------------------------------------------------ ; TEXTSTYLE: Set the style used when calling OUTTEXT. Text scaling in X and Y ; direction is passend in X/Y, the text direction is passed in A. ; ; Must set an error code: NO ; TEXTSTYLE: stx TEXTMAGX sty TEXTMAGY sta TEXTDIR rts ; ------------------------------------------------------------------------ ; OUTTEXT: Output text at X/Y = ptr1/ptr2 using the current color and the ; current text style. The text to output is given as a zero terminated ; string with address in ptr3. ; ; Must set an error code: NO ; OUTTEXT: lda TEXTMAGX ; Scale sprite sta text_sx+1 lda TEXTMAGY sta text_sy+1 stz text_sprite ; Set normal sprite lda BGINDEX bne @L1 lda #4 sta text_sprite ; Set opaque sprite @L1: lda DRAWINDEX ; Set color asl asl asl asl ora BGINDEX sta text_c lda X1 ; Set start position sta text_x lda X1+1 sta text_x+1 lda Y1 sta text_y lda Y1+1 sta text_y+1 ldy #-1 ; Calculate string length @L2: iny lda (STRPTR),y bne @L2 cpy #20 bmi @L3 ldy #20 @L3: sty STRLEN bne @L4 rts ; Zero length string @L4: iny ; Prepare text_bitmap iny sty STROFF ldy #8-1 ; 8 pixel lines per character ldx #0 clc @L5: lda STROFF sta text_bitmap,x txa adc STROFF tax lda #$ff sta text_bitmap-1,x dey bpl @L5 stz text_bitmap,x stz tmp2 iny @L6: lda (STRPTR),y sty tmp1 sec ; (ch-' ') * 8 sbc #32 stz FONTOFF stz FONTOFF+1 asl asl rol FONTOFF+1 asl rol FONTOFF+1 clc ; Choose font adc #<font sta FONTOFF lda FONTOFF+1 adc #>font sta FONTOFF+1 ; and now copy the 8 bytes of that char ldx tmp2 inx stx tmp2 ; draw char from top to bottom, reading char-data from offset 8-1 to offset 0 ldy #8-1 @L7: lda (FONTOFF),y ; *chptr sta text_bitmap,x ;textbuf[y*(1+len+1)+1+x] txa adc STROFF tax dey bpl @L7 ; goto next char ldy tmp1 iny dec STRLEN bne @L6 lda #<text_sprite ldx #>text_sprite jmp draw_sprite .data text_sprite: .byte $00,$90,$20 .addr 0, text_bitmap text_x: .word 0 text_y: .word 0 text_sx: .word $100 text_sy: .word $100 text_c: .byte 0 .rodata ; The Font ; 96 characters from ASCII 32 to 127 ; 8 pixels wide, 8 pixels high ; bit value 0 = foreground, bit value 1 = background / transparent font: ; VERSAIL .byte $FF, $FF, $FF, $FF, $FF, $FF, $FF, $FF ;32 .byte $FF, $E7, $FF, $FF, $E7, $E7, $E7, $E7 ;33 .byte $FF, $FF, $FF, $FF, $FF, $99, $99, $99 ;34 .byte $FF, $99, $99, $00, $99, $00, $99, $99 ;35 .byte $FF, $E7, $83, $F9, $C3, $9F, $C1, $E7 ;36 .byte $FF, $B9, $99, $CF, $E7, $F3, $99, $9D ;37 .byte $FF, $C0, $99, $98, $C7, $C3, $99, $C3 ;38 .byte $FF, $FF, $FF, $FF, $FF, $E7, $F3, $F9 ;39 .byte $FF, $F3, $E7, $CF, $CF, $CF, $E7, $F3 ;40 .byte $FF, $CF, $E7, $F3, $F3, $F3, $E7, $CF ;41 .byte $FF, $FF, $99, $C3, $00, $C3, $99, $FF ;42 .byte $FF, $FF, $E7, $E7, $81, $E7, $E7, $FF ;43 .byte $CF, $E7, $E7, $FF, $FF, $FF, $FF, $FF ;44 .byte $FF, $FF, $FF, $FF, $81, $FF, $FF, $FF ;45 .byte $FF, $E7, $E7, $FF, $FF, $FF, $FF, $FF ;46 .byte $FF, $9F, $CF, $E7, $F3, $F9, $FC, $FF ;47 .byte $FF, $C3, $99, $99, $89, $91, $99, $C3 ;48 .byte $FF, $81, $E7, $E7, $E7, $C7, $E7, $E7 ;49 .byte $FF, $81, $9F, $CF, $F3, $F9, $99, $C3 ;50 .byte $FF, $C3, $99, $F9, $E3, $F9, $99, $C3 ;51 .byte $FF, $F9, $F9, $80, $99, $E1, $F1, $F9 ;52 .byte $FF, $C3, $99, $F9, $F9, $83, $9F, $81 ;53 .byte $FF, $C3, $99, $99, $83, $9F, $99, $C3 ;54 .byte $FF, $E7, $E7, $E7, $E7, $F3, $99, $81 ;55 .byte $FF, $C3, $99, $99, $C3, $99, $99, $C3 ;56 .byte $FF, $C3, $99, $F9, $C1, $99, $99, $C3 ;57 .byte $FF, $FF, $E7, $FF, $FF, $E7, $FF, $FF ;58 .byte $CF, $E7, $E7, $FF, $FF, $E7, $FF, $FF ;59 .byte $FF, $F1, $E7, $CF, $9F, $CF, $E7, $F1 ;60 .byte $FF, $FF, $FF, $81, $FF, $81, $FF, $FF ;61 .byte $FF, $8F, $E7, $F3, $F9, $F3, $E7, $8F ;62 .byte $FF, $E7, $FF, $E7, $F3, $F9, $99, $C3 ;63 .byte $FF, $C3, $9D, $9F, $91, $91, $99, $C3 ;0 .byte $FF, $99, $99, $99, $81, $99, $C3, $E7 ;1 .byte $FF, $83, $99, $99, $83, $99, $99, $83 ;2 .byte $FF, $C3, $99, $9F, $9F, $9F, $99, $C3 ;3 .byte $FF, $87, $93, $99, $99, $99, $93, $87 ;4 .byte $FF, $81, $9F, $9F, $87, $9F, $9F, $81 ;5 .byte $FF, $9F, $9F, $9F, $87, $9F, $9F, $81 ;6 .byte $FF, $C3, $99, $99, $91, $9F, $99, $C3 ;7 .byte $FF, $99, $99, $99, $81, $99, $99, $99 ;8 .byte $FF, $C3, $E7, $E7, $E7, $E7, $E7, $C3 ;9 .byte $FF, $C7, $93, $F3, $F3, $F3, $F3, $E1 ;10 .byte $FF, $99, $93, $87, $8F, $87, $93, $99 ;11 .byte $FF, $81, $9F, $9F, $9F, $9F, $9F, $9F ;12 .byte $FF, $9C, $9C, $9C, $94, $80, $88, $9C ;13 .byte $FF, $99, $99, $91, $81, $81, $89, $99 ;14 .byte $FF, $C3, $99, $99, $99, $99, $99, $C3 ;15 .byte $FF, $9F, $9F, $9F, $83, $99, $99, $83 ;16 .byte $FF, $F1, $C3, $99, $99, $99, $99, $C3 ;17 .byte $FF, $99, $93, $87, $83, $99, $99, $83 ;18 .byte $FF, $C3, $99, $F9, $C3, $9F, $99, $C3 ;19 .byte $FF, $E7, $E7, $E7, $E7, $E7, $E7, $81 ;20 .byte $FF, $C3, $99, $99, $99, $99, $99, $99 ;21 .byte $FF, $E7, $C3, $99, $99, $99, $99, $99 ;22 .byte $FF, $9C, $88, $80, $94, $9C, $9C, $9C ;23 .byte $FF, $99, $99, $C3, $E7, $C3, $99, $99 ;24 .byte $FF, $E7, $E7, $E7, $C3, $99, $99, $99 ;25 .byte $FF, $81, $9F, $CF, $E7, $F3, $F9, $81 ;26 .byte $FF, $C3, $CF, $CF, $CF, $CF, $CF, $C3 ;27 .byte $FF, $03, $9D, $CF, $83, $CF, $ED, $F3 ;28 .byte $FF, $C3, $F3, $F3, $F3, $F3, $F3, $C3 ;29 .byte $E7, $E7, $E7, $E7, $81, $C3, $E7, $FF ;30 .byte $FF, $EF, $CF, $80, $80, $CF, $EF, $FF ;31 ; gemena .byte $FF, $C3, $9D, $9F, $91, $91, $99, $C3 ;224 .byte $FF, $C1, $99, $C1, $F9, $C3, $FF, $FF ;225 .byte $FF, $83, $99, $99, $83, $9F, $9F, $FF ;226 .byte $FF, $C3, $9F, $9F, $9F, $C3, $FF, $FF ;227 .byte $FF, $C1, $99, $99, $C1, $F9, $F9, $FF ;228 .byte $FF, $C3, $9F, $81, $99, $C3, $FF, $FF ;229 .byte $FF, $E7, $E7, $E7, $C1, $E7, $F1, $FF ;230 .byte $83, $F9, $C1, $99, $99, $C1, $FF, $FF ;231 .byte $FF, $99, $99, $99, $83, $9F, $9F, $FF ;232 .byte $FF, $C3, $E7, $E7, $C7, $FF, $E7, $FF ;233 .byte $C3, $F9, $F9, $F9, $F9, $FF, $F9, $FF ;234 .byte $FF, $99, $93, $87, $93, $9F, $9F, $FF ;235 .byte $FF, $C3, $E7, $E7, $E7, $E7, $C7, $FF ;236 .byte $FF, $9C, $94, $80, $80, $99, $FF, $FF ;237 .byte $FF, $99, $99, $99, $99, $83, $FF, $FF ;238 .byte $FF, $C3, $99, $99, $99, $C3, $FF, $FF ;239 .byte $9F, $9F, $83, $99, $99, $83, $FF, $FF ;240 .byte $F9, $F9, $C1, $99, $99, $C1, $FF, $FF ;241 .byte $FF, $9F, $9F, $9F, $99, $83, $FF, $FF ;242 .byte $FF, $83, $F9, $C3, $9F, $C1, $FF, $FF ;243 .byte $FF, $F1, $E7, $E7, $E7, $81, $E7, $FF ;244 .byte $FF, $C1, $99, $99, $99, $99, $FF, $FF ;245 .byte $FF, $E7, $C3, $99, $99, $99, $FF, $FF ;246 .byte $FF, $C9, $C1, $80, $94, $9C, $FF, $FF ;247 .byte $FF, $99, $C3, $E7, $C3, $99, $FF, $FF ;248 .byte $87, $F3, $C1, $99, $99, $99, $FF, $FF ;249 .byte $FF, $81, $CF, $E7, $F3, $81, $FF, $FF ;250 .byte $FF, $C3, $CF, $CF, $CF, $CF, $CF, $C3 ;251 .byte $FF, $03, $9D, $CF, $83, $CF, $ED, $F3 ;252 .byte $FF, $C3, $F3, $F3, $F3, $F3, $F3, $C3 ;253 .byte $E7, $E7, $E7, $E7, $81, $C3, $E7, $FF ;254 .byte $FF, $EF, $CF, $80, $80, $CF, $EF, $FF ;255
wagiminator/C64-Collection
3,506
C64_xu1541/software/tools/cc65-2.13.2/libsrc/lynx/crt0.s
; *** ; CC65 Lynx Library ; ; Originally by Bastian Schick ; http://www.geocities.com/SiliconValley/Byte/4242/lynx/ ; ; Ported to cc65 (http://www.cc65.org) by ; Shawn Jefferson, June 2004 ; ; *** ; ; Startup code for cc65 (Lynx version). Based on Atari 8-bit startup ; code structure. The C stack is located at the end of the RAM memory ; segment and grows downward. Bastian Schick's executable header is put ; on the front of the fully linked binary (see EXEHDR segment.) ; .include "lynx.inc" .export _exit .export __STARTUP__ : absolute = 1 ; Mark as startup .import callirq, initlib, donelib .import zerobss .import callmain .import _main .import __BSS_LOAD__ .import __INTERRUPTOR_COUNT__ .import __RAM_START__, __RAM_SIZE__ .include "zeropage.inc" .include "extzp.inc" ; ------------------------------------------------------------------------ ; EXE header (BLL header) .segment "EXEHDR" .word $0880 .dbyt __RAM_START__ .dbyt __BSS_LOAD__ - __RAM_START__ + 10 .byte $42,$53 .byte $39,$33 ; ------------------------------------------------------------------------ ; Mikey and Suzy init data, reg offsets and data .rodata SuzyInitReg: .byte $28,$2a,$04,$06,$92,$83,$90 SuzyInitData: .byte $7f,$7f,$00,$00,$24,$f3,$01 MikeyInitReg: .byte $00,$01,$08,$09,$20,$28,$30,$38,$44,$50,$8a,$8b,$8c,$92,$93 MikeyInitData: .byte $9e,$18,$68,$1f,$00,$00,$00,$00,$00,$ff,$1a,$1b,$04,$0d,$29 ; ------------------------------------------------------------------------ ; Actual code .segment "STARTUP" ; set up system sei cld ldx #$FF txs ; init bank switching lda #$C sta MAPCTL ; $FFF9 ; disable all timer interrupts lda #$80 trb TIM0CTLA trb TIM1CTLA trb TIM2CTLA trb TIM3CTLA trb TIM5CTLA trb TIM6CTLA trb TIM7CTLA ; disable TX/RX IRQ, set to 8E1 lda #%11101 sta SERCTL ; clear all pending interrupts lda INTSET sta INTRST ; setup the stack lda #<(__RAM_START__ + __RAM_SIZE__) sta sp lda #>(__RAM_START__ + __RAM_SIZE__) sta sp+1 ; Init Mickey ldx #.sizeof(MikeyInitReg)-1 mloop: ldy MikeyInitReg,x lda MikeyInitData,x sta $fd00,y dex bpl mloop ; these are RAM-shadows of read only regs ldx #$1b stx __iodat dex ; $1A stx __iodir ldx #$d stx __viddma ; Init Suzy ldx #.sizeof(SuzyInitReg)-1 sloop: ldy SuzyInitReg,x lda SuzyInitData,x sta $fc00,y dex bpl sloop lda #$24 sta __sprsys ; Clear the BSS data jsr zerobss ; If we have IRQ functions, set the IRQ vector ; as Lynx is a console there is not much point in releasing the IRQ lda #<__INTERRUPTOR_COUNT__ beq NoIRQ1 lda #<IRQStub ldx #>IRQStub sei sta INTVECTL stx INTVECTH cli ; Call module constructors NoIRQ1: jsr initlib ; Push arguments and call main jsr callmain ; Call module destructors. This is also the _exit entry. _exit: jsr donelib ; Run module destructors ; Endless loop noret: bra noret .segment "CODE" IRQStub: phy phx pha cld jsr callirq lda INTSET sta INTRST pla plx ply rti
wagiminator/C64-Collection
2,692
C64_xu1541/software/tools/cc65-2.13.2/libsrc/lynx/lynx-stdjoy.s
; ; Standard joystick driver for the Atari Lynx. ; The Lynx has two fire buttons. So it is not quite "standard". ; ; Modified by Karri Kaksonen, 2004-09-16 ; Ullrich von Bassewitz, 2002-12-20 ; Using code from Steve Schmidtke ; .include "zeropage.inc" .include "joy-kernel.inc" .include "joy-error.inc" .include "lynx.inc" .include "extzp.inc" .macpack generic ; ------------------------------------------------------------------------ ; Header. Includes jump table .segment "JUMPTABLE" ; Driver signature .byte $6A, $6F, $79 ; "joy" .byte JOY_API_VERSION ; Driver API version number ; Button state masks (8 values) joy_mask: .byte $80 ; JOY_UP .byte $40 ; JOY_DOWN .byte $20 ; JOY_LEFT .byte $10 ; JOY_RIGHT .byte $01 ; JOY_FIRE .byte $02 ; JOY_FIRE1 .byte $00 ; .byte $00 ; ; Jump table. .addr INSTALL .addr UNINSTALL .addr COUNT .addr READ .addr 0 ; IRQ entry unused ; ------------------------------------------------------------------------ ; Constants JOY_COUNT = 1 ; Number of joysticks we support ; ------------------------------------------------------------------------ ; Data. .code ; ------------------------------------------------------------------------ ; INSTALL routine. Is called after the driver is loaded into memory. If ; possible, check if the hardware is present and determine the amount of ; memory available. ; Must return an JOY_ERR_xx code in a/x. ; INSTALL: lda #<JOY_ERR_OK ldx #>JOY_ERR_OK ; rts ; Run into UNINSTALL instead ; ------------------------------------------------------------------------ ; UNINSTALL routine. Is called before the driver is removed from memory. ; Can do cleanup or whatever. Must not return anything. ; UNINSTALL: rts ; ------------------------------------------------------------------------ ; COUNT: Return the total number of available joysticks in a/x. ; COUNT: lda #<JOY_COUNT ldx #>JOY_COUNT rts ; ------------------------------------------------------------------------ ; READ: Read a particular joystick passed in A. READ: ldx #$00 ; Clear high byte lda JOYSTICK ; Read joystick and #$F3 ; Mask relevant keys rts
wagiminator/C64-Collection
5,752
C64_xu1541/software/tools/cc65-2.13.2/libsrc/lynx/eeprom.s
;**************** ; CC65 Lynx Library ; ; Originally by Bastian Schick ; http://www.geocities.com/SiliconValley/Byte/4242/lynx/ ; ; Ported to cc65 (http://www.cc65.org) by ; Shawn Jefferson, June 2004 ; ; Several changes, ; Ullrich von Bassewitz, 1004-10-14 ; ; ;**************** ;* EEPROM-routs ;* for 93C46 (1024bit => 64 16-bit words) ;* ;* created : 11.05.95 ;* last modified : ;* ;* 16.02.96 leaner (thanks to Harry) ;* 12.03.96 test for busy after write and erase (well, Harry ;)) ) ;* 22.08.97 ported to ra65 for use with cc65 ;* 02.12.97 added xref for the new ra65 ;* ;* ;* (c) 1995..97 Bastian Schick ;* CS = A7 (18) ;* CLK = A1 (11) ;* DI/DO = AUDIN (32) ;* ;* And now how to contact the EEPROM : ;* ;* CARD ;* PORT ----\/---- 93C46(SMD too) ;* (18) A7 --------| CS |- +5V ;* (11) A1 --------| CLK |- NC ;* +---| DI |- NC ;* (32) AUDIN ----+---| DO |- GND ;* ---------- ;* ;**************** .export _lynx_eeprom_read .export _lynx_eeprom_write .export _lynx_eeprom_erase .import popa .importzp ptr1 .include "lynx.inc" ; ------------------------------------------------------------------------ ; EEPROM command list EE_C_WRITE = $40 EE_C_READ = $80 EE_C_ERASE = $C0 EE_C_EWEN = $30 EE_C_EWDS = $00 ; ------------------------------------------------------------------------ ; unsigned __fastcall__ lynx_eeprom_read (unsigned char cell); ; /* Read a 16 bit word from the given address */ ; _lynx_eeprom_read: and #$3f ora #EE_C_READ jsr EE_Send9Bit lda #$a sta IODIR ; set AUDIN to Input clc stz ptr1 stz ptr1+1 ; Clear result ldy #16-1 ; Initialize bit counter @L1: ; CLK = 1 stz RCART0 stz RCART0 ; CLK = 0 stz RCART0 stz RCART0 lda IODAT and #$10 ; mask bit adc #$f0 ; C=1 if A=$10 rol ptr1 rol ptr1+1 ; shifts 0 to Carry dey bpl @L1 ldx #$1a stx IODIR ; set AUDIN for output ;EE_SET_CS_LOW ldx #3 stx SYSCTL1 dex stx SYSCTL1 lda ptr1 ldy ptr1+1 ; Load result rts ; ------------------------------------------------------------------------ ; unsigned __fastcall__ lynx_eeprom_erase (unsigned char cell); ; /* Clear the word at the given address */ ; _lynx_eeprom_erase: pha ; Save argument lda #EE_C_EWEN ; EWEN jsr EE_Send9Bit pla ; Restore cell and #$3f ora #EE_C_ERASE ; clear cell A jsr EE_Send9Bit bra EE_wait ; ------------------------------------------------------------------------ ; unsigned __fastcall__ lynx_eeprom_write (unsigned char cell, unsigned val); ; /* Write the word at the given address */ ; _lynx_eeprom_write: sta ptr1 stx ptr1+1 ; Save val into ptr1 lda #EE_C_EWEN ; EWEN jsr EE_Send9Bit jsr popa ; Get cell and #$3f ; Make valid range 0..63 ora #EE_C_WRITE ; WRITE jsr EE_Send9Bit jsr EE_Send16Bit ; Send value in ptr1 EE_wait: ; EE_SET_CS_HIGH ldx #63 EEloop: stz RCART0 stz RCART0 dex bpl EEloop lda #$0A sta IODIR ; AUDIN to input lda #$10 EE_wait1: bit IODAT ; 'til ready :D0-read is /D0-written beq EE_wait1 lda #$1a ; AUDIN to output sta IODIR lda #EE_C_EWDS ; EWDS ; bra EE_Send9Bit ; fall into ; ------------------------------------------------------------------------ ; Send 8 bit value in A to eeprom EE_Send9Bit: ; EE_SET_CS_LOW ldx #3 stx SYSCTL1 dex stx SYSCTL1 ; EE_SET_CS_HIGH ldx #63 EEloop2: stz RCART0 stz RCART0 dex bpl EEloop2 ldy #8 sec ; start bit ror A ror A ror A ror A ; bit 8 at pos. 4 EEloop3: tax and #$10 ora #$b sta IODAT ; CLK = 1 stz RCART0 stz RCART0 ; CLK = 0 stz RCART0 stz RCART0 txa rol A dey bpl EEloop3 lda #$b ; fnr neue EEPROMs sta IODAT rts ; ------------------------------------------------------------------------ ; Send 16 bit value in ptr1 to eeprom EE_Send16Bit: lda ptr1+1 ror A ror ptr1 ror A ror ptr1 ror A ror ptr1 ldy #15 EEloop4: tax and #$10 ora #$b sta IODAT ; CLK = 1 stz RCART0 stz RCART0 ; CLK = 0 stz RCART0 stz RCART0 txa rol ptr1 rol A dey bpl EEloop4 ; EE_SET_CS_LOW ldx #3 stx SYSCTL1 dex stx SYSCTL1 lda #$b ; fnr neue EEPROMs sta IODAT rts
wagiminator/C64-Collection
1,025
C64_xu1541/software/tools/cc65-2.13.2/libsrc/lynx/extzp.s
; ; Ullrich von Bassewitz, 2004-11-06 ; ; zeropage locations for exclusive use by the library ; .include "extzp.inc" .segment "EXTZP" : zeropage ; ------------------------------------------------------------------------ ; mikey and suzy shadow registers __iodat: .res 1 __iodir: .res 1 __viddma: .res 1 __sprsys: .res 1 ; ------------------------------------------------------------------------ ; sound effect pointers for multitimbral Lynx music hardware _abc_score_ptr0: .res 2 _abc_score_ptr1: .res 2 _abc_score_ptr2: .res 2 _abc_score_ptr3: .res 2 ; ------------------------------------------------------------------------ ; Filesystem variables needed for reading stuff from the Lynx cart _FileEntry: ; The file directory entry is 8 bytes _FileStartBlock: .res 1 _FileBlockOffset: .res 2 _FileExecFlag: .res 1 _FileDestAddr: .res 2 _FileFileLen: .res 2 _FileCurrBlock: .res 1 _FileBlockByte: .res 2 _FileDestPtr: .res 2
wagiminator/C64-Collection
1,147
C64_xu1541/software/tools/cc65-2.13.2/libsrc/lynx/kbhit.s
; ; Karri Kaksonen, Harry Dodgson 2006-01-06 ; ; unsigned char kbhit (void); ; .export _kbhit .export KBEDG .export KBSTL .import return0, return1 ; -------------------------------------------------------------------------- ; The Atari Lynx has a very small keyboard - only 3 keys ; Opt1, Opt2 and Pause. ; But the designers have decided that pressing Pause and Opt1 at the ; same time means Restart and pressing Pause and Opt2 means Flip screen. ; For "easter egg" use I have also included all three keys pressed '?' ; and Opt1 + Opt2 pressed '3'. ; So the keyboard returns '1', '2', '3', 'P', 'R', 'F' or '?'. .data KBTMP: .byte 0 KBPRV: .byte 0 KBEDG: .byte 0 KBSTL: .byte 0 KBDEB: .byte 0 KBNPR: .byte 0 .code _kbhit: lda $FCB0 ; Read the Opt buttons and #$0c sta KBTMP lda $FCB1 ; Read Pause and #1 ora KBTMP ; 0000210P tax and KBPRV sta KBSTL ; for multibutton txa and KBDEB sta KBEDG ; for just depressed txa and KBNPR sta KBDEB ; for debouncing txa eor #$ff sta KBNPR ; inverted previous ones pressed stx KBPRV lda KBEDG beq @L1 jmp return1 ; Key hit @L1: jmp return0 ; No new keys hit