Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
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: 2839
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 20:39
JackAsser

Registered: Jun 2002
Posts: 1989
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: 2839
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.)
2020-01-15 00:47
JackAsser

Registered: Jun 2002
Posts: 1989
Quote: 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.)


Speaking of that. Did anyone ever give a proper explanation with the 7/9 division and not 8/8?
2020-01-15 07:47
Krill

Registered: Apr 2002
Posts: 2839
Quoting JackAsser
Speaking of that. Did anyone ever give a proper explanation with the 7/9 division and not 8/8?
Haven't come across one yet. My guess is that there are technical reasons, something with the internal workings of the sideborder latches vs the pixel clock vs the clocks derived from the pixel clock vs register updates.

One practical upside is that with the more common right-to-left scrolling, the second-to-rightmost column may only be updated one video frame later upon an XSCROLL wrap from 0 to 7 (at a scroll speed of 1 pixel per video frame).
2020-01-15 08:23
Golara
Account closed

Registered: Jan 2018
Posts: 212
Quote: Speaking of that. Did anyone ever give a proper explanation with the 7/9 division and not 8/8?

Yeah, top border is weird too, with the screen being fully visible only at Yscroll = 3
2020-01-15 08:33
Krill

Registered: Apr 2002
Posts: 2839
Quoting Golara
Yeah, top border is weird too, with the screen being fully visible only at Yscroll = 3
I guess that is to do with acceptable Y-centering of the screen, also it's 24 vs 25 char rows, so it makes sense to have the default somewhere at half a char row. Now, why the default is 3 and not 4... =)
2020-01-15 08:50
JackAsser

Registered: Jun 2002
Posts: 1989
Quote: Quoting JackAsser
Speaking of that. Did anyone ever give a proper explanation with the 7/9 division and not 8/8?
Haven't come across one yet. My guess is that there are technical reasons, something with the internal workings of the sideborder latches vs the pixel clock vs the clocks derived from the pixel clock vs register updates.

One practical upside is that with the more common right-to-left scrolling, the second-to-rightmost column may only be updated one video frame later upon an XSCROLL wrap from 0 to 7 (at a scroll speed of 1 pixel per video frame).


Sounds reasonable that it's an effect of the pixel pipeline (that nobody described properly in text).
2020-01-15 08:54
Krill

Registered: Apr 2002
Posts: 2839
Quoting JackAsser
Sounds reasonable that it's an effect of the pixel pipeline (that nobody described properly in text).
Plus it's Commodore, so if there's a fraction of a penny to be saved, a fraction of a penny will be saved.
2020-01-15 09:47
Perplex

Registered: Feb 2009
Posts: 254
Quoting Krill
it's Commodore, so if there's a fraction of a penny to be saved, a fraction of a penny will be saved.


7/9 compared to 8/8, that's more than 22% saved!
2020-01-15 11:40
Krill

Registered: Apr 2002
Posts: 2839
Quoting oziphantom
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
I've often seen things like "LDA #$D8 : STA $D016" or similar. That is, setting the unconnected don't-care bits to one (because they are read like that).

I prefer not to set them ($18 in this case), unless i need the don't-care bits with specific values for re-use.
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
csabanw
megasoftargentina
Guests online: 129
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 The Ghost  (9.6)
9 Wonderland XIV  (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 TRSAC, Gabber & Pebe..  (9.5)
7 Onscreen 5k  (9.5)
8 Wafer Demo  (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 Crackers
1 Mr. Z  (9.9)
2 S!R  (9.9)
3 Mr Zero Page  (9.8)
4 Antitrack  (9.8)
5 OTD  (9.8)

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