Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
You are not logged in 
CSDb User Forums


Forums > C64 Coding > Smallest(?) 6502 chess program
2020-09-12 09:58
Rastah Bar

Registered: Oct 2012
Posts: 231
Smallest(?) 6502 chess program

A friend of mine pointed me out to https://nanochess.org/chess6.html

There is also a 6502 version https://github.com/nanochess/Atomchess-6502/blob/master/toledo_..

No unitended opcodes are used. Surely this can be further optimized.
F.e. lines 178+178 can be replaced by LAX #0, lines 189-191 by LAX #0, DEX, TXS

The loop in lines 193-196 can be replaced by
        ldx #$8b     ; ...copy in X
sr0:    sta 0,X      ; Save in address 0 plus X
        dex          ; Increment X
        bmi sr0      

So if you have some spare time and are looking for a nice little challenge ...
2020-09-12 10:50
iAN CooG

Registered: May 2002
Posts: 2799
> dex ; Increment X

now I'm confused =)
2020-09-12 11:03
Rastah Bar

Registered: Oct 2012
Posts: 231
Quote: > dex ; Increment X

now I'm confused =)


Sorry, i copied the original comments.
2020-09-12 14:12
JackAsser

Registered: Jun 2002
Posts: 1720
Quote: > dex ; Increment X

now I'm confused =)


Only valid if x=0. :)
2020-09-28 00:17
wil

Registered: Jan 2019
Posts: 14
The program looks interesting, is there known how well (ELO equivalent) it would play on a C64?


Replacing the original code lines
        ldx #$80     ; ...copy in X
sr0:    sta 0,X      ; Save in address 0 plus X
        inx          ; Increment X
        cpx #$8c
        bne sr0      ; Repeat until X is zero.

by the suggested version in the post should work, since there is this line afterwards
        tax          ; x is zero

thus the state of X and the NZ flags at the end of the loop do not matter because they get overwritten by the tax command. Carry could still be an issue, but it does not look like the program is counting on the carry being set after the loop.

Referring to the invalidated copied comment, the code can be shortened much further...




...by removing a lot of unnecessary comments. The program seems to be severely overcommented, probably addressing readers who are not familiar with the 6502 architecture.
2020-09-28 03:53
ChristopherJam

Registered: Aug 2004
Posts: 1081
TAX doesn't affect flags, but that's ok because the following code doesn't care what N or Z are anyway.

If we broaden the fill to cover the 80 bytes of board, then the loop in lines 200-212 only needs to set two bytes of every ten to '7', the rest will already be cleared. Hence, we can instead use something like
    ldx#80
:   lda#7
    sta board-10+8,x
    sta board-10+9,x
    txa
    sbx#10
    bne :-

- a reduction from 22 to 13 bytes.

Sadly the next routine then grows by a byte, as we no longer have 7 in A to use to initialise X.

But yes, plenty of scope for optimization in this one :)
2020-09-28 10:27
Rastah Bar

Registered: Oct 2012
Posts: 231
To be fair, the author is an x86 specialist and that version was ported to 6502.

@wil: Playing strength will be very weak. Nowhere near the strength of Colossus Chess. The program only allows promotion to a queen, so it is not even complete.

He has written as small chess program in C as well that is estimated to have a strength "something around 1400 (level 1) to 1600 ELO (level 3)."
https://nanochess.org/chess2.html
2020-09-28 11:07
ChristopherJam

Registered: Aug 2004
Posts: 1081
Oh no shade intended against the author. It's an impressive feat that it even exists, especially given the author's background.
2020-09-28 11:38
Rastah Bar

Registered: Oct 2012
Posts: 231
Yes, very impressive! But I could have stressed that a bit more in my original post. The author has won several IOCCC competitions, so definitely a top-knotch coder.
2020-09-28 15:45
wil

Registered: Jan 2019
Posts: 14
Quote: TAX doesn't affect flags, but that's ok because the following code doesn't care what N or Z are anyway.

If we broaden the fill to cover the 80 bytes of board, then the loop in lines 200-212 only needs to set two bytes of every ten to '7', the rest will already be cleared. Hence, we can instead use something like
    ldx#80
:   lda#7
    sta board-10+8,x
    sta board-10+9,x
    txa
    sbx#10
    bne :-

- a reduction from 22 to 13 bytes.

Sadly the next routine then grows by a byte, as we no longer have 7 in A to use to initialise X.

But yes, plenty of scope for optimization in this one :)


TAX (and the other transfer commands) affect the N and Z flags.

For example:

   lda #00 ;Z flag is set after this command
   ldy #01 ;Z flag is cleared after this 
   tax     ;Z flag is set again
2020-09-28 18:21
ChristopherJam

Registered: Aug 2004
Posts: 1081
*checks reference guides.. writes test code too..*

Wait, I do apologize, you are indeed correct.

How embarrassing. Not sure how that misapprehension on my part hasn't bitten me in the past 30 years, but anyway. Still, hopefully some new code optimization opportunities now lie in my future, thank you :)
RefreshSubscribe to this thread:

You need to be logged in to post in the forum.

Search the forum:
Search   for   in  
All times are CET.
Search CSDb
Advanced
Users Online
Shocker/ONS/sidDivers
E$G/HoKuTo FOrCE
Mr Scoly
iceout/Avatar/HF
Guests online: 91
Top Demos
1 Uncensored  (9.7)
2 Edge of Disgrace  (9.6)
3 Coma Light 13  (9.6)
4 Memento Mori  (9.6)
5 Comaland 100%  (9.6)
6 The Shores of Reflec..  (9.6)
7 Unboxed  (9.6)
8 Lunatico  (9.6)
9 Remains  (9.5)
10 C=Bit 18  (9.5)
Top onefile Demos
1 Dawnfall V1.1  (9.5)
2 Gumbo Revised  (9.5)
3 Smile to the Sky  (9.5)
4 Daah, Those Acid Pil..  (9.5)
5 Bad Boy  (9.5)
6 Crystal Gazer  (9.5)
7 Cuarentenauta  (9.5)
8 Instinct  (9.5)
9 The Night It Snowed  (9.5)
10 Rewind  (9.4)
Top Groups
1 PriorArt  (9.4)
2 Booze Design  (9.4)
3 Censor Design  (9.4)
4 Fossil  (9.4)
5 Performers  (9.3)
Top Cover Designers
1 Duce  (9.8)
2 Electric  (9.8)
3 Junkie  (9.7)
4 The Elegance  (9.4)
5 Mermaid  (9.3)

Home - Disclaimer
Copyright © No Name 2001-2020
Page generated in: 0.041 sec.