Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
 Welcome to our latest new user maak ! (Registered 2024-04-18) You are not logged in - nap
CSDb User Forums


Forums > C64 Coding > C-64 coding cargo cults
2020-01-14 13:33
Krill

Registered: Apr 2002
Posts: 2825
C-64 coding cargo cults

The most-discussed coding cargo cult on the C-64 is probably SEI/CLI around interrupt setup code.

Here's another one: acknowledging VIC raster interrupts.

According to the datasheet http://archive.6502.org/datasheets/mos_6567_vic_ii_preliminary... an active VIC interrupt is acknowledged by writing a "1" to the corresponding bit in $d019.

The usual way to achieve this seems to be "DEC $D019" and to a lesser extent other read-modify-write instructions, saving a few bytes and/or cycles compared to "LDA $D019 : STA $D019" or "LDA #$xF : STA $D019".
This works because RMW instructions on 6502/6510 read a value (here, the pending interrupts) and write the same value again (clearing the interrupt latches) before writing the modified value.
This is also why this technique does not work on SuperCPU's 65816 in native mode, as its RMW instructions lack the dummy-write of the unmodified value.

Now, the cargo cult bit is this: For raster interrupts, it suffices to write any value with bit 0 set (likewise for other VIC interrupts). Clearing all VIC interrupts can be achieved by writing any value with bits 0..3 set.

So, you can save 2 cycles by simply recycling any register value that happens to have bit 0 set, writing that one to $d019 to acknowledge a VIC raster interrupt.

Please post other coding cargo cults here. =)
 
... 31 posts hidden. Click here to view all posts....
 
2020-01-14 14:08
Krill

Registered: Apr 2002
Posts: 2825
Okay, then let's extend this thread to clever hacks as well and not just somewhat superfluous boilerplate code typed in unthinkingly. =)
2020-01-14 14:31
JackAsser

Registered: Jun 2002
Posts: 1987
Quote: Okay, then let's extend this thread to clever hacks as well and not just somewhat superfluous boilerplate code typed in unthinkingly. =)

Ahh ok, now I know the difference!
2020-01-14 15:56
oziphantom

Registered: Oct 2014
Posts: 478
interrupts must always do
pha
txa
pha
tya
pha

pla
....

is another good one.

lda vic reg
and value
ora value
sta vic reg

there are cases where this is needed, and it should be taught this way to hammer the point of setting a bit. But 99.8% of the time you can just do lda sta

also inc $d019 don't think I've ever seen dec
2020-01-14 17:18
Oswald

Registered: Apr 2002
Posts: 5017
I use lsr d019.

Remember flaming on sei/cli. What happens without them if you only changed half of the jump vector?
2020-01-14 17:40
Krill

Registered: Apr 2002
Posts: 2825
Quoting Oswald
Remember flaming on sei/cli. What happens without them if you only changed half of the jump vector?
The point is that you do not need SEI/CLI for regular interrupt setup when starting from BASIC.

There is only one possible interrupt, the CIA1 timer interrupt, which you would disable with LDA #$7F : STA $DC0D. Note that there is no need to acknowledge any pending interrupt by reading $dc0d either.

After that, no interrupts will be triggered, and you can set up a raster interrupt without worrying for a race-condition (interrupt triggering between setting interrupt vector lo- and hi-bytes). Once everything is done you would enable raster interrupts via LDA #$01 : STA $D01A.
2020-01-14 19:45
Oswald

Registered: Apr 2002
Posts: 5017
ok, but honestly thats rather a good practise than a cargo cult. you need to be sure of your enviroment, and then decide if to use sei/cli. not for the faint hearted :)

btw if anyone reads this make sure do also this:

lda #$7f
sta dc0d
sta dd0d

lda $dc0d
lda $dd0d

this will stop all timer irqs, and acknowledge any pending ones. many times have I wondered why my raster irq doesnt work after decades of coding, and it was a missing lda $dc0d often the case.

edit: I missed your note regarding dc0d, but frankly as above written many times I needed to add it in to make raster irq happen from basic!
2020-01-14 20:20
JackAsser

Registered: Jun 2002
Posts: 1987
Quote: ok, but honestly thats rather a good practise than a cargo cult. you need to be sure of your enviroment, and then decide if to use sei/cli. not for the faint hearted :)

btw if anyone reads this make sure do also this:

lda #$7f
sta dc0d
sta dd0d

lda $dc0d
lda $dd0d

this will stop all timer irqs, and acknowledge any pending ones. many times have I wondered why my raster irq doesnt work after decades of coding, and it was a missing lda $dc0d often the case.

edit: I missed your note regarding dc0d, but frankly as above written many times I needed to add it in to make raster irq happen from basic!


especially when linking with other parts. Don't assume anything setup properly by the previous part. Assume crap in all registers imo. But for the initial kernel-takeover, fine no need to sei/cli etc.
2020-01-14 20:24
Golara
Account closed

Registered: Jan 2018
Posts: 212
I use sei/cli when changing irq only in demo parts, for one files I don't think I ever had a situation where I wanted to change the irq when (or where when thinking raster position) an irq could fire (i.e I'm on a way different line)
btw. I do inc $d019, just muscle memory. But that trick with writing anything with bit 0 set is good, i'll remember that.

Here's my little tip, though only about the assembler itself. Kickasm supports labels even in the middle of opcode. For example

lda var1:#$00

var1 points to the $00 itself, it's the same as
var1 = *+1
lda #$00

It's awesome for labeling your registers, it's almost like having variables in C (especially that you can have local labels between {}) My typical irq handler hence looks like this:

irq1:
{
sta rega
stx regx
sty regy
...
inc $d019
lda rega:#$00
ldx regx:#$00
ldy regy:#$00
rti
}
2020-01-14 20:39
JackAsser

Registered: Jun 2002
Posts: 1987
Quote: I use sei/cli when changing irq only in demo parts, for one files I don't think I ever had a situation where I wanted to change the irq when (or where when thinking raster position) an irq could fire (i.e I'm on a way different line)
btw. I do inc $d019, just muscle memory. But that trick with writing anything with bit 0 set is good, i'll remember that.

Here's my little tip, though only about the assembler itself. Kickasm supports labels even in the middle of opcode. For example

lda var1:#$00

var1 points to the $00 itself, it's the same as
var1 = *+1
lda #$00

It's awesome for labeling your registers, it's almost like having variables in C (especially that you can have local labels between {}) My typical irq handler hence looks like this:

irq1:
{
sta rega
stx regx
sty regy
...
inc $d019
lda rega:#$00
ldx regx:#$00
ldy regy:#$00
rti
}


Just beware that such handler is not re-entrant itself, not will it handle background-loading over I/O. At least, have that in mind when linking if "random" stuff occurs.
2020-01-14 23:23
Krill

Registered: Apr 2002
Posts: 2825
Here's another one: X-scrolling.

With the 38-columns mode (without open sideborders), the rightmost column is never visible, regardless of XSCROLL.

Thus, hardscroll handling can safely operate on 39 columns only and ignore the final one, saving cycles in copy loops and the like.

(Bonus: Compared to the 40-columns mode, the left border is collapsed by 7 pixels and the right border by 9 pixels. With XSCROLL = 7, only 38 columns are visible.)
Previous - 1 | 2 | 3 | 4 | 5 - Next
RefreshSubscribe to this thread:

You need to be logged in to post in the forum.

Search the forum:
Search   for   in  
All times are CET.
Search CSDb
Advanced
Users Online
Guests online: 78
Top Demos
1 Next Level  (9.8)
2 Mojo  (9.7)
3 Coma Light 13  (9.7)
4 Edge of Disgrace  (9.6)
5 Comaland 100%  (9.6)
6 No Bounds  (9.6)
7 Uncensored  (9.6)
8 Wonderland XIV  (9.6)
9 The Ghost  (9.6)
10 Bromance  (9.6)
Top onefile Demos
1 It's More Fun to Com..  (9.8)
2 Party Elk 2  (9.7)
3 Cubic Dream  (9.6)
4 Copper Booze  (9.5)
5 Rainbow Connection  (9.5)
6 Wafer Demo  (9.5)
7 TRSAC, Gabber & Pebe..  (9.5)
8 Onscreen 5k  (9.5)
9 Dawnfall V1.1  (9.5)
10 Quadrants  (9.5)
Top Groups
1 Oxyron  (9.3)
2 Nostalgia  (9.3)
3 Booze Design  (9.3)
4 Censor Design  (9.3)
5 Crest  (9.3)
Top Webmasters
1 Slaygon  (9.7)
2 Perff  (9.6)
3 Morpheus  (9.5)
4 Sabbi  (9.5)
5 CreaMD  (9.1)

Home - Disclaimer
Copyright © No Name 2001-2024
Page generated in: 0.043 sec.