Hidden Code

From Maze

Current revision as of 18:57, 6 August 2010 by JohnBell (Talk | contribs) (Formatting fixes)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Hidden Code

(Note: This section does not get into areas that I believe are going to be covered in greater depth elsewhere in the book. The biggest glaring omission is in character sets/graphics. It is intended as a high-level gloss of some of the features of the environment that impact 10-Print's output; depending on the trajectory of the book, it might serve as an introduction to other sections or be fleshed out more itself. I'd like to take it in the direction of how other environments would change the output/require different code, but I'll see how it fits with other sections.)

10 PRINT CHR$(205.5+RND(1)); : GOTO 10

One of the features that make one-line programs interesting is the amount of functionality that can be squeezed from a single line of code. In the case of 10-Print, the final appearance of the program's output has as much to do with the Commodore 64's BASIC environment as the code that is actually written in BASIC on the screen. The functionality specified in the code – printing, looping, and displaying characters – is explored elsewhere in the book. This section looks at the hidden code, the functionality that is implicitly inherited from the environment and has a big impact on the aesthetics of the 10-Print program.

Colors

Set background color to dark blue
Set text color to light blue
10 PRINT CHR$(205.5+RND(1)); : GOTO 10

The first environmental setting is apparent before the program is written; the default text and background colors of the Commodore 64. While these defaults can be overridden easily enough – text colors in BASIC on the Commodore 64 are printable characters so adding PRINT CHR$(30), for example, would make the text green; background colors are similarly changeable using POKE – the original 10-Print program outputs using the pre-existing conditions of the environment.

Modified version:

5 PRINT CHR$(30)
6 POKE 53281, 0
10 PRINT CHR$(205.5+RND(1));
20 GOTO 10

Screen grid

Set background color to dark blue
Set text color to light blue
Divide screen into a 40 column by 25 row grid
10 PRINT CHR$(205.5+RND(1)); : GOTO 10

While the code for 10-Print is devoted to choosing a character and printing it on the screen, it says nothing about where on the screen the chosen character should appear. Instead it depends on the Commodore 64's screen memory map, a system that divides the screen into a grid 40 characters wide and 25 characters tall while in text mode. Each memory location has an associated pixel position on the screen, and any character value that is stored in that memory slot will be displayed at the corresponding screen position.

Modified version: Not possible in text mode

Truncation

Set background color to dark blue
Set text color to light blue
Divide screen into a 40 column by 25 row grid
10 PRINT CHR$(round down(205.5+RND(1))); : GOTO 10

The process of choosing a random number is described elsewhere in this book, but one key feature of 10-Print's environment that makes the random number useful is its ability to automatically truncate decimals. The equation used to determine which character code to display, 205.5+RND(1), results in values between 205.500000 and 206.500000 and includes 6 digits after the decimal. Character codes, however, are integer values. In many environments the programmer would need to include a separate function to round off the equation output and create an integer that would be recognized as a character code, potentially pushing the 10-Print program beyond a single line. The Commodore 64 BASIC environment simply truncates the decimal value, resulting in character codes of either 205 or 206.

Modified version: not possible

Cursor

Set background color to dark blue
Set text color to light blue
Divide screen into a 40 column by 25 row grid
Move cursor to column 0
10 PRINT CHR$(round down(205.5+RND(1)));
Move cursor 1 column to the right
If cursor column is greater than 39
Move cursor to column 0
Move cursor down one row
GOTO 10

Once a grid has been established the software still needs to determine the grid position in which it should be drawing a character. Without this information the characters would be drawn either randomly across the screen or one on top of another instead of in the neatly linear pattern seen in 10-Print. The Commodore 64's text mode includes the concept of a cursor that begins on the left side of the screen, advances to the right by one column every time a character is printed to the screen, and drops to the next row while returning to the leftmost column after the final column is filled. The key command here is PRINT; if the characters were simply POKE'd to the screen, the cursor would not be moved and the environment would not automatically handle positioning. The semi-colon at the end of the PRINT statement ensures that a line break will not be inserted between each character, allowing the cursor to move horizontally despite only one character being printed in each iteration of the loop.

Modified version:

10 POKE 1024+RND(1)*1024,205.5+RND(1): GOTO 10

Scrolling

Set background color to dark blue
Set text color to light blue
Divide screen into a 40 column by 25 row grid
Move cursor to column 0
10 PRINT CHR$(round down(205.5+RND(1)));
Move cursor 1 column to the right
If cursor column is greater than 39
Move cursor to column 0
Move cursor down one row
If cursor row is greater than 24
Erase characters from row 0
Move character at column x, row n to column x, row n-1
Erase characters from row 24
Move cursor to row 24
GOTO 10

10-Print is programmed to loop forever, so there must be a contingency available for when the cursor has filled the entire screen grid with characters and cannot move to the next row. In addition to automatically wrapping text, the Commodore 64's text mode also automatically scrolls the contents of the screen when the cursor is on the bottom row and attempts to move down. While it seems intuitive to do this, some programming environments would simply stop the program or return the cursor to the first column and row of the grid, overwriting the characters that had already been printed on the screen.

Modified version:

10 FOR I=1024 TO 2043: POKE I,205.5+RND(1): NEXT I: GOTO 10
Personal tools