critique my code

Basic and Machine Language

Moderator: Moderators

Post Reply
User avatar
toby405
Vic 20 Amateur
Posts: 49
Joined: Fri Dec 21, 2012 8:33 pm
Location: USA

critique my code

Post by toby405 »

Hi all. I'm just learning assembly, having fun with 6502 since my first computer ever was a VIC 20.

I would appreciate comments on my code. It prints a blue "A" in the upper left corner of the screen. It should work on expanded/unexpanded vic.

I'm using dasm/VICE on a mac. I invoke this with SYS4608.

Am I doing anything dumb? Thanks!

Code: Select all

        processor 6502
        org $1200

        ; set up vic-20 screen memory address at $0-$1.
        LDA #$0
        STA $0
        LDA $0288
        STA $1

        ; set up color memory address at $2-$3.
        LDA #$0
        STA $2
        LDA $9002
        EOR #$80
        BNE .1
        LDA #$94
        JMP .2
.1      LDA #$96
.2      STA $3

        LDY #$0
        LDA #$1         ; screen code for "A"
        STA ($0),Y      ; set screen code
        LDA #$6         ; color code for blue
        STA ($2),Y      ; set color code
        
        RTS
User avatar
Kweepa
Vic 20 Scientist
Posts: 1316
Joined: Fri Jan 04, 2008 5:11 pm
Location: Austin, Texas
Occupation: Game maker

Post by Kweepa »

You probably shouldn't be using $0-3 on the zero page.
For short routines from BASIC, the best zero page addresses are $fb-fc and $fd-fe as these are unused by the OS.

You can also calculate the color memory pointer rather quicker with this:

Code: Select all

   lda #0
   sta $fd
   ldx #$94
   lda $9002
   bpl .1
   ldx #$96
.1 stx $fe
Or quicker, using $f3-f4:

Code: Select all

   lda #0
   sta $fd
   lda $f4
   and #$fe
   sta $fe
Welcome back to the VIC!
User avatar
toby405
Vic 20 Amateur
Posts: 49
Joined: Fri Dec 21, 2012 8:33 pm
Location: USA

Post by toby405 »

Yeah using $0-$3 was dumb. I'm surprised those 4 bytes of zero page ($fb-$fe) is all we really get but I did find a memory map that also says that. Can you use more if you autostart a cart? I don't plan to use basic at all.

bpl: brilliant!
User avatar
Mike
Herr VC
Posts: 4901
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Post by Mike »

Hi,
toby405 wrote:I'm surprised those 4 bytes of zero page ($fb-$fe) is all we really get[...]
There are some more ZP bytes available, which are normally shared between Tape and RS232 operations. See the thread 'Safe page zero addresses'.

Additionally, the ZP addresses 3..6 also seem save to use: even if they're supposed to contain vectors to two routines concerning ASCII <-> float conversion, BASIC just initialises but never uses them. Neither does any program I know - BASIC calls these routines directly, so supposedly do the user programs.

I routinely included these addresses (where necessary) in my newer projects.
I don't plan to use basic at all.
On the VIC-20, that doesn't buy you much. Unlike to the C64, it is not possible to bank out the BASIC ROM to reveal RAM 'beneath'.

Even if you don't intend to use the interpreter by running a BASIC program, you'll be surprised that it contains a lot of useful routines. :)
rhurst
Omega Star Commander
Posts: 1371
Joined: Thu Jan 31, 2008 2:12 pm
Website: https://robert.hurst-ri.us
Location: Providence, RI
Occupation: Tech & Innovation

Post by rhurst »

Agreed with Mike that there are many BASIC routines useful, particularly if you are going to program a game cart and limit yourself there. And temporary use or sharing some ZP memory assigned to BASIC is possible, too.

That said, if you are not using any IO routines (serial, tape, BASIC), that frees up a lot of zero page. I have used these regularly with the constraints in mind (HEX):

- a 100% machine language program loaded to execute, not mixed with BASIC, and no further use of serial or tape:

Code: Select all

01-02
10
71-72
90
92-98
9A-9F
B7
F7-FE
- if you can avoid use of KERNAL screen IO routines:

Code: Select all

C7-CA
CE
D1-D3
D5-D6
D8-F6
- if you can avoid use of any BASIC ROM, variables, and math:

Code: Select all

17-70
This is not meant to be all comprehensive, mind you, just sharing my experiences. For example, you can also avoid/disable KERNAL routines... but then your program is responsible for a lot of things, and it can get really complex. And being familiar with pages $02 and $03 can be just as important as Zero page.

If you are interested in some game cartridge programming, you might take a look into the source for VIC Software Sprite Stack -- in its primer gamecart folder there is startup code and the ROM image to run.
Any technology distinguishable from magic is insufficiently advanced.
https://robert.hurst-ri.us/rob/retrocomputing
User avatar
Kweepa
Vic 20 Scientist
Posts: 1316
Joined: Fri Jan 04, 2008 5:11 pm
Location: Austin, Texas
Occupation: Game maker

Post by Kweepa »

However, if you're really taking over the machine entirely you can use every single byte of the zero page. For example, Aleksi Eeben's Whack, which starts out like this:

Code: Select all

;-----  zeropage variables

addx            = $00             ; any xy-movement
addy            = $01

tunnels         = $02             ; dungeon level generator
tunnelrun       = $03
rooms           = $04
roomsize        = $05
doors           = $06

vert            = $07             ; vertical door check
horiz           = $08             ; horizontal door check

stairupx        = $09
stairupy        = $0a

stairdownx      = $0b
stairdowny      = $0c

manx            = $0d
many            = $0e

underman        = $0f

gamevar         = $10

HITPOINTS       = $10             ; game variables
GOLD            = $11
GOLD100         = $12
LEVEL           = $13
AMULET          = $14

viewxy          = $15             ; line-of-sight temporary
flip            = $16
monx            = $17
mony            = $18
monster         = $19

tmp             = $1a

scr             = $1b             ; screen line pointer
scrh            = $1c
color           = $1d             ; color memory pointer
colorh          = $1e

seed1           = $1f             ; pseudo-random number generator
seed2           = $20
prevr           = $21

soundplaying    = $22
sounddelay      = $23
soundindex      = $24

oldstick        = $25
newstick        = $26
stickdir        = $27
stickrepeat0    = $28
stickrepeat1    = $29

modulo          = $2a
Similarly, my Blue Star and The Keep use almost the entire zero page.
User avatar
toby405
Vic 20 Amateur
Posts: 49
Joined: Fri Dec 21, 2012 8:33 pm
Location: USA

Post by toby405 »

Thanks for all the zero page info guys, extremely helpful.
Post Reply