Hacker News new | past | comments | ask | show | jobs | submit login
Elite on the 6502: The original 6502 assembly source, heavily commented (bbcelite.com)
212 points by CharlesW 4 months ago | hide | past | favorite | 59 comments



I think Computer Architecture can be designed around legacy but fun hardwares.

So instead of using some fantasy machines such as LC-3, students can directly get into the world of 6502.

First semester: mostly concentrated on 6502 with a full 6502 CPU emulator as the final project.

Second semester: concentrate on a real machine such as the NES. Students need to learn to improve their emulators to program in it (so, do you need a debugger? a decompiler?), as well as hacking roms, burning roms, using carts to load the games into a real NES, and programming new games for it. The game must run in the emulator as well as a real NES.

Third semester for honour: upgrade to a 16/24/32-bit machine, e.g. 80286, SNES, 68K, whatever, and get some serious project done. Can use C if applicable. The project can even be a simple OS or a BASIC interpreter to pave the way for advanced classes. Or maybe a computer virus, anything that is fun and creative.

OS and compiler classes can also be designed around real hardware and projects. e.g. Porting an early version of Linux to a once popular architecture and add a few small functionalities; Write a compiler for a LISP-like language for that architecture and OS.

You don't even need Data Structure and Algorithm and those BS Java programming classes - they learn them on the way. And if they miss anything they can pick them up on the job.


> First semester: mostly concentrated on 6502 with a full 6502 CPU emulator as the final project.

I like the principle, and the 6502 holds a special place in my heart as my introduction to machine code programming on an Apple ][ in late 1980 after I got bored with BASIC after two days .. self-taught from the ROM listing and 6502 reference in the back of the manual.

But it's absolutely the wrong ISA today.

RISC-V RV32I (or RV32E, with only 16 registers) is the right ISA today, for many reasons. It is every bit as easy to understand as 6502, if not easier. It is certainly much easier to write an emulator for than 6502 -- and I've done both. And it is at least 10x easier to write real useful programs using, especially if you're going to deal with values bigger than 8 bits, or pointers, or recursive/reentrant subroutines.

You can also cheaply buy a large and growing range of RISC-V hardware, starting from the $0.10 CH32V003 (48 MHz, 2k RAM, 16k of flash for your program) or boards with that chip for about $1, up to ESP32-C3 or RP2350 boards for a couple of bucks with a few hundred KB of RAM. Or the full Linux $5 Milk-V Duo with 1 GHz 64 bit CPU, 64 MB RAM, and SD card for the OS/programs, which you can power from the USB port on any PC and also ssh into it from the PC.

The only advantage the 6502 has now is it is one of the few chips you can still buy with exposed and non-multiplexed 16 bit address bus and 8 bit data bus, which allows you to do tricks such as hard-wiring the data bus to a NOP instruction (0xEA), and feed it a slow clock and watch the PC increment the address bus through all the NOPs.

Nice, but you can do that with an emulator too, or for that matter with a debugger talking to your microcontroller chip.


I don't disagree with you. I picked 6502 has the advantage of having so many real legacy products built on top of it.

But again, I agree it's not a great ISA (and only having 3 registers always bug me anyway), so as long as it's a real machine I think it's good. Maybe the one you talked about, or a Z80 machine, would all do the job.


Something with meaningful restrictions such as register count and RAM that you'll hit quickly when hand-coding assembler is a super IMHO useful teaching tool. 16 registers is luxurious!


The 16 registers in RV32EC [1] on the CH32V003 is not a lot using the standard ABI once you take out the zero register, return address, stack pointer, globals pointer, and thread pointer [2] you're left with 11 registers, allocated as 6 function argument / local variable registers (caller cave), 2 callee save registers, and 3 temporary registers.

Empirically you need up to 20% more instructions executed vs the 32 register RV2I, which is also similar to what Intel reports with their new "APX" x86 extension giving 32 GPRs.

The 2k RAM also concentrates the mind more so than a 6502 or Z80 with 64k RAM.

[1] you can ignore the "C" for simplicity if you want

[2] which arguably you're not going to use for that purpose so it's effectively available too, most usefully as a 3rd callee save register.


For a teaching tool, you can get the best of both worlds by simply restricting RISC-V to fewer registers. Something like: "Write this in RISC-V assembly, but only use registers 0..3, and 31 for the return address."


rv32e and rv64e have 16 registers rather than 32.

Maybe use these.


I like your idea, though I'm probably biased because I learned computers and computer programming starting with zero experience, zero knowledge and nothing but a 4K, sub-1 Mhz Radio Shack Color Computer. Unfortunately, in 1980 there were virtually no real instructional materials available on microcomputers. Learning consisted mostly of just trying things, typing in listings from low-budget hobbyist 'zines and comparing notes at user's group meetings.

One great advantage to your approach is that emulators are free and some of them, like MAME, have extremely powerful debugging and exploration tools built right in. Plus there's extensive developer documentation, SDKs and instructional materials available for many of the most popular platforms along with lots of commented code disassemblies. Having an actual course structured around curated sets of these materials would be pretty amazing.


There's a YouTube channel called CocoTown where he's building a Moon Patrol clone for the Color Computer in assembly from scratch. He's making good use of MAME and other modern tools. Can't imagine how less pleasant it would have been creating something like that using just EDTASM back in the day.


That's really a nice project. I had a thought a few years ago that if someone wants to be a game programmer, one idea is to find a hero (e.g. Rebecca Heinemann or John Carmack) and replicate some games they made, aka walk their roads on retro platforms.


Yep, and I know some instructors actually tried that (from Carnegie-Mellon):

https://bobrost.com/nes/

I think it's a good idea to go further to work on more difficult retro platforms. I know teachers like LC-3 because it is simple, but I think they underestimate the devoted students.


> I think it's a good idea to go further to work on more difficult retro platforms.

Agreed, although I'm not sure platforms like Genesis, Amiga or NeoGeo would really be much more difficult for a beginner following a framework than NES. However, their increased performance, resolution and colors would be more capable of creating inspiring output.


I love the 6502, but if we're considering teaching architecture using a legacy machine my preferred choice would be the PDP-11. The ISA is quite nice, and the machine has many of the "modern" features one would expect from a computer, like address translation. On top of that, there's loads of good, historic code to be looked at. Hell, bring back the Lions commentary book!


I don't disagree with you. Any real computer would work I think. But maybe teaching a not-so-good but popular ISA has educational values too. After all we don't know why good things are good if we haven't seen the bad ones...hah!


6502 would give them a dose of reality, but I think teaching people by having them program an 8-bit CISC chip is cruel. There's so many asymmetries that get in the way.

(Also, I don't think anyone actually likes NES games except for nostalgia, but this might just be because square waves make my ears hurt.)


This just isn't true. There are plenty of very good NES games that have great game loops and are a blast to play. Some of the good ones have quirks or unintuitive controls but are still quite enjoyable.


Sure, if you turn off the sound. Maybe a lowpass filter would do.

I guess dads with NES nostalgia have lost their high-pitched hearing anyway.


I like this idea.

My fantasy course would start with a BBC Micro - 6502 BBC Basic with its inline assembler.

Then move on to an Acorn Archimedes - 32 bit ARM which was designed as a direct replacement for 6502.

Finally move to Raspberry Pi - 64 bit ARM with multiple cores.


Ah, just need some years for me to learn all these stuffs and produce a few courses...maybe 10 years given I have a family and not rich enough to fire my boss?


When I was in college, we took a hybrid approach. We used a real CPU (6809), but instead of using a real computer (since 6809-based computers were not as common as Z80 or 6502-based ones), we used an educational kit designed and built by the professor who taught the course. This kit could be programmed via the serial console using the built-in debugger (or monitor, as they were called back then).


I did an EE degree and the sophmore firmware course used an 8 bit PIC (or a motorola 6800 a few years before). About a third of the semester was entirely in PIC assembly, then C, then advanced stuff in C. I think your idea is pretty viable, and sounds more fun than the comp arch class offered.


Thanks. It's going to put a hell lot of work on the professors so it's probably more suitable for some people like Gustav (the pikuma guy).


The more I think about AI the more often I get back to what little time humans have. Besides a tad useless your course would be a ton of fun but when I looked in my agenda I couldn't find a few years to spend on it.


I think it's going to be more useful once they get pass the first two courses when they go for more advanced courses. I'd argue 6502 or 8086 is still more useful than LC-3 which is frequently used in CompArch classes.

The thing about advanced courses are: all production material more or less went through 30-40 years of evolution so it's impossible to get students to jump into it directly. Few people can hack modern gcc or Linux kernals nowadays. We can only go back some 30 years and work on the machines then to get some ideas.

Yeah it takes a lot of time though, plus it's just a concept, not real courses -- I myself is a very mediocre programmer.


Kind of like your idea in real life. The Vectrex is used in a college in Germany. Very fun projects result.

https://itwww.hs-pforzheim.de/daten/mitarbeiter/johannsen/ve...


If I were king of the forest, there would be a degree program in Comparative Coding (comparative lit for code).

We don’t treat this as a creative discipline and thus we don’t spend time looking at masters, how they were successful and the ways they were just as human as anyone.

Someone would get paid for writing stuff like this, same as people getting paid to write about Poe, Cummings, Hemingway, Thoreau.

But I guess the problem is that the way we price teaching and coding, the gap is far too wide between talking about and doing, unless you do it as a side hustle/hobby.


Should people get paid to write about Poe, Cummings, Hemingway, Thoreau? The original works themselves are pretty accessible. People can just read them and form their own interpretations. I'm skeptical that I really gained anything from reading literary criticism, at least it didn't make me appreciate the original works more or make me a better writer. I understand that there's a long tradition of scholarship in comparative literature and I'm not trying to be anti-intellectual or dismissive of an entire field of study, but maybe we should examine our assumptions and consider whether we're getting a good value from paying academics to do that work? Is it possible that society would be better off if we paid them to do something else, like maybe write new literature?


I don't actually care one way or another about that particular subject, but if you start making judgements about academic fields of study are worthwhile to pay for on the basis of value to society, you allow other people -- more ignorant people, let's say -- with different concepts of societal value to make somewhat unfortunate decisions. You might end up without any new literature at all.


Very little literature is written by academics. People who want to write new literature can just go ahead and do it.

In a world of limited time and resources we always make judgements about which academic fields of study are worthwhile. At some level it's a zero-sum game: time spent on comparative literature is time not spent on philosophy or history or creative writing. So, given that we have to prioritize which people to pay and which courses to require, how should we make those decisions?


> At some level it's a zero-sum game

To live a life thinking this way outside of work would fill me with existential despair. Life well lived is more than a mere exercise in market efficiency.

Academia isn't supposed to maximize our return on investment, and the people who made this their lives work didn't choose the field because it was the highest pay they could obtain or the thing which contributed the most to the orphan crushing machine (the economy).


As a society with limited resources we can't pay an infinite number of academics. If we pay more of them to do comparative literature then that necessarily means we pay fewer to do other stuff like history or philosophy. So, given that we need to make hard choices, which criteria should we use? It doesn't have to be ROI but there has to be some kind of rational basis for making decisions.

This is how things work in the real world. Enjoy your existential despair.


> So, given that we need to make hard choices, what criteria should we use?

That's a great question. I suppose we should start by asking ourselves who is currently paying them. It seems to me that would mostly be the for profit colleges who offer liberal arts degrees.

So far, letting "the free market" decide seems to be working for the most part. Although I am concerned that people are forgetting the value of having an educated popluace, and conversations like this one reinforce that fear. Do you know that 20% of Americans are functionally illiterate and the 54% read below the sixth grade level now?

It sounds like there is a quiet part you aren't saying out loud here, and I'm curious what it is? Do you feel that the government should be limiting access to education in some way?


On the contrary, I don't think government should be limiting access to education. But for publicly funded schools we should continuously re-evaluate the optimal required (i.e. non-elective) curriculum. Students only have limited time available to learn. Time spent on one subject like comparative literature is time not spent on other subjects like philosophy or statistics or foreign languages. So, given that it's a zero-sum game, what is the optimal amount of time spent on comparative literature? At some point you have to put aside the platitudes about "knowledge for knowledge's sake" and get to specifics about how many teachers to hire in each academic field.

There are very few for-profit colleges that offer liberal arts degree. Almost all colleges that pay academics to teach comparative literature are government or non-profit institutions. (Sure, you can buy an English degree of dubious quality from a for-profit school like University of Phoenix but the numbers there are tiny.)

Illiteracy is a symptom of a fundamental breakdown in the primary education system before students are even exposed to more complex liberal arts subjects like comparative literature. So I have no idea what point you're trying to make there.


You are technically correct, which is the best kind of correct. I concede the "zero-sum" point, thanks for taking the time to discuss it.

That's said, I still feel there is room to discuss which outcome we are trying to optimize for when we discuss the cost benefit of education and how to allocate those resources. Is the goal profit for employers, well adjusted complete cotizens who understand the world they live in, or something else?

I do suspect that our current attempts to laser focus on the skills most valuable to employers are short sighted, and frankly might be damaging society almost as much as the utter failure of primary education. That is why I mentioned the illiteracy issue before.

If only a minority can read well, and we are trying to "optimize" the learning outcomes of the remaining smaller percentage of competent students, we leave only a tiny percentage of well rounded and capable thinkers.


All knowledge is worthwhile. Its that easy. If you want to distinguish “useful” knowledge everything will eventually get thrown out. Why should people spend time and money investigating distant galaxies for example? Its completely useless for 99.999999 of all people. At best it results in “huh, neat” when reading about some new discovery about some star.


Your comment is bunk: should society also pay for people's hobbies? Should we spend 100% of our economic output on humanities?


I should be clearer. I believe in the value of hobbies - but perhaps because my childhood hobbies were geekily technical.

I also perhaps shouldn't use absurdity to argue against illogical statements.


Python is pretty accessible, too... Should we pay people to write it?

I had some great comp lit courses in undergrad, which exposed me to some really fascinating work that I wouldn't have found otherwise.

Especially a course on literature of the Caribbean, which looked at a combination of contemporary work and older Western lot which are typical reference points: Robinson Crusoe and the Tempest. A good course brings together interesting world, and provides some structured discussion of both history and culture. You get more out of it through discussion with both the Prof and other students.

These courses at the undergrad level also create more writing practice on diverse topics, which is critical for developing writing skills generally.

I went on to get a PhD in mathematics, and now do machine learning for conservation, collaborating with folks all over the globe. Understanding the shape and impact of colonialism has been important for my subsequent work.


My English Lit friend definitely was aware of a trap of learning to pick apart literature before you can create your own. And then fell into it anyway. I suppose it’s like medical students and the DSM, sometimes it just freaks you out instead of informing.

It should probably be an MS class or a senior level class.


Some people do teach this type of content and do a pretty good job at it. DisplacedGamers on youtube is a great channel, Gustavo Pezzi from pikuma.com is excellent, and there is also Ben Eater also covering 6502 programming, just to name a few.


Having greatly enjoyed Ben Eater's content (especially the build-your-own-VGA!), I'll definitely check those others out. Thanks!


I was talking to a friend who works in development and he says that they even frown on recursion now. They think it's too complex and causes junior programmers to footgun.

It's a shame because recursive code is often the most clean, elegant and fast for things like traversing trees.


One of my peeves with JavaScript is that I feel the reduce() function has its arguments in the wrong order and increases cognitive load. Elixir’s is “right”. I’ve never been that much of a fan of recursion myself. Iteration and list comprehension for me.


I don't really write code that traverse trees that often these days. I do use libraries or services (e.g. SQL databases) that do though.

And I have no idea whether the implementation of map/reduce I use is implemented as a recursive function or not, I just use it.

If your friend works with implementing libraries or database engines I would be a bit shocked. If he implements applications and services I could agree that keeping things simple and not inventing your own frameworks could be a good idea.


The MIT "platform studies" series is quite close to that, http://platformstudies.com/


There's a famous annotated version of Unix V6 that falls into this category. I heard that it had copyright issues for years, though. It'd be interesting to see people write more of these about open source code.

https://cs3210.cc.gatech.edu/r/unix6.pdf


The map overview is so well done. The comments are so nice and thoroughly explaining what is stored in which register and what's being manipulated.

I wish all codebases were like this. This must have taken months to document it.

[1] https://elite.bbcelite.com/c64/articles/map_of_the_source_co...

[2] https://elite.bbcelite.com/c64/main/subroutine/tactics_part_...


Glad you like it! I’ve been working on this for nearly five years, along with my other software archaeology projects - I started in May 2020, back in lockdown. It’s a true labour of love.

Here’s a timeline of the project so far:

https://elite.bbcelite.com/about_site/site_history.html

I’m sure it will keep growing, there’s always something more to write about…


The "Advanced User Guide" referred to several times in the comments is The Advanced User Guide for the BBC Micro by Bray, Dickens and Holmes, 1983, ISBN 0 946827 00 1 . https://stardot.org.uk/forums/viewtopic.php?f=42&t=17242 https://archive.org/details/bbc-micro-advanced-user-guide/ I can't find a source for this now, but IIRC Bell and Braben themselves used this when writing Elite. (I am not an expert.)


Thank you, that's a good point. The site started off with just the BBC Micro version, and I never expanded the book references to be clearer.

Anyway, I have reworded every reference to books in the commentary so they now contain the full title and author list, so hopefully people will be able to find the relevant tomes. I've also linked to the books from my "useful links" page in a more prominent manner:

https://elite.bbcelite.com/about_site/useful_links.html

I hope that helps!


The game elite (on my apple iie) played a big role in me being interested in 3d graphics (most importantly, hidden line removal). I was really curious as a high school student how the game achieved what it did, and it wasn't until later that I read a few interviews with the authors (and later, the source code) that I began to appreciate just how unprepared I was back then to write a first-class game.

Beyond that it was a great game to play. I used to buy drugs on poor agricultural planets, then sell them for massive profits on rich industrial ones, then buy illegal weapons and sell them back on the poor agricultural planets. Starfighting was quite good in terms of frame rate and enemy AI. At some point, hundreds of hours into the game, I was offered a unique mission to recover a spacecraft...


Have to mention the new Commodore VIC-20 version of Elite: https://www.youtube.com/watch?v=3J0xRcux1FI


"This site contains the original 1980s source code for the classic space game Elite, with every single line documented and (for the most part) explained. It is literally the original 6502 assembly source code, just heavily commented."

For those wondering what Elite was.


The site also now has a disassembly of !Lander, which in hindsight is an amazing demo (that begat Zarch / Virus) - super smooth 3D in 1987 on a “micro”. As a kid in the early ‘90s I didn’t appreciate it, it was that exploding spaceship thing you could run on the computers at school. But in hindsight, wow that ARM and VIDC could be amazing.


Thanks for the Lander pointer. Virus blew my mind when I first saw it, those decades ago. Now to restrain myself from hours in the source code.


Alexander the Ok, on youtube, has an amazing video on this. It is not quite as technical as the webiste for obvious reasons, but great overview and historical facts as well.

https://www.youtube.com/watch?v=lC4YLMLar5I




The Teletext variant is amazing




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: