Skip to content

Instantly share code, notes, and snippets.

@ruanpetterson
Last active July 16, 2021 15:39
Show Gist options
  • Save ruanpetterson/b5e34dd57fb02e9df1fabbfa357b43f2 to your computer and use it in GitHub Desktop.
Save ruanpetterson/b5e34dd57fb02e9df1fabbfa357b43f2 to your computer and use it in GitHub Desktop.
[Ben Eater] Build a 6502 computer
#include <stdio.h>
#include <unistd.h>
#define SIZE 0x8000
int main() {
FILE *fp = fopen("rom.bin", "w");
if (fp != NULL) {
unsigned char rom[SIZE];
unsigned char code[] = {
0xa9, 0xff, // lda #$f
0x8d, 0x02, 0x60, // sta $6002
0xa9, 0x55, // lda #$55
0x8d, 0x00, 0x60, // sta $6000
0xa9, 0xaa, // lda #$aa
0x8d, 0x00, 0x60, // sta $6000
0x4c, 0x05, 0x80, // jmp $8005
};
for (int i = 0; i < sizeof(code); i++) {
rom[i] = code[i];
}
for (int i = sizeof(code); i < SIZE; i++) {
rom[i] = 0xea;
}
rom[0x7ffc] = 0x00;
rom[0x7ffd] = 0x80;
fwrite((char *)rom, 1, SIZE, fp);
fclose(fp);
return 0;
}
return 1;
}
#include <stdio.h>
#include <fstream>
#define SIZE 0x8000
//
// Please see this video for details:
// https://www.youtube.com/watch?v=yl8vPW5hydQ
//
int main() {
std::ofstream file;
file.open("rom.bin", std::ios::binary | std::ios::out);
if (file.is_open()) {
unsigned char rom[SIZE];
unsigned char code[] = {
0xa9, 0xff, // lda #$f
0x8d, 0x02, 0x60, // sta $6002
0xa9, 0x55, // lda #$55
0x8d, 0x00, 0x60, // sta $6000
0xa9, 0xaa, // lda #$aa
0x8d, 0x00, 0x60, // sta $6000
0x4c, 0x05, 0x80, // jmp $8005
};
for (int i = 0; i < sizeof(code); i++)
rom[i] = code[i];
std::fill(&rom[sizeof(code)], &rom[SIZE], 0xea);
rom[0x7ffc] = 0x00;
rom[0x7ffd] = 0x80;
file.write((char*)rom, sizeof(rom));
file.close();
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
#
# Please see this video for details:
# https://www.youtube.com/watch?v=yl8vPW5hydQ
#
code = bytearray([
0xa9, 0xff, # lda #$ff
0x8d, 0x02, 0x60, # sta $6002
0xa9, 0x55, # lda #$55
0x8d, 0x00, 0x60, # sta $6000
0xa9, 0xaa, # lda #$aa
0x8d, 0x00, 0x60, # sta $6000
0x4c, 0x05, 0x80, # jmp $8005
])
rom = code + bytearray([0xea] * (32768 - len(code)))
rom[0x7ffc] = 0x00
rom[0x7ffd] = 0x80
with open("rom.bin", "wb") as out_file:
out_file.write(rom)
@ruanpetterson
Copy link
Author

Please see this video for details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment