190 points · by xmyatniyx · 4y ago · 133 comments
This stuff used to matter! I had an NCR5380 chip on an Amiga, simple, memory mapped I/O, no DMA or interrupts. To get a tape drive to stream (remember that?) the byte transfer loop really had to be tweaked. But once fully tweaked, "whoooooooosh" instead of "chugga chugga chugga".
And truly heroic programming techniques had to be employed on the C64 to do X/Y smooth scrolling games. Often a static part of the screen, conveniently displaying scores etc, existed to make it work - there was just enough bandwidth to do 80% of the screen, say, so you find an excuse to keep the rest of it static.
I kinda miss those days, and I kinda don't. I guess it was good to have experienced them.
You can easily scroll the entire screen on a C64 if that's all you're doing.
Some games did also scroll bitmaps. There the naive version requires moving 9000 bytes (40x25x8 for the bitmap data, 40x25 for the colour data) every time you need to scroll, and that indeed starts to bite. There are games which reduces the cost this using a trick called AGSP ("Any Given Screen Position").
But you're right static parts of the screen were often larger to reduce the dynamic part. That was rarely down to just scrolling the screen in isolation, though, but because the overall budget of cycles you had to work with was tiny. Often you might also have a lot of other stuff which consumed lots of cycles affected directly by the size of the playing field. E.g. if you did sprite multiplexing (moving a sprite after it had been partially or fully rendered to reuse the same hardware sprite), you might well be keeping the CPU busy throughout the full rendering of the playing field.
There was also the consideration of how much effort you wanted to go to in order to avoid glitches, since unless you could do the scrolling entirely while the VIC was rendering the parts of the screen outside the playing field, you'd need to make sure the rendering and copying didn't overlap, and of course just restricting playing field size was an easy workaround for that problem.
Most decent multiplexers took just a scanline or two/three, multiple times down the screen (i.e. whenever relocating any already drawn sprites) — often with decent sized gaps (time when the CPU wasn't involved in manipulating sprites and could do other things), with a larger chunk during the offscreen period / at the bottom of the screen, when one was preping the data (mostly sorting the sprite's y-coords) for the next frame's screen draw.
— During debugging/etc, we'd often enable colour changes to the screen border, at the beginning and end of the multiplexer code (for both the interrupt stuff in the playfield, and the non-playfield section), so we could visually see how it was working/performing.
The border changing thing has just reminded me how bad the development process was using the commodore assembler with a 1541 drive which was horribly slow. assemble, dump image, reboot, crash, reboot, load assembler, try and work out what had happened :)
At some point I ended up with a PC running a system called, I think PDS, which was a cross assembler with dongle to push the image straight into the memory of the C64. I even think you could inspect and change memory on the running machine - it was amazing!
Quite right, one could prepare the moved version in the background during the 7 steps where you're merely diddling the smooth scroll register, and then flip to it in an instant. But wait, was it possible to page flip the colour map? Also, always having the appropriate moved version ready even as the player is doing unpredictable things goes into the "heroic programming techniques" zone again.
As for glitches, it's amazing what can be done if perfection is sacrificed and there were plenty of good games that did have them, e.g. sprite multiplexing. But I did mean "effortless looking perfect" smooth scrolling.
The fastest technique I saw for updating the colour map in a single go, was to have the whole thing as a huge block of immediate mode load-stores, then one could 'scroll' the data across the LDA instructions within that code, in advance, over n-frames, then call this self-modified code block when one did the character screen flip (immediate load-stores was faster than load-stores from colour ram). e.g.
scroll_splat_colour:
LDA #$00 # colour data for char
STA $D800 # colour ram
LDA #$00
STA $D801
# etc., for every visible char onscreen in scrolling area
And one would be updating/scrolling those values loaded into the A register, in chunks over previous frames, similar to: LDA scroll_splat_colour+6
STA scroll_splat_colour+1
LDA scroll_splat_colour+11
STA scroll_splat_colour+6
# etc., for every lda/sta in the above
Perhaps not the clearest explanation, but hopefully enough to communicate the idea.FWIW, I didn't invent that technique, it was an improvement Jon Williams made to my code, whilst we both worked for Images Software (now Climax). Not sure where he got it from, maybe he invented it himself, maybe he cribbed it from elsewhere.
Related: I thought sprite multiplexing was awesome, and there were quite a few tricks there too to get it performant. But that's another far more complex topic.
It's mentioned at the bottom of the article in the "Thoughts" section:
You could certainly use self modifying code and unroll this copy routine to get better performance at the price of flexibility and arguably understanding for the average casual 6502 assembly coder. Again, this was not a "how fast can we absolutely make it" but an everyday use examination.
It's not quite as fast as self-modifying or custom compiled code, but it's pretty close.
Also when I was in the demo scene on the Atari ST one had to do specific timings in the assembly code to be able to draw outside the screen's borders (so the borders were on screen but couldn't be drawn on by code).
This is but one example of this that's happening every day, there is much much more like hair rendering in EA FIFA soccer or automatic trading financial software running on GPUs.
There's a whole world of applications where people are still concerned with every last cycle of performance just like in the C64 days.
https://www.youtube.com/watch?v=eviSykqSUUw&list=PLabw4gCouT...
With it you can reduce the amount of memory you need to copy every 8 pixels (the 8 pixel part can be done with smooth scroll registers).
There's a thread and example code on github here. https://www.lemon64.com/forum/viewtopic.php?t=70539
Also note it's such a terrible hack on the DRAM that it doesn't work on all C64s and there's a technical discussion about that here. https://www.linusakesson.net/scene/safevsp/index.php
Hardware mod if VSP doesn't work on your C64 and more technical details. http://wiki.icomp.de/wiki/VSP-Fix
Also it makes mention of the C64-Reloaded which is a modern C64 product that includes the fix.
I also remember the software voice synthesizer "SAM" needing to blank the screen to render glitch-free sampled audio. Then along came, what was it "Impossible Mission" ("Another visitor! Stay a while...") doing pretty clean sampled audio with the screen on. Not that the C64 SID chip was even remotely intended to be able to play sampled audio in the first place!
The Amiga was unimaginably powerful by comparison. Even a basic configuration had 8x the memory a C64 had, and it had all those fancy DMA toys to offload the CPU.
I don't recall how SAM did it, but sample playing on the C64 SID chip was indeed a nice trick — it was actually done by modulating the main output volume, which made a slight click when changing.
Eventually this got used by some of the C64 musicians / music player libs, so one could play a channel of samples as well as the three regular synth channels on the SID. IIRC, Outrun used this particularly well in its title screen and/or loading music, having some vocal samples "O-O-Outrun!" (and skidding sound effects) as well a sampled drums.
Annoyingly, IIRC, some revisions of the SID chip behaved slightly differently, and had louder or softer sample playback when this hack was used. But still: clever stuff.
Interesting that the two systems were only about two years apart in development. The first C64 prototype would have been in 1981 and the Amiga 1983 but of course they were targeting different price points.
There's a certain reflective quality and even satisfaction from using a chainsaw after coming up using a tree saw by hand. It feels progressive, even if it is just optimizing for time at the expense of energy.
However, expressing these partially unrolled loops nicely is a nice performance-not-safety feature of WUFFS called "Iterate loops":
https://github.com/google/wuffs/blob/main/doc/note/iterate-l...
Well, I say performance not safety, as always they want both, but you could safely just write the never unrolled case, while the existence of Iterate loops allows you to express a much faster special case but know the compiler will fix things up properly no matter what.
Compiler is not going to do absolutely anything for you on those retro platforms.
Jason Turner's CppCon 2021 talk, "Your New Mental Model of constexpr" has half the presentation as a C64 program (though for practical reasons not actually running on a C64 but instead an emulator) because most of the heavy lifting is done by the C++ 20 compiler. https://youtu.be/MdrfPSUtMVM?t=1422
Now, Jason's approach is not going to beat hand-crafted 6502 machine code in a fair fight but he often doesn't need to fight fair and that's the point of his talk.
You could see these patterns on-screen as the reads and writes took place (I think it took about a couple of seconds to do this to 48k of RAM)
"Memory bandwidth" is being used in marketing materials today, so it's a little useful to understand what it means. (The author of this article confuses it with memory transfer, probably others do as well.)
The interval between sprites on the same scanline is 3 cpu cycles. That's a single 6502 instruction, the write to that register. How do you do any kind of load or compare instruction along with that to decide whether to display that sprite?
The answer was to copy that stream of instructions to RAM ahead of time, and replace each write to a missing invader with a no-op. The code is here if anyone wants to see (the "inv3" demo): http://dos486.com/atari/
Even this is easier said than done: there are only 128 bytes of RAM in the entire machine, and that has to suffice for global variables and stack memory in addition to storing modified code like this!
Transfer Alternate Increment (TAI), Transfer Increment Alternate (TIA), Transfer Decrement Decrement (TDD), Transfer Increment Increment (TII) - pretty much x86 'rep movsb', except not great at 6 cycles per byte (~160KB/s). For contrast 5 years older 80286 already did 'rep movsw' at 2 cycles per byte. 6 years later Pentium did 'rep movsd' at 4 bytes per cycle. Nowadays Cannonlake can do 'rep movsb' full cachelines at a time at full cache/memory controller speed.
There were programmers in other divisions making royalties off of their games. Tod Frye famously got $700K or so for his terrible version of 2600 Pac-Man (it was terrible not because he was a bad programmer, but because marketing decided that 2K of ROM had to be enough, and he was smart enough to pull off a miracle . . . of sorts).
Also, the OP apparently doesn't know how to unroll loops, which is the first thing you do to your game's hot spots. (Never had to resort to self-modifying code).
0 HIMEM: 5608
1DATA5532563300922139021390213932135045238263536503037252627503845615305817312092293902932835845478875645836575037148845550351083966132653706165276445377489621322135821322334213502130051520282036
2DATAQLNZQLNZQAQQDSAQRDSAQQDSAQVDSAQXCKDNAPFXANAQXXANANXNXNXQXNXCQXKXNZQXKXNZQCQODMAQODMAUUYQXCKATAUVQXCKMNXQXKANXUAUTCQXKENXUMUSQJHSAHFQZTXRDFQZTAOCQZTAOBQHDSAPDSAQADSANZNZKDSAQZDSAXZQZOZXZUAXZJ
3 READ L$: READ H$
4 FOR I = 1 TO LEN (L$)
5POKE767+I,10*(ASC(MID$(H$,I,1))-65)+VAL(MID$(L$,I,1))
6 NEXT
RUN
HGR : CALL 768: CALL 5608I completely forgot about HIMEM... and what a difference it made when it happened...
God there is so much computer history knowledge thats going to die in the next 20 years.
Easy to forget that the velocity of understanding for people in tech has been ridiculous over the decades.
Ask a new hot shot programmer to do dip-switches and IRQs etc... if they are <40 years old... they may not know what youre even talking about, let alone be able to tell you a PIN switch on a mobo for IRQ settings (PRE IPv4)...
There was a generation of people brought up THROUGH THE STACK in a hardware manner...
So much history is lost through the advancement of history.
This is why we lost the tech to construct pyramids of scale.
"Kids these days just arent interested in levitating blocks with sound waves, and they just only want to domesticate Camels and plants for something called "agriculture""
The LDA, STA, etc operators for zero page access are different opcodes than their two byte address equivalents
The first version contained a memory corrupting bug that took some time to figure out. Depending on the locations of the source and destination you have to start copying forwards from the beginning of the source, or backwards from the end. If there's an overlap you risk overwriting the source before it is copied to the destination.
One neat trick (I remember reading about from Alan Cox I believe) if you have control over the hardware is to memory map I/O devices like serial input / output such that incrementing addresses starting at a given address all point to the same physical device/register. E.g. allocate 256 contiguous bytes in your memory map to point to the same thing. This way you can do bulk I/O transfers to/from memory using MVP/MVN instead of "get a byte, put a byte" instruction by instruction.
https://forum.vcfed.org/index.php?threads/performance-of-lo-...
Port IO Read Speed : 219.39 KB/s
MMIO Read Speed : 310.77 KB/s
Some variants of XTIDE hardware also implement this, as does the free bios.
"The emulator also has a fun hack for disk performance I'm hoping will get replicated in some of the upcoming retro 65C816 board design. Like the 6502 the 65C816 sucks at continually reading from an MMIO port and writing it to sequential memory locations. It sucks less than a 6502 because you've got 16bit index registers, but at the same clock it was doing about 100K/second that a Z80 can do 250K (with ini loops). The revised emulated disk interface has the same mmio port replicated across a chunk of address space and this allows a block move instruction (MVN) to do all the work at 6 clocks/byte. At that point the 65C816 suddenly jumps to twice as fast as the Z80 on disk I/O."
If you're moving memory around in bank 0 (or have memory mapping), you can use the direct page register to read/write anywhere in bank 0 and the stack to read/write anywhere in bank 0.
16-bit LDA dp, PHA is 4 + 4 = 8 cycles or 4 cycles per byte. Best case would be if you know it's constant data before hand, eg, LDA #0, PHA, PHA .... 2 cycles per byte!
For general purpose copying MVP and MVN are easier and have better code density.
While the article does mention it ignores loop unrolling, it's a bit disingenuous, because that almost DOUBLES performance and it's what nearly all real world code is doing.
Also Sam's Journey PAL version does not need any kind of DMA transfer tricks. NTSC version is just a tiny bit behind in timing, so to be glitch free, REU is used. PAL version still works with minor glitches on an NTSC system.
This is because NTSC has 263 * 63 - 25 * 40 = 15569 cycles available per frame (ignoring those stolen by sprites) and PAL 263 * 63 - 25 * 40 = 18656 cycles (again, ignoring sprites).
The difference is enough that the NTSC version can't move required 2000 bytes of color RAM and character RAM in time in the worst case without REU.
Is that what You intended?
SD and even DD are fine (125kbps and 250kbps), so you could read 360KB floppies from a PC.
Either way, beautiful.
They've come a long way.
ARM was designed by the team at Acorn that had worked on the BBC Micro, which used a 6502. They decided to design a custom processor because bit felt none of the 16- or 32-bit processors on the market at the time met the standard set by the 6502 for simplicity and low cost. So, they designed their own architecture which took cues from both the cutting-edge RISC research in academia, and the simple practicality of the 6502.
(On a similar note: the 6502’s main competitor, the Zilog Z80, is an early ancestor of x86! The Z80 is an enhanced clone of the Intel 8080, which of course the 8086 was heavily based on.)
This legacy still shows up today in the instruction mnemonics: ARM uses “branch” naming (BEQ - branch if equal, BCS - branch if carry set, etc) because that’s what the 6502 used, whereas x86 spells it “jump” (JEQ, JCS, etc.). ARM uses LDR/STR to load and store registers from memory (like the 6502’s LDA/LDX/LDY/STA/STX/STY), whereas x86 just spells everything “MOV”. ARM only uses memory-mapped I/O to access hardware, whereas x86 has separate input and output ports.
PowerPC was outliving its useful life due not to ISA, but manufacturing limitations. So Apple went to Intel, but that wasn't fit for mobile. Apple partnered with ARM to make their mobile chips. Then their mobile chips grew into the M1 and M2 along with ARM, bringing them back to a RISC-ish platform like they had with PowerPC. So it's sort of a dual path back to the same place.
I owned an Z80 based computer when I was 8 to 10 years old. Its instruction set and memory access does not have any resemblance for me with 8086.
They seem like very distant relatives.
Sure it is a small instruction set compared to modern CPUs, but RISC is an idea, not a number.
I'd venture to say that RISC is designing with the goal of making very efficient instructions and allow very efficient compilers to be written for it.. It's the idea to have one faster way of doing something rather than multiple convenient ways, because the compiler don't care, and compiler vendors appreciate not having to chose between multiple almost similar instructions that may or may not be faster in some particular case if they don't have to.