ejolson
Posts: 8053
Joined: Tue Mar 18, 2014 11:47 am

Re: A Pico Z-machine

Mon Jul 26, 2021 6:00 am

ejolson wrote:
Sun Jul 25, 2021 10:09 pm
The same binary can be flashed and run with Pocfel on the Pico using ztouf2. How exciting is that? I find it amazing nobody has created a Z-code version of this classic wumpus hunting game before.
I was reading about Z-machines and found
Wikipedia wrote: The Inform website lists links to freely available interpreters for 15 desktop operating systems (including 8-bit microcomputers from the 1980s such as the Apple II, TRS-80, and ZX Spectrum, and grouping "Unix" and "Windows" as one each), 10 mobile operating systems (including Palm OS and the Game Boy), and four interpreter platforms (Emacs, Java, JavaScript, and Scratch). According to Nelson, it is "possibly the most portable virtual machine ever created".
https://en.wikipedia.org/wiki/Z-machine

see also

http://www.inform-fiction.org/zmachine/ ... eters.html

Along with Amstrad, Apple II, Atari ST, BeOS, DOS, GEM, Mac OS, Newton, Oric, OS/2, RISC OS, Spectrum, Unix, Windows, eBookman. EPOC, Gameboy, Helio, Nokia, PalmOS, Psion, TI, Windows CE, Wizard, Emacs, Java and Javascript, I'm happy to announce the Pico can now run Z code.

In anticipation of a new listing, I tidied up the in-game menu for the Pico version of Pocfel to remove the memory-hungry commands that cause crashes. The result when hunting wumpi looks like

Code: Select all

Pocfel version 13 running...                                                    
                                                                                
Instructions? (y-n) n                                                           
You are in room 4                                                               
Bats nearby                                                                     
I feel a draft                                                                  
There are tunnels to 2 7 8                                                      
Move or shoot (m-s) /save                                                       
                                                                                
Enter filename for save game: wumpus1                                           
[Saved]                                                                         
                                                                                
>/dir                                                                           
               ./               ../          wumpus1                            
>m                                                                              
which room? 7                                                                   
You are in room 7                                                               
You fell into a pit                                                             
Another game? (y-n) /restore                                                    
                                                                                
Enter filename for save game: wumpus1                                           
[Starting history playback]                                                     
[End of history playback]                                                       
                                                                                
[Restored]                                                                      
                                                                                
>m                                                                              
which room? 2                                                                   
You are in room 2                                                               
Bats nearby                                                                     
There are tunnels to 4 5 15                                                     
Move or shoot (m-s)
I find it amazing wumpus.c immediately gets a save and restore feature just from running on the Pico Z-machine. That's almost as good as a Unix core dump.

Does the Wikipedia article really claim a Z-code interpreter has been written in Scratch? Could that even be true?

ejolson
Posts: 8053
Joined: Tue Mar 18, 2014 11:47 am

Re: A Pico Z-machine

Mon Jul 26, 2021 4:48 pm

ejolson wrote:
Mon Jul 26, 2021 6:00 am
I find it amazing wumpus.c immediately gets a save and restore feature just from running on the Pico Z-machine. That's almost as good as a Unix core dump.
It seems I've been doing too much reading recently, maybe on account of the fires which again for the last week have made it difficult to see across the valley from the top of the dog house or even half way. As a result, I discovered a less universal virtual machine that runs on the Pico.

https://microhobby.com.br/blog/2021/06/ ... y-pi-pico/

As far as I can tell, the main drawback of the .NET nanoFramework is that it doesn't play Lost Pig. Could a Z-code interpreter be written in C# to remedy that? If possible in Scratch, it might be possible in .NET too.

Back to the plan of world domination, I've been unable to find an RP2040 on a circuit board designed to plug directly into a USB A socket. Does anyone make one? I'd ask Fido for help, except I don't want to revisit that project to port KiCad to the PET.

hippy
Posts: 10542
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: A Pico Z-machine

Mon Jul 26, 2021 6:28 pm

ejolson wrote:
Mon Jul 26, 2021 4:48 pm
I've been unable to find an RP2040 on a circuit board designed to plug directly into a USB A socket. Does anyone make one?
The only one I know of is -

Adafruit Trinkey QT2040 - https://www.adafruit.com/product/5056

ejolson
Posts: 8053
Joined: Tue Mar 18, 2014 11:47 am

Re: A Pico Z-machine

Mon Jul 26, 2021 6:49 pm

hippy wrote:
Mon Jul 26, 2021 6:28 pm
ejolson wrote:
Mon Jul 26, 2021 4:48 pm
I've been unable to find an RP2040 on a circuit board designed to plug directly into a USB A socket. Does anyone make one?
The only one I know of is -

Adafruit Trinkey QT2040 - https://www.adafruit.com/product/5056
That seems to be exactly the idea, except missing the laser-engraved hardwood case and out of stock. I wonder how much more those extra thick printed circuit boards that fit in a USB socket cost. Is this why the retail price is double a regular Pico?

ejolson
Posts: 8053
Joined: Tue Mar 18, 2014 11:47 am

Re: A Pico Z-machine

Mon Jul 26, 2021 10:00 pm

ejolson wrote:
Mon Jul 26, 2021 6:00 am
I'm happy to announce the Pico can now run Z code.
It seems the release of Pocfel for the Pico was timed well with Aaron Reed's survey on 50 Years of Text Games.

https://if50.substack.com/p/1996-so-far

The year 1996 is notable for So Far by Andrew Plotkin, the first major Z-machine game that was independently produced.

https://ifdb.org/viewgame?id=rcrihauxixy48svr

This led to the public release of the Inform language and the start of a whole new era of interactive fiction. Given the historical significance, I decided to download it and check if it runs on the Pico Z-machine.

First I typed

Code: Select all

$ wget https://ifarchive.org/if-archive/games/zcode/SoFar.z8
$ ztouf2 SoFar.z8 
/x/libb/ejolson/bin/ztouf2.bin SoFar.z8 SoFar.uf2 0x10180000
Wrote 1173 blocks to SoFar.uf2
and then as root

Code: Select all

# mount /dev/sda1 /mnt
# cp SoFar.uf2 /mnt
# umount /mnt
Upon connecting to the USB serial port, I obtained

Code: Select all

Pocfel version 13 running...



Hot, foul, and dark. How did indoor theater become so fashionable? Well enough
in spring rain or winter, but not in the thick, dead afternoon of high summer.
And though Rito and Imita looks very fine, shining with electric moonslight in
the enclosed gloom, you're much more aware of being crammed in neck-by-neck
with your sweaty fellow citizens.

Damn the crowd, in truth: your mood was hot, foul, and dark when you sat down.
Aessa was supposed to meet you here. She's made excuses before, and you don't
think about what it might mean. Try not to think, rather. Just watch the story.
One of your favorites. But it's miserably hot, and you just aren't caught up in
the play...

SO FAR
An Interactive Catharsis
Copyright 1996 by Andrew Plotkin.
(First-time players should type "about".)
Release 6 / Serial number 961218 / Inform v1502 Library 5/12
Standard interpreter 1.1

Lower Theater (on the bench)
If nothing else, you have a good seat. The stage is only a few rows to the
north. People are jammed on benches in every direction; you think the aisle is
somewhere to the west.

The play is into the final act. Rito has finally found out about Imita's
affair, and he stalks the stage, whipping voice and hands about himself. A
footfall behind him; he turns, and sees Imita. She backs a step at the force of
his gaze.

>
Woohoo! Here is a game with almost as much cultural importance as the original Zork and Colossal Cave adventures, not to mention Hunt the Wumpus.

The only difficulty is that unlike Lost Pig
    lostpig.jpg
    lostpig.jpg (13.58 KiB) Viewed 495 times
      there isn't a game graphic to burn into the hardwood case.

      ejolson
      Posts: 8053
      Joined: Tue Mar 18, 2014 11:47 am

      Re: A Pico Z-machine

      Tue Jul 27, 2021 5:29 am

      ejolson wrote:
      Mon Jul 26, 2021 10:00 pm
      Given the historical significance (of So Far), I decided to download it and check if it runs on the Pico Z-machine.
      After climbing an iron pole and getting stuck, I decided to read the walk through. The walk through indicated multiple save and restore points which the Pico handled without incident. In this way I tested things for some time and never experienced another crash or out of memory error.

      Woohoo! Pocfel Version 13 is stable!
      Last edited by ejolson on Tue Jul 27, 2021 5:59 am, edited 1 time in total.

      ejolson
      Posts: 8053
      Joined: Tue Mar 18, 2014 11:47 am

      Re: A Pico Z-machine

      Tue Jul 27, 2021 5:40 am

      ejolson wrote:
      Tue Jul 27, 2021 5:29 am
      Woohoo! Pocfel Version 13 seems stable!
      It seems So Far changed since the walk through was made so I couldn't finish it.

      For a break, I decided to try something more relaxing and compiled my own C translation of the Govern Ancient Sumeria simulation which appeared in David Ahl's 101 Basic Computer Games.

      Removing GNU readline (it's not needed) and switching to integer arithmetic (since vbccz doesn't support floats) led to

      https://fractal.math.unr.edu/~ejolson/p ... mmurabi.z8

      which runs on the Pico Z-machine as

      Code: Select all

      Pocfel version 13 running...
      
      hmrabi--Govern Ancient Sumeria
      
      Try your hand at governing ancient Sumeria
      Successfully for a 10-year term of office.
      
      Would you like more information?  yes
      
      Description
      
      In this game you direct the adminitrator of Sumeria, Hammurabi,
      how to manage the city.  The city initially has 1,000 acres, 100
      people and 3,000 bushels of grain in storage.
      
      You may buy and sell land with your neighboring city-states for
      bushels of grain--the price will vary between 17 and 26 bushels
      per acre.  You also must use grain to feed your people and as
      seed to plant the next year's crop.
      
      You will quickly find that a certain number of people can only
      tend a certain amount of land and that people starve if they
      are not fed anough.  You also have the unexpected to contend
      with such as a plague, rats destroying stored grain, and variable
      harvests.
      
      You will also find that managing just the few resources in this
      game is not a trivial job over a period of say ten years.  The
      crisis of population density rears its head very rapidly.
      
      Source
      
      This program translated to C by Eric Olson from the one appearing
      in 101 BASIC Computer Games by David Ahl.  In turn that program
      was translated from the original FOCAL program which was floating
      around DIGITAL in the late 1970's.
      
      
      Hammurabi:  I beg to report to you,
      In year 1 , 0 people starved, 5 came to the city.
      Population is now 100
      The city now owns 1000 acres.
      You harvested 3 bushels per acre.
      Rats ate 200 bushels.
      You now have 2800 bushels in store.
      
      Land is trading at 20 bushels per acre.
      How many acres do you wish to buy?  0
      How many acres do you wish to sell?  0
      How many bushels do you wish to feed your people?  2000
      How many acres do you wish to plant with seed?  800
      
      
      Hammurabi:  I beg to report to you,
      In year 2 , 0 people starved, 5 came to the city.
      A horrible plague struck!  Half the people died.
      Population is now 52
      The city now owns 1000 acres.
      You harvested 5 bushels per acre.
      Rats ate 100 bushels.
      You now have 4300 bushels in store.
      
      Land is trading at 20 bushels per acre.
      How many acres do you wish to buy?  0
      How many acres do you wish to sell?  100
      How many bushels do you wish to feed your people?  500
      How many acres do you wish to plant with seed?  500
      
      You starved 27 people in one year!!!
      Due to this extreme mismanagement you have not only
      been impeached and thrown out of office but you have
      also been declared 'national fink' !!
      
      So long for now.
      
      Maybe the interactive fiction wasn't so bad after all. Since 16-bit integers are used for most of the variables, I wonder if it is possible to finish without an overflow resulting in a negative amount of grain.

      ejolson
      Posts: 8053
      Joined: Tue Mar 18, 2014 11:47 am

      Re: A Pico Z-machine

      Tue Jul 27, 2021 10:03 pm

      ejolson wrote:
      Tue Jul 27, 2021 5:40 am
      Since 16-bit integers are used for most of the variables, I wonder if it is possible to finish without an overflow resulting in a negative amount of grain.
      I changed all the variables to 32-bit to make sure there are no overflows and added a %ld format specifier to the simple printf implementation in cstubs.c. The link to hammurabi.z8 in the previous post has been updated to the new version. It also asks for a random seed.

      Here is a better run:

      Code: Select all

      Pocfel version 13 running...
      
      hmrabi--Govern Ancient Sumeria
      
      Try your hand at governing ancient Sumeria
      Successfully for a 10-year term of office.
      
      Would you like more information?  no
      
      Please enter a random seed between 0 and 29282:  10234
      
      
      Hammurabi:  I beg to report to you,
      In year 1 , 0 people starved, 5 came to the city.
      Population is now 100
      The city now owns 1000 acres.
      You harvested 3 bushels per acre.
      Rats ate 200 bushels.
      You now have 2800 bushels in store.
      
      Land is trading at 25 bushels per acre.
      How many acres do you wish to buy?  0
      How many acres do you wish to sell?  0
      How many bushels do you wish to feed your people?  2000
      How many acres do you wish to plant with seed?  999
      
      
      Hammurabi:  I beg to report to you,
      In year 2 , 0 people starved, 9 came to the city.
      Population is now 109
      The city now owns 1000 acres.
      You harvested 1 bushels per acre.
      Rats ate 150 bushels.
      You now have 1150 bushels in store.
      
      Land is trading at 22 bushels per acre.
      How many acres do you wish to buy?  0
      How many acres do you wish to sell?  100
      How many bushels do you wish to feed your people?  2000
      How many acres do you wish to plant with seed?  900
      
      
      Hammurabi:  I beg to report to you,
      In year 3 , 9 people starved, 8 came to the city.
      Population is now 108
      The city now owns 900 acres.
      You harvested 1 bushels per acre.
      Rats ate 0 bushels.
      You now have 1800 bushels in store.
      
      Land is trading at 17 bushels per acre.
      How many acres do you wish to buy?  0
      How many acres do you wish to sell?  100
      How many bushels do you wish to feed your people?  2000
      How many acres do you wish to plant with seed?  800
      
      
      Hammurabi:  I beg to report to you,
      In year 4 , 8 people starved, 2 came to the city.
      A horrible plague struck!  Half the people died.
      Population is now 51
      The city now owns 800 acres.
      You harvested 4 bushels per acre.
      Rats ate 550 bushels.
      You now have 3750 bushels in store.
      
      Land is trading at 23 bushels per acre.
      How many acres do you wish to buy?  50
      How many bushels do you wish to feed your people?  1020
      How many acres do you wish to plant with seed?  509
      
      
      Hammurabi:  I beg to report to you,
      In year 5 , 0 people starved, 8 came to the city.
      Population is now 59
      The city now owns 850 acres.
      You harvested 3 bushels per acre.
      Rats ate 0 bushels.
      You now have 2853 bushels in store.
      
      Land is trading at 23 bushels per acre.
      How many acres do you wish to buy?  20
      How many bushels do you wish to feed your people?  1180
      How many acres do you wish to plant with seed?  589
      
      
      Hammurabi:  I beg to report to you,
      In year 6 , 0 people starved, 17 came to the city.
      Population is now 76
      The city now owns 870 acres.
      You harvested 2 bushels per acre.
      Rats ate 229 bushels.
      You now have 1868 bushels in store.
      
      Land is trading at 26 bushels per acre.
      How many acres do you wish to buy?  0
      How many acres do you wish to sell?  100
      How many bushels do you wish to feed your people?  1520
      How many acres do you wish to plant with seed?  759
      
      
      Hammurabi:  I beg to report to you,
      In year 7 , 0 people starved, 5 came to the city.
      A horrible plague struck!  Half the people died.
      Population is now 40
      The city now owns 770 acres.
      You harvested 1 bushels per acre.
      Rats ate 0 bushels.
      You now have 3328 bushels in store.
      
      Land is trading at 25 bushels per acre.
      How many acres do you wish to buy?  0
      How many acres do you wish to sell?  0
      How many bushels do you wish to feed your people?  800
      How many acres do you wish to plant with seed?  399
      
      
      Hammurabi:  I beg to report to you,
      In year 8 , 0 people starved, 10 came to the city.
      Population is now 50
      The city now owns 770 acres.
      You harvested 5 bushels per acre.
      Rats ate 0 bushels.
      You now have 4324 bushels in store.
      
      Land is trading at 24 bushels per acre.
      How many acres do you wish to buy?  0
      How many acres do you wish to sell?  0
      How many bushels do you wish to feed your people?  1000
      How many acres do you wish to plant with seed?  499
      
      
      Hammurabi:  I beg to report to you,
      In year 9 , 0 people starved, 5 came to the city.
      A horrible plague struck!  Half the people died.
      Population is now 27
      The city now owns 770 acres.
      You harvested 4 bushels per acre.
      Rats ate 0 bushels.
      You now have 5071 bushels in store.
      
      Land is trading at 20 bushels per acre.
      How many acres do you wish to buy?  30
      How many bushels do you wish to feed your people?  540
      How many acres do you wish to plant with seed?  269
      
      
      Hammurabi:  I beg to report to you,
      In year 10 , 0 people starved, 32 came to the city.
      Population is now 59
      The city now owns 800 acres.
      You harvested 5 bushels per acre.
      Rats ate 0 bushels.
      You now have 5142 bushels in store.
      
      Land is trading at 19 bushels per acre.
      How many acres do you wish to buy?  100
      How many bushels do you wish to feed your people?  1180
      How many acres do you wish to plant with seed?  589
      
      In your 10-year term of office 1.56 percent of the
      population starved per year on average, i.e., a total of
      17 people died!!
      You started with 10 acres per person and ended with
      15.25 acres per person.
      
      A fantastic performance!!!  Charlemange, Disraeli, and
      Jefferson combined could not have done better!
      
      Would you like to continue playing?  no
      
      So long for now.
      
      For reference, the C source is

      https://fractal.math.unr.edu/~ejolson/p ... hmrabi.tgz

      To build hammurabi.z8 you must also install the Inform 6 compiler as well as vbccz. Of additional interest is the 32-bit arithmetic library runtime.s for the Z-machine. I modified it slightly so it compiles with the latest Z-code assembler.
      Last edited by ejolson on Tue Jul 27, 2021 10:35 pm, edited 9 times in total.

      M_P
      Posts: 94
      Joined: Sun Jan 06, 2013 5:40 pm

      Re: A Pico Z-machine

      Tue Jul 27, 2021 10:05 pm

      Man, I love those old text games. Zork, HHGttG... lots of good memories there (admittedly some memories of being extremely frustrated).

      Thank you for posting this!

      ejolson
      Posts: 8053
      Joined: Tue Mar 18, 2014 11:47 am

      Re: A Pico Z-machine

      Tue Jul 27, 2021 10:07 pm

      M_P wrote:
      Tue Jul 27, 2021 10:05 pm
      Man, I love those old text games. Zork, HHGttG... lots of good memories there (admittedly some memories of being extremely frustrated).

      Thank you for posting this!
      You are welcome! Let me know if you have any trouble building pocfel for the Pico.

      ejolson
      Posts: 8053
      Joined: Tue Mar 18, 2014 11:47 am

      Re: A Pico Z-machine

      Fri Jul 30, 2021 5:27 pm

      ejolson wrote:
      Tue Jul 27, 2021 5:29 am
      Woohoo! Pocfel Version 13 is stable!
      Changes have been made to fix a minor bug in the custom readline code. The download link in the first post of this thread has been updated to version 14.

      Return to “General”