Recent

Author Topic: Reading Lightspeed Pascal source code  (Read 6141 times)

RememberTP

  • New Member
  • *
  • Posts: 12
Reading Lightspeed Pascal source code
« on: August 11, 2021, 11:23:04 am »
When Chris Crawford released the source code for most of his games a couple of years ago, three of the programs were written in Pascal.

All were for the classic MacOS. One is readable ASCII (Balance of Power 1990), but the other two (Patton Strikes Back and Le Morte D’Arthur) are labelled as "I have the Lightspeed Pascal source, need translation to ASCII"

If I look at the .p files that are supposed to be the Pascal source in a text editor, they look like this:

c605 0000 000c 0241 4908 9801 c803 0000
002c 9e01 7e05 0747 6c6f 6261 6c73 7c01
7e05 0653 7562 4d61 6308 9801 b008 0000
0054 0943 6c65 6172 4d6f 7665 a801 7e03
0357 686f a601 7e05 0749 6e74 6567 6572
etc etc.

Is that.. tokenised / compressed / what?

It would be possible to get a 68k Mac emulator and a copy of Lightspeed (and I may end up doing that) but is there a way to convert that into ASCII without that level of faff?


MarkMLl

  • Hero Member
  • *****
  • Posts: 3473
Re: Reading Lightspeed Pascal source code
« Reply #1 on: August 11, 2021, 11:57:48 am »
There's a pattern in there: a length byte followed by data which in some cases is ASCII:

> 0653 7562 4d61 63 SuMac
> 08 9801 b008 0000 0054
> 0943 6c65 6172 4d6f 7665 CleaHMove

https://wiki.freepascal.org/THINK_Pascal

I'd guess it's a compiled bytecode (possibly P-Code) and suggest that you look at the provenance of the compiler e.g. whether it's related to one of Wirth's original portable compilers or has been written from scratch.

In any event, decompilation is somewhere between hard and intractable.

MarkMLl

http://pascal.hansotten.com/niklaus-wirth/px-compilers/p4-compiler/
https://sourceforge.net/projects/pascalp4/
https://sourceforge.net/p/pascalp5/wiki/Home/
https://github.com/StanfordPascal
« Last Edit: August 11, 2021, 12:05:03 pm by MarkMLl »
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 9688
  • FPC developer.
Re: Reading Lightspeed Pascal source code
« Reply #2 on: August 11, 2021, 12:09:19 pm »
If you run *nix or cygwin "file" on the file, what does it say the format is ?

winni

  • Hero Member
  • *****
  • Posts: 2793
Re: Reading Lightspeed Pascal source code
« Reply #3 on: August 11, 2021, 12:48:03 pm »

RememberTP

  • New Member
  • *
  • Posts: 12
Re: Reading Lightspeed Pascal source code
« Reply #4 on: August 11, 2021, 01:33:25 pm »
If you run *nix or cygwin "file" on the file, what does it say the format is ?

data

If I look at the file with less, I can see the text editor was trying to be too clever and it is indeed a mix of ASCII and.. tokens?

<C6>^E^@^@^@^L^BA<98>^A<C8>^C^@^@^@,<9E>^A~^E^GGlobals|^A~^E^FSubMa<98>^A<B0>^H
^@^@^@T  ClearMove<A8>^A~^C^CWho<A6>^A~^E^GInteger<AA>^A<98>^A<B0>^H^@^@^@<86>
   GiveOrder<A8>^A~^C^CWho|^A~^D^DWha<A6>^A~^E^GInteger<AA>^A<98>^A<B2>^K^@^@^@
<C4>^NInvertLandmar<A8>^A~^B^Ax|^A~^B^Ay<A6>^A~^E^GInteger<AA>^A<A6>^A~^E^GInteger<98>^A<B0>^K^@^@^A0^OAdjustStrengths<A8>^A~^F^HAttacke|^A~^F^HDefende<A6>^A~^E^GInteger<98>^A<BA>^A~^F    AStrength|^A~^F DStrength<A6>^A~^E^GInteger<AA>^A
<98>^A<B0>^K^@^@^AH^NTacticalAdvic<98>^A<B0>   ^@^@^Ar
DecideMov<A8>^A~^C^CWho<A6>^A~^E^GInteger<AA>^A<98>^A<B0>^G^@^@^A<82>^GDailyAI
<98>^A<CA>^C^@^@^A<E2><92>-^@^AU{***********************************************************************************}<B0>^H^@^@^B
        ClearMove<A8>^A~^C^CWho<A6>^A~^E^GInteger<AA>^A<98>^A<80>^E^@^@^C^N^@^@
^B<B2><86>^P\^GMission^X\^CWho^\L\
FlankGuarx^O\^GMission^X\^CWho^\
\       NoMission<98>^Ax^M\
OrderCount^X\^CWho^\
Z^C^@^@<98>^Ax^O\^MOrderExecTime^X\^CWho^\
^HZ^C^@^@<98>^Ax^O\^LRefigureMove^X\^CWho^\
\^DTRUE<98>^A<9A>^A<98>^A<92>-^@^AU{***********************************************************************************}<B0>^H^@^@^C@   GiveOrder<A8>^A~^C^CWho|^A~^D^DWha<A6>^A~^E^GInteger<AA>^A<98>^A<80>^E^@^@^E<BC>^@^@^E`<86>^G\^DWhatZ^C
^@^@<80>^E^@^@^@^@^@^@^E\<86>^H\^LJustChecking<80>^E^@^@^@^@^@^@^DZx!\^NOrderConfirmed
^P\^FOrdersTT^X\^CWho^R\

MarkMLl

  • Hero Member
  • *****
  • Posts: 3473
Re: Reading Lightspeed Pascal source code
« Reply #5 on: August 11, 2021, 01:40:48 pm »
If I look at the file with less, I can see the text editor was trying to be too clever and it is indeed a mix of ASCII and.. tokens?

Who said anything about using less? You know it's data rather than text so use xxd | less or strings | less.

Won't help you though, I've already demonstrated that it's compiled or tokenised code: you need to either find the original source or research the provenance of that compiler.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

RememberTP

  • New Member
  • *
  • Posts: 12
Re: Reading Lightspeed Pascal source code
« Reply #6 on: August 11, 2021, 01:41:55 pm »
There's a pattern in there: a length byte followed by data which in some cases is ASCII:

In any event, decompilation is somewhere between hard and intractable.

Yes, looking at it with something that doesn't try to be too clever reveals it's ASCII plus something. Some of the bytes before the ASCII has the correct length of the string, some include a non-ASCII character in that - to say something like 'letter with a semicolon after it'?? Other bytes are presumably tokenised reserved words.

It's supposed to be source code :) but from the looks of it, Lightspeed tried to compact the source code files in its IDE.

I remember having at least one IDE that liked to have the number of spaces at the start of the line encoded in a single byte to do that, and this is a bit beyond that. I blame the capacity of floppy discs :)




RememberTP

  • New Member
  • *
  • Posts: 12
Re: Reading Lightspeed Pascal source code
« Reply #7 on: August 11, 2021, 01:44:27 pm »
Who said anything about using less? You know it's data rather than text so use xxd | less or strings | less.

Won't help you though, I've already demonstrated that it's compiled or tokenised code: you need to either find the original source or research the provenance of that compiler.

I tried less because it doesn't try to be too clever. Turns out that Sublime Text does.

This IS the original source according to its author. It was just saved by the IDE he was using in an annoying format.

Sigh, it looks like I have to go down the 68k Mac emulation route.

winni

  • Hero Member
  • *****
  • Posts: 2793
Re: Reading Lightspeed Pascal source code
« Reply #8 on: August 14, 2021, 12:20:33 pm »
Hi!

A free trial version of Sublime Text for Win10/Linux/Mac can be downloaded from german Chip magazine:


https://www.chip.de/downloads/Sublime-Text_57903494.html

Winni

RememberTP

  • New Member
  • *
  • Posts: 12
Re: Reading Lightspeed Pascal source code
« Reply #9 on: August 15, 2021, 12:15:52 am »
Indeed.

It's the editor I first used to look at the code, and it tried to be too clever on encountering a non-text file.

MarkMLl

  • Hero Member
  • *****
  • Posts: 3473
Re: Reading Lightspeed Pascal source code
« Reply #10 on: August 15, 2021, 09:02:19 am »
it tried to be too clever on encountering a non-text file.

No, /you/ tried to be too clever. Somebody else has already told you that you should have started off using the file command, the problem is that if you jump straight in using less it's likely to send an escape sequence to your console session that does something odd like tell it not to insert CR (^M) when it encounters a line end so you get "staircased" output.

(Somewhat later) This might be useful https://developer.apple.com/forums/thread/73330 look for a posing by KMT towards the end. The caveat there is that there's nothing that says that he knows enough about Pascal to recognise whether the result was valid sourcecode, or if all he'd managed to do was discard the spurious stuff.

MarkMLl
« Last Edit: August 15, 2021, 05:32:31 pm by MarkMLl »
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018