Elite BeebAsm Source

Got a programming project in mind? Tell everyone about it!
Post Reply
User avatar
kieranhj
Posts: 690
Joined: Sat Sep 19, 2015 10:11 pm
Location: Farnham, Surrey, UK
Contact:

Elite BeebAsm Source

Post by kieranhj » Fri Jul 13, 2018 9:25 am

I thought it had been too long since I gave you any presents so here is an initial port of the BBC Elite source code from BBC BASIC to BeebAsm. It can be built on a modern PC development environment to produce binary identical exexcutables. Everything is in GitHub here: https://github.com/kieranhj/elite-beebasm.

Some caveats at this point:
  • The BBC Cassette source built for DFS has been ported for simplicity to begin with as it only generates two executables (loader and main game)
  • Python is required to complete the checksum and encryption parts of the build process previously done in BBC BASIC
  • The build process is brittle so modifications are not yet possible - the checksum & encrytions scripts are likely to fail due to hard coded symbols
  • The game source is still incredibly terse (i.e. unreadable) but is at least has one instruction per line (all 10,000 of them)
I am very open to PR's if anyone would like to improve the readability of the source files to add whitespace after the assembler instructions, rename the variables and labels to something more readable, identify and document important functions etc. I'm sure the non-Windows folks will want to improve the build process for Linux etc. as well, please do so.

Happy Friday everyone!
Bitshifters Collective | Retro Code & Demos for BBC Micro & Acorn computers | https://bitshifters.github.io/

User avatar
kieranhj
Posts: 690
Joined: Sat Sep 19, 2015 10:11 pm
Location: Farnham, Surrey, UK
Contact:

Re: Elite BeebAsm Source

Post by kieranhj » Fri Jul 13, 2018 5:19 pm

Quick update, I've improved the whitespace so assembler instructions are legible and all labels are on their own line. If anyone feels like identifying and renaming labels to something more meaningful then please feel free. :)
Bitshifters Collective | Retro Code & Demos for BBC Micro & Acorn computers | https://bitshifters.github.io/

chrisn
Posts: 368
Joined: Sat Apr 19, 2014 11:31 am
Location: UK
Contact:

Re: Elite BeebAsm Source

Post by chrisn » Fri Jul 13, 2018 5:51 pm

This is great! =D> A while ago I started annotating some of the source code. Will dig out my notes and see if I can contribute anything.

tom_seddon
Posts: 127
Joined: Mon Aug 29, 2005 11:42 pm
Contact:

Re: Elite BeebAsm Source

Post by tom_seddon » Fri Jul 13, 2018 7:24 pm

Great stuff! I was going to do some stuff with the Elite code a couple of years ago, but the prospect of making it build on the PC was just too daunting, and the source was barely more comprehensible than a disassembly anyway.

I did convert the 2nd processor I/O code to dasm format, producing nearly byte-identical output to the file on the executive version disk - the differences were in some of the pregenerated tables (no obvious side effects). Shouldn't be hard to dig it out and make it build with BeebAsm... (I also got it building a useable loader, though I don't remember why and I'm not sure I bothered making that bit a byte-for-byte match.)

--Tom
Last edited by tom_seddon on Fri Jul 13, 2018 7:25 pm, edited 1 time in total.

User avatar
tricky
Posts: 2559
Joined: Tue Jun 21, 2011 8:25 am
Contact:

Re: Elite BeebAsm Source

Post by tricky » Fri Jul 13, 2018 10:42 pm

Thanks Kieran, top man (not the shop).

User avatar
kieranhj
Posts: 690
Joined: Sat Sep 19, 2015 10:11 pm
Location: Farnham, Surrey, UK
Contact:

Re: Elite BeebAsm Source

Post by kieranhj » Tue Jul 17, 2018 11:42 am

A small update: I've commented the loader source so I can remove the checksums / encryption / tamper protection.

For those interested, this is what the Elite loader does - some sneaky stuff in here!

\ ENTRY code at &20C9
\ Disable all interupts
\ Set USERV, BRKV, IRQ2V and EVENTV to point to machine reset (&FFFC)
\ Ensure all vectors are pointing into MOS (&C000 and upwards)
\ Make NETV an RTS
\ Poke JSR into David2 fn and set ADC to sample 2 channels
\ Poke PHA into PROT1 fn
\ Issue *TV 255,0 command (via OSBYTE 144)
\ Remove BREAK intercept (via OSBYTE 247)
\ Set ADC to 8-bit conversion (via OSBYTE 190)
\ Issue paged ROM service call (via OSBYTE &8F)
\ Disable output buffer empty event (via OSBYTE 13)
\ Set character status flag (via OSBYTE 225)
\ Get address of MOS key translation table (via OSBYTE 172)
\ Disable ESCAPE, memory cleared on BREAK (via OSBYTE 200)
\ Disable character entering keyboard buffer event (via OSBYTE 13)
\ Reset stack
\ Push 33 bytes from BEGIN% onto the stack - two functions:

\\ DOMOVE fn as running on the stack
\\
\\ 01DF : LDA (70),Y
\\ 01E1 : EOR 2086,Y
\\ 01E4 : STA (72),Y
\\ 01E6 : DEY
\\ 01E7 : BNE 01DF
\\ 01E9 : INC 73
\\ 01EB : INC 71
\\ 01ED : DEX
\\ 01EE : BNE 01DF
\\ 01F0 : RTS

\\ Copy BLOCK fn to stack and decrypt TUT fn
\\
\\ 01F1 : PLA
\\ 01F2 : PLA
\\ 01F3 : LDA 0C7C,Y ; BLOCK
\\ 01F6 : PHA
\\ 01F7 : EOR 0BAD,Y ; TUT
\\ 01FA : STA 0BAD,Y
\\ 01FD : JMP (20AD)

\ NB. loops by indirecting through 5 branches!
\ Issue 67 VDU commands to set up square MODE4 screen at &6000
\ Disable cursor editing, keys give ASCII values (via OSBYTE 4)
\ Disable flashing colours (via OSBYTE 9)
\ Poke JSR indirect into crunchit fn
\ Define 4x sound envelopes (via OSWORD 8 )

\ Move & decrypt WORDS9 to language workspace (4x pages from &1100 to &0400)
\ Move & decypt P.ELITE to screen (1x pages from &1D00 to &6300)
\ Move & decypt P.A-SOFT to screen (1x pages from &1E00 to &6100)
\ Move & decypt P.A-SOFT to screen (1x pages from &1F00 to &7600)
\ Draw Saturn
\ Move & decypt DIALSHP to screen (1x pages from &1500 to &7800)
\ Move & decypt 2x pages of code from UU% down to &0B00
\ NB. Above decryptions are all EOR'd against loader code to prevent tampering

\ Poke BRK into OS01 fn and call OSBPUT with file handle Y=0...
\ By now the address &01F1 has been sneakily placed into BPUTV (&218)
\ So this call to OSBPUT actually calls the second function placed in stack and never returns...

\ Copies code between BLOCK and ENDBLOCK to stack and decrypt ENTRY2 code
\ Enable interupts and set IRQ1V to IRQ1 fn
\ Read key with time limit (via OSBYTE &81)
\ Address of ENTRY2 already pushed onto stack at start of BLOCK code so RTS...

\ ENTRY2 at &BC3
\ Set OSPRNT vector to TT26 fn
\ Issue OSCLI command in MESS1 "*L.ELTcode 1100"
\ Execute CHECKER fn but version copied into stack:

\\ Verify MAINSUM of WORDS9 = 4x pages from &400 to &800
\\ Verify (hard coded) checksum of LBL fn in elite-bcfs.asm (ELThead)
\\ Call LBL fn in elite-bcfs.sm (ELThead) to verify loader code with CHECKbyt checksum

\ Issue *TAPE command (via OSBYTE 140)
\ Decrypt and copy down all ELITE game code from &1128 to &F40 (80x pages)
\ This decryption is EOR'ing with an incrementing counter (0-255) in sequence
\ Set BRKV and WRCHV to point at BR1 and TT26 fns in elite-source.asm
\ Calls final boot code by issuing RTS (address &163 already copied to stack)

\ Disable SYSVIA interrupts for Timer2, CB1, CB2, CA2
\ Set IRQ1V to IRQ1 fn in elite-source.asm & set Timer 1 Counter Hi value
\ Disable ESCAPE, memory cleared on BREAK (via OSBYTE 200)
\ Calculate Checksum0 = 70x pages of all Elite code from &F40 to &5540
\ Finally start the game by calling the entry fn indirected through first 2 bytes of elite-source.asm, grandly titled 'TT170'

I'm sure this will bring back memories for anyone that hacked the loader back in the day. Nothing too evil but certainly lots of obfuscation, making things harder to disassemble cleanly and hiding code in the stack. I did like the OSBPUT trick, that had me for a short while.

I've removed the checksum & encryption already but it's in a branch at the moment - I need to figure out the best way to set up the build process.
Last edited by kieranhj on Tue Jul 17, 2018 11:44 am, edited 1 time in total.
Bitshifters Collective | Retro Code & Demos for BBC Micro & Acorn computers | https://bitshifters.github.io/

Kevin Edwards
Posts: 94
Joined: Tue Mar 14, 2006 9:16 pm
Contact:

Re: Elite BeebAsm Source

Post by Kevin Edwards » Tue Jul 17, 2018 12:02 pm

Awesome work, Kieran!

I remember looking through the original code years ago and it made my eyes bleed! Not because I didn't follow the code, but because of the (lack of) formatting and the short labels etc.

Must re-visit it and have a play.

Cheers,
Kevin.

User avatar
kieranhj
Posts: 690
Joined: Sat Sep 19, 2015 10:11 pm
Location: Farnham, Surrey, UK
Contact:

Re: Elite BeebAsm Source

Post by kieranhj » Tue Jul 17, 2018 12:15 pm

Thanks Kevin. Yes, the code being 'terse' is an understatement. It helps a bit with the whitespace and one statement per line but it's still tough to follow without any meaningful labels. I will see about tracing through the code a little way to at least comment the functions in the main loop.
Bitshifters Collective | Retro Code & Demos for BBC Micro & Acorn computers | https://bitshifters.github.io/

User avatar
davidb
Posts: 2091
Joined: Sun Nov 11, 2007 10:11 pm
Contact:

Re: Elite BeebAsm Source

Post by davidb » Tue Jul 17, 2018 12:53 pm

On the Electron I found that side-stepping the whole process by using the Superior re-release was the way to go! :) I'm guessing that the original protection caused problems at the time of the re-release.

User avatar
oss003
Posts: 2753
Joined: Tue Jul 14, 2009 11:57 am
Location: Netherlands
Contact:

Re: Elite BeebAsm Source

Post by oss003 » Tue Jul 17, 2018 2:38 pm

Hi guys,

You can find the Atomic Elite version here: https://github.com/oss003/Elite
This version is based upon a snapshot from the Electronic version.

Greetings
Kees

User avatar
kieranhj
Posts: 690
Joined: Sat Sep 19, 2015 10:11 pm
Location: Farnham, Surrey, UK
Contact:

Re: Elite BeebAsm Source

Post by kieranhj » Tue Jul 17, 2018 9:18 pm

Quick update that the build process now creates an unencrypted / non-checksum'd version of the loader and game by default, so won't be binary identical. Added a new build target 'encrypt' and changed the verify batch file to make encrypted versions again to compare against the originals.

I've started looking to comment the code but slow going, so don't hold your breath. (Happy for someone else to have a go...) There are also 0 bytes left in main RAM I realised so not much scope for modification anyway..!
Bitshifters Collective | Retro Code & Demos for BBC Micro & Acorn computers | https://bitshifters.github.io/

VectorEyes
Posts: 77
Joined: Fri Apr 13, 2018 1:48 pm
Contact:

Re: Elite BeebAsm Source

Post by VectorEyes » Tue Jul 17, 2018 9:36 pm

Newbie question. If there are 0 bytes left, but the cassette version was missing stuff compared to the disc version, then where does all the extra stuff fit?

Actually never mind I've just answered my own question haven't I? The extra stuff is dynamically loaded from disc when you enter and leave a space station, because no need to keep the in-space and in-station code both in memory at once?

User avatar
jms2
Posts: 1982
Joined: Mon Jan 08, 2007 6:38 am
Location: Derby, UK
Contact:

Re: Elite BeebAsm Source

Post by jms2 » Wed Jul 18, 2018 8:15 am

Kees, do you still have the disassembly of the Electron version that you started with? I've been trying to create my own but using your version would save me some time. I know it's not that hard to do, but my rate of progress is 100x slower than yours!

User avatar
oss003
Posts: 2753
Joined: Tue Jul 14, 2009 11:57 am
Location: Netherlands
Contact:

Re: Elite BeebAsm Source

Post by oss003 » Wed Jul 18, 2018 10:05 am

jms2 wrote:
Wed Jul 18, 2018 8:15 am
Kees, do you still have the disassembly of the Electron version that you started with? I've been trying to create my own but using your version would save me some time. I know it's not that hard to do, but my rate of progress is 100x slower than yours!
I have but it's not so documented as the Atom source. You can combine them because the label names are the same. I'll post the Electron source later when I'm at home.

Greetings
Kees

User avatar
jms2
Posts: 1982
Joined: Mon Jan 08, 2007 6:38 am
Location: Derby, UK
Contact:

Re: Elite BeebAsm Source

Post by jms2 » Wed Jul 18, 2018 10:30 am

That's great, thanks.

User avatar
oss003
Posts: 2753
Joined: Tue Jul 14, 2009 11:57 am
Location: Netherlands
Contact:

Re: Elite BeebAsm Source

Post by oss003 » Wed Jul 18, 2018 5:06 pm

Here's the Electron source.
Text is a file with the decoded text messages.

And a post about how the Electron scans keys: viewtopic.php?f=44&t=5975&p=55883&hilit ... key#p55883

Greetings
Kees
Attachments
text.txt
(8.55 KiB) Downloaded 10 times
elitedata.txt
(44.6 KiB) Downloaded 6 times
elitecode.txt
(394.21 KiB) Downloaded 6 times
Last edited by oss003 on Wed Jul 18, 2018 5:27 pm, edited 1 time in total.

User avatar
jms2
Posts: 1982
Joined: Mon Jan 08, 2007 6:38 am
Location: Derby, UK
Contact:

Re: Elite BeebAsm Source

Post by jms2 » Wed Jul 18, 2018 8:43 pm

Great - thanks Kees

Post Reply