Speed-Coding for the 6502 – a Simple Example
Key topics
Diving into the world of 6502 microprocessor optimization, a recent article showcased a simple example of "speed-coding" that sparked a lively discussion. Commenters debated the merits of pre-calculating lookup tables versus building them on-demand as a cache, with some pointing out that the article actually explored the former, while others suggested the latter could be a valuable next step. As one commenter noted, the table is used to scale X/Y coordinates, making every entry necessary, while another observed that the technique of using lookup tables to trade off memory for runtime is a timeless optimization trick. The conversation highlighted the creative problem-solving that went into working with the 6502's constraints, with some sharing their own experiences squeezing complex projects into tight memory limits.
Snapshot generated from the HN discussion
Discussion Activity
Moderate engagementFirst comment
22m
Peak period
7
0-6h
Avg / period
2.6
Based on 13 loaded comments
Key moments
- 01Story posted
Aug 28, 2025 at 5:24 PM EDT
4 months ago
Step 01 - 02First comment
Aug 28, 2025 at 5:46 PM EDT
22m after posting
Step 02 - 03Peak activity
7 comments in 0-6h
Hottest window of the conversation
Step 03 - 04Latest activity
Sep 1, 2025 at 10:45 PM EDT
4 months ago
Step 04
Generating AI Summary...
Analyzing up to 500 comments to identify key contributors and discussion patterns
Want the full context?
Jump to the original sources
Read the primary article or dive into the live Hacker News thread when you're ready.
They could go one step further and calculate the table as needed and use it as a cache.
For an single image scaling it might get a little bit better.
Making it an on-demand cache instead is a neat next step. Whether it helps or hurts depends on the actual input: If the input image uses every pixel value anyway, the additional overhead of checking whether the table entry is computed is just unnecessary extra with no value.
But if a typical image only uses a few pixel values, then the amortized cost of just calculating the few needed table entries may very well be significantly below the cost of the current algorithm.
If images are somewhere in between, or their characteristics not well known, then simply trying out both approaches with typical input data is a good approach!
Unless you’re perfectly happy with 0.2 seconds, for example because the runtime of some other parts take so long that dwarfs those 0.2s, then why bother.
If we presume this scaling operation will be done more than one time (which it probably will be if it's getting this level of optimization) then it gets even worse.
Of course none of that applies here, but it colors the way you think about things…
It's about 2x faster. Your code uses 44 CPU cycles x 64
Edit: plus a branch instruction, maybe that adds 3 cycles x 64 I guess
1) you only need the first 1/4 of the Sine table since the remaining 3/4 are either the first 1/4 in reverse and/or with the sign flipped.
2) and of course Sine can also be used as a Cosine lookup if you add pi/2 radians to the cosine angle (wrapping around of course).
3) to avoid the size needed for a table of floats you can of course use integers (scaled by some factor) or fixed-point values.
4) and simple interpolation would get you seemingly more precision.
(Combining all the above was a bit gross so documentation and a good SPI helped.)
I used a Hewlett Packard development system with a 12" hard disk.
Good times.