[1]An ag(e)ing hacker -- Luca Saiu's blog -- Luca Saiu -- blog Atom feed [2]An ag(e)ing hacker -- Luca Saiu's blog -- Luca Saiu -- blog RSS feed [3]top [4](dir)

How I learned procedural abstraction and the wonder of science – 2011-09-19 05:06 (last update: 2014-11-24 23:55)

[5]An ag(e)ing hacker — Luca Saiu's blog

How I learned procedural abstraction and the wonder of science

Inspired by a happy “discovery” I made today, I’ve written this instead of finishing the post about identity.

The abandoned tape

This story began on a summer evening of the late Eighties when I was 10, maybe 11. I was at some small country fair near home with my brother, five years younger than me — quite a big difference back then.

My brother found an audio cassette discarded on the ground, and wanted to take it home; it looked dirty and I remember that I didn’t want to pick it up at first, but I guess I wasn’t too hard to convince after all. The cassette was probably home-recorded, or in any case it had a blank label. Ok, let’s take it. Back home we tried to listen to it on our little boombox, and we immediately recognized the sound: it’s a computer program! Indeed it was. And it was even for our computer, the Commodore 64 ([6]http://en.wikipedia.org/wiki/Commodore 64) — a machine already a little past its prime at the time, but hey, that was all we had.

We tried it: as a thousand other times I hit SHIFT + RUN STOP, then the usual

PRESS PLAY ON TAPE

appeared on the screen. PLAY. We thought it was a game. And we used to love games back then, but that thing would prove to be so much better.

FOUND "HALLEY"

LOADING...

Then you had to wait the usual two to three minutes for loading. I think this one took a little less than average.

Halley

The basic idea of Halley was simple: you told the program a date, time and location on Earth, and it drew the sky bodies as they would look from that place at that time.

It had some more functionalities for highlighting constellations, and of course for showing the position of Halley’s comet, due to pass in 1986 and hence at the center of a minor popular craze; we got the program later, when it was already a few years old.

Several features. But I think that even as a child I immediately understood that joining the Big Dipper stars with line segments was not the interesting part: that thing could compute the apparent position of the stars, from numbers. Of course I had no practical application for this, but who cared? I didn’t know the phrase yet, but I got the idea perfectly: there was a predictive model at work.

The incredible beauty of science. I was in awe and wonder.

Procedural abstraction

The program quickly led me to a second important discovery, this time about programming. This may be somewhat header to “get” now, for people not accustomed to the Commodore 64 or to the education environment at the time. I lived in an isolated setting where nobody could teach me about computers; at that time (and for some more years later) everything I knew about programming came from two small books: the Commodore manual, and another one explicitly dedicated to programming that machine — neither was very good, I can say now.

The peculiar limitations of the machine interface didn’t help either. “Real” programs at the time were written in machine language — and I’m saying machine language, not assembly: see below. Now, knowing myself better, I think I would have been able to learn at least some machine language at the time; but I didn’t have any documentation. Assembly and machine language were covered in more advanced books, clearly less popular and therefore less easily available. BBSs were out of reach for me.

For the unlucky children who lived in small country villages as I did, there was just BASIC. Most of the home computers of the time came with a BASIC interpreter in ROM, which was available at boot time and also served as a crude interface to the “operating system”, again in ROM: excruciatingly slow compared to native code, BASIC was touted as “friendly”. And indeed it was easier to use than machine language, but I wouldn’t exactly call Commodore 64’s BASIC a high-level language.

PEEK and POKE

For the time when it got out the machine even had some relatively good capabilities in terms of graphics, sound, and connectivity: the problem was that none of these functionalities was available from BASIC: in order to use sprites, for example, which were supported at the hardware level, you had to manually “PEEK and POKE” (BASIC for what we would call “load and store” now) bytes into memory-mapped registers, explicitly mentioning their numeric addresses. Even many mundane operations, such as changing the background color in text mode, could only be performed that way.

I still remember some addresses by heart. For example, this command changed the text-mode background to white:

POKE 53281, 1

The recommended way of resetting the machine via software was by jumping to a machine language routine in ROM, which happened to begin at the address 64738; from BASIC:

SYS 64738

Switching the screen to graphic mode and drawing wasn’t trivial at all; moreover there was no linear framebuffer letting you address each pixel individually: instead you could address each row in each 8x8 square making up the screen (40x25 squares), and play with bitwise operators to update the desired pixels in the desired row.

The arrangement of squares in memory wasn’t exactly convenient for graphics, either. The first 8x8 square in memory was the topmost leftmost on screen, and contained the eight-pixel rows indexed from 0 to 7; the second square, with rows 8 to 15 was the second from the left in the top row of squares; the third square in the first row of squares had rows 16..23, and so on up to the fortieth square; then started the second row of squares, and the forty-first 8x8 square in memory was the leftmost one of the second row from the top.

In practice you computed the row offset in memory, then you PEEKed the byte at the base address summed to the offset, twiddled some bits in it, and finally POKEd back an updated value.

In two-color mode each square row byte encoded 8 pixels; but in multi-color mode, a square row byte only encoded 4 “wide pixels” (two bits each, four colors), with halved horizontal resolution — Those were the official machine specifications; but as I learned many years later good hackers could push those limits by carefully playing with the CRT scan line timing, changing supposedly global settings at just the right moment to have them apply to a selected area of the screen only.

So, if a BASIC program was easier to read than an assembly listing in terms of arithmetics and control flow, many apparently “fundamental” operations still had to be implemented at a very low level.

If you didn’t know anything else then for you that was just the way things were, and you learned all magic numbers by heart without asking yourself many questions about alternatives. When it was the only way you knew it didn’t even feel inconvenient.

Procedures

One day I hit RUN STOP by mistake when running Halley. I saw BREAK IN and some line number, followed by the usual READY prompt and the blinking cursor. Of course I knew what it meant: apparently, Halley was written in BASIC.

LIST seemed to confirm the hypothesis: indeed there was a big BASIC program, which in theory I could have studied and tried to understand.

But very soon I noticed some strange commands in Halley’s source, whose names all began by the left arrow character “←”, for some reason: my English was still weak at the time but I could understand what “←LINE” and “←CIRCLE” meant: apparently they were graphic commands. I tried to run them interactively, and indeed they seemed to work. Yet I was absolutely sure they were not part of the predefined BASIC. I already knew how to draw a line and it was a much bigger pain than that, with all the square row offset computation and bit twiddling.

The new commands stopped working after a machine reset. This fact confirmed that they were part of Halley, which extended the BASIC language, providing new names for higher-level operations accepting parameters. This much I understood by myself, and for me it was quite a discovery. It was obviously the right way of proceeding: naming operations to be used more than once by a caller who may also supply parameters. I had discovered procedural abstraction.

As I only had two programming books I had read them many times, and I knew for sure that Commodore’s BASIC didn’t provide any way of defining named procedures with parameters; so I was sure that such definitions had to be performed in that fabled “machine language”[7]^1. That day I still couldn’t write my own procedures, but I had been enlightened.

Searching for a disk or tape image on the net

I’m a sentimental type; I’ve always been. And of course because of its meaning for me I’d always have loved to be able to easily run Halley in the following years, after having finally moved to different computers.

Our Commodore 64 is still there in its box, and the tapes should also work; it all worked fine the last time I tried, maybe less than ten years ago. But the setup is clumsy, there is stuff to move around, dust and disagreeable people; and most importantly I’ve been living in France for some time already, physically far from my old computer.

Running Halley on an emulator is the obvious solution; VICE ([8]http://www.viceteam.org/) is free software and works wonderfully; but since it’s not completely trivial to make a usable file from a cassette, in practice I also needed to find a copy of Halley somewhere on the Internet; yet apparently that particular software wasn’t that popular, and I’ve always been unable to find even a mention of it — until this afternoon.

When I was looking again at the web search results after lunch, a little bored, I started mindlessly skimming the usual false positives: no, it’s not “The Galaxy Project” nor the other two or three more famous titles always popping up...; then it came.

Identified the author

I don’t remember exactly the search query bringing me to the interview with the author Nemo Galletti — but after seeing that name my déjà vu feeling didn’t last long. I had finally found it: [9]http://ready64.it/articoli/leggi/idart/8/intervista-a-nemo-galletti. It doesn’t look that hard to locate after the fact, and the interview dates back to 2008. Was the last time I looked for Halley really before 2008? I’m still not sure.

From the interview I discovered something interesting: the new primitives to which I owed my enlightening experience were “the famous Toma routines”: Galletti gladly acknowledges that he reused some graphical routines written by a Danilo Toma. Galletti seems very unassuming: he disclaims any particular competence in the field of astronomy, and dismisses the formulas he used as “easily available in specialized magazines”. This surprises me.

Identified the graphic routines’ author

Toma’s routines were published on a paper magazine in at least two versions during the Eighties. His routines, written and assembled manually into machine code (!), were extracted back from memory for publication as a long listing of DATA commands and a “commented disassembly” (!!). At the time there were good technical reasons for such a crude solution: the machine was too small even to host a reasonable assembler, without hardware extensions.

Something I was shocked to learn now after many years was that Toma’s routines actually supported 3D coordinates, and rendered in perspective; and of course Galletti made use of the third coordinate.

Looking at the code when I was young and without any documentation whatsoever for Toma’s routines I couldn’t understand the role of all parameters; anyway now I feel more than a little disappointed in myself for not having suspected at the time something that important. It’s clear now that back then I played a bit with circles, lines and points, but I was fascinated by the concept I had understood for the first time, rather than by that particular set of new primitives.

Some years later at 15 or 16 I also wrote my own procedures for rendering wireframe objects in 3D perspective like, I’ve just learned now, Toma did; my project had some interesting aspects and I should write about it sometime, but I think the thing in the end was slowish. And I used Pascal on a much stronger computer instead of machine language on a Commodore 64. No match.

You can find magazine scans with a long article by Toma including the listings for his routines (starting at page 35), and the software on a disk image: [10]http://ready64.it/download/scheda_download.php?id_download=46. By the way, that is what was considered a “popular” publication at the time, not necessarily for the mathematically inclined; I wouldn’t call it too shallow or dumbed down.

Toma also seemed nice and unassuming; but except for what he wrote in the piece linked above and the occasional mention of “Toma’s routines”, I can’t seem to find anything about him on the Net; there is some information about a table tennis player with the same name and surname, but the athlete looks way too young to be the same person as the hacker.

Running Halley again

Given the author’s name it became easy to find a downloadable[11]^2 copy of Halley: [12]http://ready64.it/download/scheda_download.php?id_download=255.

Now I’m finally running the program again, on the VICE emulator. I’ve hesitated a little before looking again at the BASIC source, lest my expertise of today spoiled my childhood memories.

Then I made my mind, hit what would have been RUN STOP and typed[13]^3

LIST

Today I’m not terribly impressed with the source code. But that doesn’t necessarily mean anything.

A reminder for old-timers or an introduction for young spoiled kids: Commodore “BASIC V2”

The lack of primitive operations at a level higher than PEEK and POKE was not the only problem in Commodore’s BASIC.

That BASIC dialect didn’t leave a lot of freedom to the programmer: all variables were global, with names limited to a length of two characters. Since spaces in the source code consumed precious memory it was customary at the time to just leave them out, and for example write

120 FORI=1TO10STEP2:PRINTI:NEXTI

instead of

120 FOR I = 1 TO 10 STEP 2: PRINT I: NEXT I

As was the norm for home computer BASICs at the time, line numbers were mandatory and could not be replaced with symbolic labels.

The only conditional was a simple IF with fall-though control (no “else” branch), and a FOR..TO..STEP..NEXT loop (STEP was optional). As far as “structured programming” goes, that was it: if you wanted any other non-procedural control feature, you had to simulate it with GOTO.

There was support for a kind of crude subroutines, of course identified by line numbers only, which you could enter with GOSUB and exit with RETURN; an implicit call stack for BASIC routines was maintained for return addresses, but there was no mechanism at all to pass parameters or return results. And of course no procedure-local variables.

It was another world. It wouldn’t be fair to look at the programs of that time with our eyes of today.

And in Halley the “difficult” part seems perfectly reasonable, with data about each constellation neatly grouped in their own DATA section; there are comments. Control flow seems hard to follow to my now unaccustomed eyes, but how could it be otherwise? You worked with what you had.

No, I really have nothing negative to say about Halley’s source.

Twenty-five years after the comet

Halley’s comet has come and gone, the minor fad of that time also long forgotten. As fads go that was a good one: it introduced people to astronomy, and I’m sure the interest has stuck at least with somebody.

Even if sometimes I still toy with the idea of buying a cheap telescope and learning something, I doubt astronomy will ever become a real passion for me. But already back then I understood very clearly that astronomy was not the point: it was just one example of the ordered beauty of rationality, so plain and simple that even the child I was could see it.

I didn’t know anything about Galileo at the time, but now I’m sure astronomy wasn’t the point for him, either.

First epilogue

Some months ago, a supermarket in a lower-class suburb of Paris. The guy waiting in the counter line in front of me is a little unkempt, with a beard and a tired expression; around my age, he may resemble me somewhat.

He has only two items to check: a big pack of baby food jars, and a telescope.

(Do they sell telescopes here?)

He’s smiling.

Second epilogue

Here I am, finally again in front of Halley, now running on a virtual Commodore 64 as emulated by VICE, easily many times faster than the real thing even on my YeeLoong. And this is how Halley renders the sky of tonight, 18th September 2011 at 23:59, Paris latitude:

[14]the-sky-from-paris--2011-09-18--23.59--halley

— Luca Saiu, 2011-09-19 05:06 (last update: 2014-11-24 23:55)

[15][hacking.png]-[16][hacking.png] Tags:

[17]commodore-64, [18]english, [15]hacking, [19]my-masters, [20]myself, [21]nostalgia, [22]science

[23]Next post [24]Previous post

___________________________________________________________________________

[25]Go to the main index...

[1]Atom feed-[26][feed-icon.png] All post feeds: [1]Atom 1.0, [2]RSS 2.0.

[27][my photo]

Luca Saiu

The opinions I express here are my own and do not necessarily reflect the beliefs or policies of my employer, or for that matter of anyone else. In case you felt that the public statement of my thoughts threatened your warm sense of security and your emotional stability, please feel free to leave at any time.

The system does not support user comments and probably never will. Anyway you can [28]contact me by e-mail if you want to discuss some topic with me. I might update my posts if you provide interesting insights.

___________________________________________________________________________

Copyright © 2009, 2011-2014, 2017, 2018, 2021, 2022 Luca Saiu

Verbatim copying and redistribution of this entire page are permitted in any medium without royalties, provided this notice is preserved.

This page was generated by [29]trivialblog. trivialblog is [30]free software, available under the [31]GNU GPL.

Tag icon copyright information is available [32]in this file.

___________________________________________________________________________

Footnotes

[33](1)

It was easy to write a machine language routine into memory byte by byte, using only BASIC — even relatively convenient with READ and DATA; the difficult part to figure out was how to compose the machine language part. Alas, I never learned machine language or assembly for the Commodore 64. One of these days I might decide to do it, for kicks.

[34](2)

The publisher explicitly endorses downloading its old Commodore 64 software from [35]http://ready64.it:

Sul sito ready64.org legalmente autorizzato da Systems, sono disponibili liberamente le scansioni dei numeri della rivista Commodore Computer Club, Computer, e diversi software per Commodore 64 editi da Systems, quali la collana Commodore Club (poi Software Club), Commodore 64 Club, ma anche software quali La Voce (I, II, III), Raffaello, l’emulatore di MS-DOS, la Dichiarazione dei Redditi, etc. Sul sito www.museo-computer.it legalmente autorizzato da Systems, sono disponibili liberamente le scansioni dei numeri della rivista Sinclair Computer e Commodore Computer Club edite da Systems.

— Somebody at [36]http://www.systems.it/chisiamo.html

Halley’s complete BASIC source code is of course available, just one RUN STOP hit away. Toma’s routines have no actual source code: the closest existing thing to their “source code” was effectively machine language, not even assembly — and that code is available.

Anyway, even with its full source code available Halley is still not free software, as you’re technically not allowed to modify it and redistribute it (that’s the default, unless they tell us otherwise: unfortunately the wording “freely available” is not enough in this case). Yet I consider Halley to be a case of nostalgia and retrocomputing; it’s not current productive software use. For this reason only, I find acceptable to publish this article about the software, and link to it.

[37](3)

Actually it’s not that simple. I had forgotten how inconvenient it was to edit a large program on the Commodore 64; the LIST command displayed the program on the screen, scrolling vertically as soon as the last line was reached. You could slow down the process by keeping CONTROL pressed, but not pause or scroll up: so the most comfortable way of reading or editing a program section was listing just a line interval, if you knew more or less what you were looking for: for example you typed

LIST 100-200

to see all the lines with numbers in [100, 200]. Yes, with a dash.

___________________________________________________________________________

References

[1] file:///blog/feeds/atom.xml

[2] file:///blog/feeds/rss.xml

[3] file:///tmp/tmp.gUbGhiyvr3/patched.html#Top

[4] file:///tmp/tmp.gUbGhiyvr3/dir.html#Top

[5] file:///blog

64 [6] http://en.wikipedia.org/wiki/Commodore 64

[7] file:///tmp/tmp.gUbGhiyvr3/patched.html#FOOT1

[8] http://www.viceteam.org/

[9] http://ready64.it/articoli/leggi/idart/8/intervista-a-nemo-galletti

[10] http://ready64.it/download/scheda_download.php?id_download=46

[11] file:///tmp/tmp.gUbGhiyvr3/patched.html#FOOT2

[12] http://ready64.it/download/scheda_download.php?id_download=255

[13] file:///tmp/tmp.gUbGhiyvr3/patched.html#FOOT3

[14] file:///tmp/tmp.gUbGhiyvr3/the-sky-from-paris--2011-09-18--23.59--halley.png

[15] file:///blog/tags/hacking

[16] file:///blog/icons/hacking.png

[17] file:///blog/tags/commodore-64

[18] file:///blog/tags/english

[19] file:///blog/tags/my-masters

[20] file:///blog/tags/myself

[21] file:///blog/tags/nostalgia

[22] file:///blog/tags/science

[23] file:///tmp/6

[24] file:///tmp/4

[25] file:///blog/posts

[26] file:///blog/icons/feed-icon.png

[27] file:///static/my-photo.jpg

[28] mailto:positron@gnu.org

[29] http://ageinghacker.net/blog/posts/2

[30] http://www.gnu.org/philosophy/free-sw.html

[31] http://www.gnu.org/licenses/gpl.html

[32] file:///blog/icons/licenses

[33] file:///tmp/tmp.gUbGhiyvr3/patched.html#DOCF1

[34] file:///tmp/tmp.gUbGhiyvr3/patched.html#DOCF2

[35] http://ready64.it/

[36] http://www.systems.it/chisiamo.html

[37] file:///tmp/tmp.gUbGhiyvr3/patched.html#DOCF3