Hacker News
A great old-timey game-programming hack (2013)
mikewarot
|next
[-]
My first pass at it was a naïve search using the built in string matching. Because of the size of the file, and the speed of PCs, it would sometimes take more than 30 seconds to do a search. Ward wanted it faster, much faster.
I eventually came up with an algorithm that took the text you wanted to search for, and changed it into a special kind of bit mask, table. It was a table 256 bytes long, initialized to zeros. If the first character of the string you wanted to search for was a D, then the first bit of the $44 and $64 mask would be set, to handle case sensitivity. A wildcard * was only 1 character long, but would set all bits for that character. I then used the 8088 XLAT instruction and a very small loop ingest a character of the text being searched, use that as an index into the mask table, and AND that with the existing flag in a different register, then shifted it, checking for CARRY (a match!) and looping if not zero.
It took search down to less than 3 seconds if the whole file was loaded in RAM. The thing we used as a benchmark was searching for "DOS TECH REF", the IBM DOS Technical Reference Manual.
I can't find the code, lost it over the years, but it was one of my coolest hacks.
gchamonlive
|root
|parent
[-]
The problem is that cloudfront functions are very limited in code size and execution cycles, and the list of cidr blocks from the external CDN was so long it meant we couldn't do a simple comparison, so we did something similar, representing all cidr blocks and the incoming source IP to a binary representation and used binary search to see if the source IP matched any cidr block range. It's also very hard to estimate correctly if the resulting function would in fact fit the compute budget, because AWS isn't very clear about what a cf function cycle is in practice, so we tested it extensively, with real and synthetic cases to simulate worse conditions.
taspeotis
|next
|previous
[-]
> Years ago, I was working at Video Seven, a now-vanished video adapter manufacturer, helping to develop a VGA clone. The fellow who was designing Video Seven’s VGA chip, Tom Wilson, had worked around the clock for months to make his VGA run as fast as possible, and was confident he had pretty much maxed out its performance. As Tom was putting the finishing touches on his chip design, however, news came fourth-hand that a competitor, Paradise, had juiced up the performance of the clone they were developing by putting in a FIFO.
> That was all he knew; there was no information about what sort of FIFO, or how much it helped, or anything else. Nonetheless, Tom, normally an affable, laid-back sort, took on the wide-awake, haunted look of a man with too much caffeine in him and no answers to show for it, as he tried to figure out, from hopelessly thin information, what Paradise had done. Finally, he concluded that Paradise must have put a write FIFO between the system bus and the VGA, so that when the CPU wrote to video memory, the write immediately went into the FIFO, allowing the CPU to keep on processing instead of stalling each time it wrote to display memory.
> Tom couldn’t spare the gates or the time to do a full FIFO, but he could implement a one-deep FIFO, allowing the CPU to get one write ahead of the VGA. He wasn’t sure how well it would work, but it was all he could do, so he put it in and taped out the chip.
> The one-deep FIFO turned out to work astonishingly well; for a time, Video Seven’s VGAs were the fastest around, a testament to Tom’s ingenuity and creativity under pressure. However, the truly remarkable part of this story is that Paradise’s FIFO design turned out to bear not the slightest resemblance to Tom’s, and didn’t work as well. Paradise had stuck a read FIFO between display memory and the video output stage of the VGA, allowing the video output to read ahead, so that when the CPU wanted to access display memory, pixels could come from the FIFO while the CPU was serviced immediately. That did indeed help performance—but not as much as Tom’s write FIFO.
[1] https://www.jagregory.com/abrash-black-book/#the-challenge-o...
adonovan
|next
|previous
[-]
green_wheel
|next
|previous
[-]
Agentlien
|root
|parent
|next
[-]
Not saying that most game programming is like that, but if you specialize in graphics and performance, preferably in large productions, you get to work close to the hardware and solve a lot of really fun problems in interesting ways.
pjc50
|root
|parent
|next
|previous
[-]
Or there's MCUs with programmable IO for bit-bashing protocols.
Krei-se
|root
|parent
|next
|previous
[-]
WebGPU in a browser is worth the learning curve - and gives you the same interface then in Rust!
When you sniffed that sweet sweet shader code and memory layouting - why not get into WebAssembly?
You can have sth very similar to an OS in your browser if you want. It's a lot of fun and knowledge actually useful instead of the next hypecycle of frameworks and dialects.
sehugg
|next
|previous
[-]
The Atari 2600 has horizontal object counters that you can't set directly, but they latch when you write to them, and they set the corresponding object to the X position of the beam at that cycle. Pole Position requires three of these objects to draw the left/right side of the road and center line, and they are supposed to meet at the horizon.
But the beam moves to the right 3 pixels for every CPU cycle, and writing with STA would result in each being separated by 9 pixels. What's the fix? Move the stack pointer to where the object pointers live (the 2600 maps the stack to zero page, where the RAM and registers live) and invoke BRK, making sure the BRK vector returns you to the next instruction. The object pointers are adjacent, so BRK writes to each object pointer on successive cycles, and now each of them is 1 cycle = 3 pixels apart.
pjc50
|next
|previous
[-]
This sort of thing is why the Amiga was so massively successful by adding a collection of co-processors: https://en.wikipedia.org/wiki/Amiga_Original_Chip_Set
Each of which had limited but dedicated functionality for moving and transforming bytes in RAM. The blitter in particular was dedicated to moving memory around for tile and sprite rendering.
EvanAnderson
|root
|parent
[-]
jimbobbam
|next
|previous
[-]
whizzter
|root
|parent
|next
[-]
That said, modern machines have enough oomph that tricks of that kind haven't really been the major contributions on the table for the past 25 years (sure, there's some tricks with PS2 VU's/memory, register swaps,etc but they're small in comparison to the total product sizes).
rob74
|next
|previous
[-]
On the PC in question, if you wanted to draw something onto the screen, you had to do that yourself, byte by byte. No texture mappers, no blitters. Just bytes. Bytes that you had to move yourself.
That's not entirely correct: the graphics hardware took the video memory and built an image from that (frame buffer graphics). Which is already more than the earliest game consoles did.
Agentlien
|next
|previous
[-]
I'm young enough that I wasn't there, but I've been programming since I was a kid and working with graphics for the last 15 years.
This type of excitement is the feeling I got from learning C and C++ to begin with. Trying to really understand how things were working under the hood.
I still get that sense when working on low level graphics and performance in my current job. In fact, when the texture memory corruption was introduced the article felt eerily similar to the events which inspired me to write my own recent article about texture memory layouts ( https://agentlien.github.io/texture )
Dwedit
|next
|previous
[-]
For those unfamiliar with Monster Max (1994, Game Boy), it's basically the last of the British Isometric Platformers, a genre started by Knight Lore on ZX spectrum.
whizzter
|root
|parent
|next
[-]
For reference, unlike the PC,C64,etc f.ex. the Gameboy (and NES) only allows users to write memory during VBlanks/HBlanks (or when the screen is turned off), so instead of delaying and adding complicated logic for that due to memory usage contention the CPU is simply cut-off from the memory (thus reads/writes at the wrong time are just nonsense/discarded iirc).
fferen
|next
|previous
[-]
__del__
|root
|parent
[-]
the ti-81 was somehow worse. the If conditional only had the implicit else form (no Then/Else/End), there were no looping constructs (just Lbl, Goto, DS< and IS>), you had two lists for array-like data, and i can't recall ever figuring out how to manipulate strings—it may not have been possible.
mrandish
|next
|previous
[-]
My first computer was the original 4K Color Computer (1981). I learned 6809 assembler on it and worked full time programming it for several years, while also running one of the largest users groups for it. Traveling to Coco trade shows around the country, I met many of the devs writing the most popular Coco games and the trick was definitely known by around Summer 1983 as that's when the first Rainbowfest was held and I heard about there.
Around 1983, Steve Bjork (MegaBug / Zaxxon Coco) told me the devs at DataSoft, the company behind some of the popular 1st party cartridge games Radio Shack sold, were using it. I also heard about it in 1984 from Tim Jenison (CocoMax). The earliest usage of the trick in a commercial game was probably in a Williams arcade game, either Defender (1981) or Joust (1982). A few years ago at the Hackers Conference Tim told me he'd once asked Eugene Jarvis (Joust, Defender) about it and Eugene told him after discovering it they'd nicknamed the trick "PUSH-U / PUL-Me." I confirmed this with Sam Dicker (Sinistar (1983), Defender) last year during the Amiga 040th anniversary at the Computer History Museum.
While the basic trick to do a fast software block image transfer was discovered separately by several 6809 programmers, I can't recall ever seeing the Direct Page (DP) register used with it before today. That and some of the other refinements in TFA are quite clever additions. I think the Coco's lack of any hardware sprites, tiles, scrolling or acceleration made Coco devs especially desperate, so we had to become masters of software blits with PSHU/PULS and software sprites with XOR. And since all this was happening before Usenet and dial-up BBSes were commonly available, there was no way to share cool tricks widely except printed books and magazines.
Fortunately, the 6809 made up for Radio Shack's penny-pinching design by being much faster than the 6502 and Z80, which were microcoded while the 6809 was implemented directly in logic gates. It's widely considered the most advanced of all the 8-bit CPUs with an especially elegant, orthogonal ISA supporting position independent, re-entrant, multitasking code and powerful indirect addressing modes. One little known bit of history is that Jef Raskin's team at Apple used the 6809 for the initial Macintosh prototypes before Steve Jobs managed to cut a steep discount deal with Motorola on the workstation-priced 68000 CPU. By 1981, Moto was probably starting to realize they'd been pricing their CPUs far too high, having lost the Apple II, Commodore PET, VIC-20, C64 and then IBM PC designs, despite having a better chip. Waking up just in time to win, arguably, the LAST big desktop computer CPU design... ever? Way to go Moto!
2013 Hacker News comments here: https://news.ycombinator.com/item?id=6913467
zahlman
|root
|parent
|next
[-]
It's interesting to think about the traditional attitude towards documentation. Back in the day, people could discover incredibly clever things, but when it came to writing stuff down and distributing the information in an organized way, it seems like you only got the "reference" documentation. The books you could get would only list the opcodes; to learn a trick like this, you'd either have to talk to someone who knew it, or figure it out again.
djmips
|root
|parent
|previous
[-]
On the Vectrex, also a 6809 machine, the fastest vector draw routines use the stack to thread draw commands, not quite the same thing but those sixteen bit stack pointers sure are useful.
mrandish
|root
|parent
[-]
Re: the Vectrex, I never knew that but it makes sense since chasing the beam requires feeding a massive queue of vector pairs.
awmc2000
|next
|previous
[-]
__del__
|root
|parent
|next
[-]
as he emphasized endlessly, you can modify the running kernel, so it's easy to rip out his fanatical religious stuff.
Agentlien
|root
|parent
|previous
[-]
nigehban
|next
|previous
[-]
whizzter
|root
|parent
[-]
Shading was expensive since you had 3 color components to scale, but one trick if you had monochrome color was that you could do it with 1(16bit) or 2 multiplications(32bit) by moving bits around.
If you had RGB, you could do red and blue at once since you'd easily mask by 0xff00ff , then multiply by the intensity (0-0x100), since the result of 0xff*0x100 is at most 0xff00 you could just mask the result by 0xff00ff00 and shift it down 8 bits (the shifting could be with the green component combined back in).
1 multiply for 16bit color (5R 6G 5B) was basically the same thing by shifting green upwards, or'ing, masking and multiply a 5bit intensity once on the 32bit pattern 00000gggg gggg00000 rrrrr000 000bbbbb before shifting down the green part again and combining the result.
I'm sure others had even more similarly clever tricks back in that era.
brunooliv
|next
|previous
[-]
baud9600
|next
|previous
[-]
Panzerschrek
|next
|previous
[-]
Rohansi
|root
|parent
|next
[-]
BrtByte
|next
|previous
[-]
abrookewood
|next
|previous
[-]
cobbzilla
|next
|previous
[-]
II2II
|root
|parent
[-]
Real Programmers wrote in machine code.
Not FORTRAN. Not RATFOR. Not, even, assembly language.
Machine Code.
Raw, unadorned, inscrutable hexadecimal numbers.
Directly.
That said, I prefer Tom's story. It's more about the joy of understanding the machine than it is about the framing of a legend.