I made a chess program in under 1K of 6502 assembly that runs on an unexpended KIM-1

Discussion of anything and everything relating to chess playing software and machines.

Moderator: Ras

User avatar
maksimKorzh
Posts: 775
Joined: Sat Sep 08, 2018 5:37 pm
Location: Ukraine
Full name: Maksim Korzh

I made a chess program in under 1K of 6502 assembly that runs on an unexpended KIM-1

Post by maksimKorzh »

Hey what's up guys, Code Monkey King's here.
I'd like to present a new project I've just completed -
it's a chess program written in 6502 assembly that takes only 909 bytes of KIM-1 RAM when loaded:
DATA SEGMENT: 192 bytes (board, move offsets, etc.)
CODE SEGMENT: 717 bytes (search, make move & display)

Project repo and readme: https://github.com/maksimKorzh/6502-chess
Source in 6502 assembly: https://github.com/maksimKorzh/6502-che ... /chess.asm
Source in KIM-1 Paper Tape format: https://github.com/maksimKorzh/6502-che ... /chess.ptp
Prototype in C: https://github.com/maksimKorzh/6502-che ... rc/chess.c

As a development environment I've been using a KIM-1 emulator I've made eariler:
https://maksimkorzh.github.io/KIM-1/
(6502 CPU emulator and assembler are 3rd party projects, I just bundled them together to emulate and develop for KIM-1)

There's also a mobile version with pre-loaded chess on it:
https://maksimkorzh.github.io/KIM-1/mob ... chess.html

Finally you can have a look how I play the whole game of chess vs this engine:


Looking forward to your feedback)
User avatar
algerbrex
Posts: 608
Joined: Sun May 30, 2021 5:03 am
Location: United States
Full name: Christian Dean

Re: I made a chess program in under 1K of 6502 assembly that runs on an unexpended KIM-1

Post by algerbrex »

maksimKorzh wrote: Sun Jun 12, 2022 3:39 pm Hey what's up guys, Code Monkey King's here.
I'd like to present a new project I've just completed -
it's a chess program written in 6502 assembly that takes only 909 bytes of KIM-1 RAM when loaded:
DATA SEGMENT: 192 bytes (board, move offsets, etc.)
CODE SEGMENT: 717 bytes (search, make move & display)

Project repo and readme: https://github.com/maksimKorzh/6502-chess
Source in 6502 assembly: https://github.com/maksimKorzh/6502-che ... /chess.asm
Source in KIM-1 Paper Tape format: https://github.com/maksimKorzh/6502-che ... /chess.ptp
Prototype in C: https://github.com/maksimKorzh/6502-che ... rc/chess.c

As a development environment I've been using a KIM-1 emulator I've made eariler:
https://maksimkorzh.github.io/KIM-1/
(6502 CPU emulator and assembler are 3rd party projects, I just bundled them together to emulate and develop for KIM-1)

There's also a mobile version with pre-loaded chess on it:
https://maksimkorzh.github.io/KIM-1/mob ... chess.html

Finally you can have a look how I play the whole game of chess vs this engine:


Looking forward to your feedback)
Cool project Maksim! One of the next projects I'm planning on doing is actually an emulator, for chip-8 though.

I poked around a little bit in the asm source code as I've never really tried to write a serious program in only assembly before, so I was curious what that would even look like.

I may try playing against it a little later today. What would you say the biggest challenge was in trying to convert chess programming ideas to a much lower level language like assembly?
User avatar
maksimKorzh
Posts: 775
Joined: Sat Sep 08, 2018 5:37 pm
Location: Ukraine
Full name: Maksim Korzh

Re: I made a chess program in under 1K of 6502 assembly that runs on an unexpended KIM-1

Post by maksimKorzh »

algerbrex wrote: Sun Jun 12, 2022 4:15 pm
maksimKorzh wrote: Sun Jun 12, 2022 3:39 pm Hey what's up guys, Code Monkey King's here.
I'd like to present a new project I've just completed -
it's a chess program written in 6502 assembly that takes only 909 bytes of KIM-1 RAM when loaded:
DATA SEGMENT: 192 bytes (board, move offsets, etc.)
CODE SEGMENT: 717 bytes (search, make move & display)

Project repo and readme: https://github.com/maksimKorzh/6502-chess
Source in 6502 assembly: https://github.com/maksimKorzh/6502-che ... /chess.asm
Source in KIM-1 Paper Tape format: https://github.com/maksimKorzh/6502-che ... /chess.ptp
Prototype in C: https://github.com/maksimKorzh/6502-che ... rc/chess.c

As a development environment I've been using a KIM-1 emulator I've made eariler:
https://maksimkorzh.github.io/KIM-1/
(6502 CPU emulator and assembler are 3rd party projects, I just bundled them together to emulate and develop for KIM-1)

There's also a mobile version with pre-loaded chess on it:
https://maksimkorzh.github.io/KIM-1/mob ... chess.html

Finally you can have a look how I play the whole game of chess vs this engine:


Looking forward to your feedback)
Cool project Maksim! One of the next projects I'm planning on doing is actually an emulator, for chip-8 though.

I poked around a little bit in the asm source code as I've never really tried to write a serious program in only assembly before, so I was curious what that would even look like.

I may try playing against it a little later today. What would you say the biggest challenge was in trying to convert chess programming ideas to a much lower level language like assembly?
Thank you, it's a very good question.
The biggest issue for me was to implement 12 local variables on a stack and fetch the return value from the previous stack frame - since I didn't know for sure how that should be done it was intriguing whether it works or not. Apart from this fitting evaluation values into 8 bits was a real pain but even harder thing was to make a signed comparison between a current and the best scores.

CHIP-8 is a good idea, good luck with that!
User avatar
algerbrex
Posts: 608
Joined: Sun May 30, 2021 5:03 am
Location: United States
Full name: Christian Dean

Re: I made a chess program in under 1K of 6502 assembly that runs on an unexpended KIM-1

Post by algerbrex »

maksimKorzh wrote: Sun Jun 12, 2022 6:14 pm CHIP-8 is a good idea, good luck with that!
Thanks, it should be a fun project. I remember I started one a couple of years ago but never finished it. I'll probably use it as an excuse to learn a new language. I'm thinking either Rust, or a functional programming language like Ocaml, since I don't know either of those very well.

Either way though, congrats!
User avatar
maksimKorzh
Posts: 775
Joined: Sat Sep 08, 2018 5:37 pm
Location: Ukraine
Full name: Maksim Korzh

Re: I made a chess program in under 1K of 6502 assembly that runs on an unexpended KIM-1

Post by maksimKorzh »

algerbrex wrote: Sun Jun 12, 2022 6:37 pm
maksimKorzh wrote: Sun Jun 12, 2022 6:14 pm CHIP-8 is a good idea, good luck with that!
Thanks, it should be a fun project. I remember I started one a couple of years ago but never finished it. I'll probably use it as an excuse to learn a new language. I'm thinking either Rust, or a functional programming language like Ocaml, since I don't know either of those very well.

Either way though, congrats!
Thank you)
User avatar
mvanthoor
Posts: 1784
Joined: Wed Jul 03, 2019 4:42 pm
Location: Netherlands
Full name: Marcel Vanthoor

Re: I made a chess program in under 1K of 6502 assembly that runs on an unexpended KIM-1

Post by mvanthoor »

maksimKorzh wrote: Sun Jun 12, 2022 3:39 pm Looking forward to your feedback)
This is both a completely useless but also completely epic project. I'd personally never write something like this, but as an embedded software engineer, I can appreciate it :) (Did you also write the emulator yourself? In that case it would be very hard to find out of something that goes wrong is actually wrong in the chess program, or if the problem is in the emulator...)

I'll certainly go and take a look at this series.
Author of Rustic, an engine written in Rust.
Releases | Code | Docs | Progress | CCRL
User avatar
hgm
Posts: 28426
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: I made a chess program in under 1K of 6502 assembly that runs on an unexpended KIM-1

Post by hgm »

As if writing a new program for orthodox chess for PC or Mac is anything less than useless... This is why they call it a 'hobby'. :roll:
User avatar
maksimKorzh
Posts: 775
Joined: Sat Sep 08, 2018 5:37 pm
Location: Ukraine
Full name: Maksim Korzh

Re: I made a chess program in under 1K of 6502 assembly that runs on an unexpended KIM-1

Post by maksimKorzh »

hgm wrote: Mon Jun 13, 2022 3:16 pm As if writing a new program for orthodox chess for PC or Mac is anything less than useless... This is why they call it a 'hobby'. :roll:
Mr. Muller don't you like how your MicroMax 3 nested loop idea for generating moves fits into KIM-1?
And also exactly thankfully to you I've considered signed comparison of score and best score!
User avatar
maksimKorzh
Posts: 775
Joined: Sat Sep 08, 2018 5:37 pm
Location: Ukraine
Full name: Maksim Korzh

Re: I made a chess program in under 1K of 6502 assembly that runs on an unexpended KIM-1

Post by maksimKorzh »

mvanthoor wrote: Mon Jun 13, 2022 2:13 pm
maksimKorzh wrote: Sun Jun 12, 2022 3:39 pm Looking forward to your feedback)
This is both a completely useless but also completely epic project. I'd personally never write something like this, but as an embedded software engineer, I can appreciate it :) (Did you also write the emulator yourself? In that case it would be very hard to find out of something that goes wrong is actually wrong in the chess program, or if the problem is in the emulator...)

I'll certainly go and take a look at this series.
Hi Marcel, thank you for your feedback.
Yes I made the emulator, but I've bundled existing 6502 CPU emulator. As for emulator's accuracy - it runs most of the original KIM-1 software including KIM venture by Bob Leedom which no other existing emulator I know can handle. I'm in touch with a friend owning a couple of original KIMs and a few modern replicas like Corsham KIM and PAL-1. He has tested my program on all the hardware he has and it works exactly the same as on the emulator.
User avatar
Rebel
Posts: 7435
Joined: Thu Aug 18, 2011 12:04 pm
Full name: Ed Schröder

Re: I made a chess program in under 1K of 6502 assembly that runs on an unexpended KIM-1

Post by Rebel »

Nice to see the good old 6502 code.

One thing

Code: Select all

LDA BOARD,Y      ;      occupied by pieces
CMP #$00         ;
BNE SCR          ;
One nice thing of the 6502 is after a load into memory the Zero Flag and Negative Flag are automatically set, meaning you can skip the CMP #$00 instruction.
90% of coding is debugging, the other 10% is writing bugs.