DRAWING THE LINE (PART 1)   Leave a comment

DRAWING THE LINE (PART 1)

C64DRAW1
An early version of my program to draw a line across the C64 screen, developed using WinVICE V2.2.

I’ve decided once and for all to make another attempt, after 29 years, to investigate how the hell it would be possible to carry out the simple feat of drawing a line across the high resolution or low resolution graphics screen on a Commodore 64, using its built in Commodore BASIC V2! Of course, I was originally planning a court case against the computer dealer, the computer magazine, and/or Commodore themselves, challenging them to show in court how to draw a line across the screen. I wanted my money back, as well as compensation for distress caused. I also wanted to show the Court how it was done on other computers. I’m coming back to this problem after years of studying and using various programming techniques on several different computers, such as the BBC Micro, Amstrad CPC, MSX, Amiga, Windows PC, Linux, and Atari 8 bit computers. Get set for a big shock, because I’ve actually made some progress! Be careful while reading this article, because it contains numerous Commodore BASIC V2 PEEKs and POKEs. I think these can cause brain damage. You have been warned!

I hope everyone reading this now knows that the BASIC built in to the Commodore 64 wasn’t even written for the Commodore 64, but was ported from the original Commodore PET released in 1977, which was 5 years before the Commodore 64 came out! To recap, Commodore BASIC V2 was roughly the same as a very early version of Microsoft BASIC for the 6502 processor. Apple used the same source code for their BASIC, but had the decency to add commands for colour, graphics, and sound. For more details about the common core code of these BASICs see http://www.pagetable.com/?p=46 Jack Tramiel, founder of Commodore, did a dirty deal with Microsoft back in 1977 that for a one off fee of US$25,000 Commodore could use their early version of 6502 BASIC on as many Commodore computers as they liked. This was the very beginning of mass market computers, so Bill Gates of Microsoft decided to accept the deal, instead of an alternative deal of US$3 per each computer sold by Commodore which used this BASIC. That meant that unless or until Commodore sold more than 8,333 computers, Microsoft wouldn’t lose out. No one could predict how popular computers would become. Little did Bill Gates realise that Jack Tramiel was so miserly, he would continue to use this version of BASIC on 3 ranges of computers, the Commodore PET/2000/4000/8000, Commodore VIC-20, and Commodore 64 for 7 years until leaving Commodore, then it wouldn’t be until 17 years later that the Commodore 64 ceased production in 1994! Commodore somehow adapted the original Microsoft 6502 BASIC for use in their computers, starting with what they called “Commodore BASIC V1”, then making some very minor changes for “Commodore BASIC V2”. In the meantime, Commodore produced and sold cartridges offering some extra commands that should have been built in to their computers. These were called the VIC-20 Super Expander, the Super Expander 64, and Simons’ BASIC,. There were a lot of third party extended BASICs as well. Neither the cartridges nor any extended BASIC on tape could produce programs that would run on any Commodore 64 without the cartridge or a copy of the extended BASIC. Magazines tended not to list articles on programming in any extended BASIC, because there were so many different extended BASICs available. INPUT magazine had various listings in Simon’s BASIC, though.

Jack Tramiel only cared about cutting costs, while Clive Sinclair DID cut costs, but gave users of his computers reasonable versions of BASIC. Commodore started by making the PET computer, which was basically a cheap, cut down copy of an Apple ][ computer. This was followed up by the VIC-20, which was supposed to be better than just a video games console, because it had a keyboard and the same old Commodore BASIC V2 built in. It was also a way to make use of a crappy old video chip called the VIC-I, which Commodore had developed back in 1977, but failed to find a market for. After this, Commodore decided to make a cheap copy of the Atari 800 computer, which they called the Commodore 64. Meanwhile, Clive Sinclair and Sinclair Research made something like a cheap copy of a Tandy TRS80, but with its own BASIC instead of a Microsoft BASIC, and called it the ZX80. It had a black and white mono display, like the TRS80 and could do rough, blocky graphics, also like the TRS80. It did this with the PLOT command, while the TRS80 used the SET command.

Now let’s get back on track with the very daunting task of drawing a line across the screen in Commodore BASIC V2. To attempt this, I’ll be consulting only sources which were published in or before 1984, when I got a Commodore 64. Before the beginning of 1985, I’d given up on it and was drooling over the prospect of getting an Amstrad CPC and/or an MSX. At the moment I’m thinking of “Your 64” magazine, as well as the books “The Working Commodore 64” by David Lawrence, and “Graphic Art on the Commodore 64” by Boris Allen. I read these books and magazines back in 1984. I can make a list of those all important memory locations to PEEK and POKE from there.

Don’t forget, here’s how to do this in Atari BASIC, a non Microsoft style BASIC, which came out in 1979!

10 GRAPHICS 8+16:REM HIRES MODE WITHOUT TEXT WINDOW
15 SETCOLOR 0,0,0:SETCOLOR 2,10,15:REM BLACK TO DRAW ON LIGHT BLUE BACKGROUND
20 COLOR 1
30 PLOT 0,0
40 DRAWTO 319,190
50 GOTO 50

The Commodore 64 has a lores graphics mode of 160×200 dots or pixels and a hires graphics mode of 320×200 pixels. I will attempt to use either of these modes to draw a single line across the screen. The lores screen is multicolour mode, meaning that it can display up to 4 colours in each 8×8 pixel character square. The hires mode can only display 2 colours in each 8×8 character square. These display modes each take up 8K. The graphics origin is the top left hand corner of the screen, the same as most other computers of the time.

Some relevant memory locations are as follows –

1024-2023 – these locations seem to be connected with mapping the screen into character cells, even for the lores and hires graphics screens

53265 – POKE this location with 32 to enter bit mapped mode. Perhaps you could use the variable SM to stand for screen mode, because of course, only the first two letters of any variable in Commodore BASIC V2 are counted.

53272 – register used to control where the VIC-II video chip gets character data OR what address to use for the start of RAM for the hires screen. POKE with 8 to set the address to 8192. Perhaps the variable SA could stand for this. On some computers the address is fixed, but there’s a choice of locations on the Commodore 64 and some other computers. This enables quick switching between screens that have already been drawn. I think the Atari 8 bit also has this, while the Amstrad CPC screen RAM is fixed at &C000 or 49152.

Don’t forget, here’s how to draw a line across the screen in BBC BASIC, which should run not only on the BBC Micro, but the Acorn Electron, Acorn Archimedes, BBC BASIC for Windows, and possibly on Brandy BASIC for Linux including the Raspberry Pi computer as well. Different versions of BBC BASIC may give slightly different results. I tested this on BBC BASIC for Windows running under WINE in Linux. Unfortunately, Brandy BASIC locked up or crashed on me!

10 MODE 1
20 PLOT 1,1279,0
30 DRAW 0,511
40 GOTO 40

Your 64 magazine Issue No. 1 had an article about plotting points and drawing lines on pages 61-62. You can find a copy of this magazine on http://awesome.commodore.me/?pagename=magazine&pathname=magazine&displayname=&picsperpage=20&searchterm=Your_64 . Unfortunately, the listing about plotting points didn’t actually work, so that’s not much good, is it? Obviously, I have no way to debug it, because to do that I would actually have to understand how the crappy Commodore BASIC V2 works on the C64 in the first place! For some strange reason, the listing about drawing lines between random points on the graphics screen DOES work, though. The most important part of each of those two programs seems to be the equation B=8 1 9 2 +I N T ( Y/ 8 ) * 3 2 0 + I N T ( X/ 8 ), where B represents the pixel to be turned on, 8192 is the beginning of the graphics screen RAM., and X and Y represent the horizontal and vertical screen coordinates, the same as on NORMAL computers!

I bought the book “The Working Commodore 64” by David Lawrence, in 1984, because I thought it would enable me to make use of sprites and graphics screens, due to the summary on the back of the book, which mentioned graphics. Unfortunately, it was very disappointing because the graphics drawing program listed lacked a SAVE function! Author David Lawrence claimed it would be “a simple matter” to add this to the program, so obviously this actually meant it was virtually impossible and the program was useless to me, because I’d lose any drawing at the end of a session. Thanks for nothing David! The Commodore BASIC V2 SAVE command doesn’t save blocks of RAM, only BASIC programs. MSX BASIC and GW-BASIC have BSAVE to do this. His sprite program was just an editor which enabled users to edit, save, and load sprites, but there was nothing else about what to do with the sprites in a program! It contained a save routine involving the commands OPEN and PRINT#, but the sprites were stored in the cassette buffer and I have no idea if or how this technique could be adapted to save a graphics screen. In short, the whole book was crap. I don’t think any follow up answering these questions was ever published. David Lawrence also co wrote the book “Commodore 64 Machine Code Master”, but there was no point learning Machine Code just because Commodore BASIC V2 was crap. It was far less stressful just to buy a different computer instead. A quick look at the contents page of this book reveals that it’s mainly concerned with a type in Assembler/Disassembler/File Editor which allows you to extend the inadequate Commodore 64 BASIC V2 by adding 14 new commands, but unfortunately it seems NONE of these commands were for colour, graphics, or sound, which is what Commodore 64 programmers were crying out for. It did actually include the commands BSAVE and BLOAD, which I suppose it may have been possible to use in conjunction, with the annoying, incomplete graphics program mentioned above.

The way to plot points on the graphics screens using Commodore BASIC V2 seems to be by doing quite complicated mathematical equations, similar to the geometry problems I used to get kept behind after class for. Some important points from this book are as follows, but rephrased due to Copyright, but it’s fun just to expose the thinking behind this system.

You need to know how the Commodore 64 hires bitmapped screen is organised, because Jack Tramiel was too mean to pay US$3-US$10 per Commodore 64 sold for a later version of BASIC with built in graphics commands. If you find this too stressful, and/or aren’t good at maths, then just go out and buy a Sinclair Spectrum, a BBC Micro, an Acorn Electron, a Dragon, a Tandy Color Computer, an Oric, or an Atari instead. The C64 hires screen has 320×200 pixels, a total of 64,000 pixels. To store each of these separately you need 8,000 bytes, meaning 64,000 bits. Each character position needs 8 bytes. Starting from the top left hand corner of the screen, the first 8 bytes (0-7) are used for what on the text screen would be the first character position, the second eight bytes are the second character based 8×8 grid, and so on. With 40 characters positions for each line, each line takes up 320 bytes. The line of 8×8 grids can hold 8 lines each a single pixel wide, but drawing them all would look like a solid bar.

This seems to me to mean that if you want to draw a line from the top left hand corner of the screen to the bottom right hand corner of the screen, then after drawing the first 8 pixels, what follows in the screen RAM is the top left hand corner of the second 8×8 pixel block on the top row of the screen. If you carry on like this, then it will look like you’ve printed a row of backslash characters across the top row, then you’ll just continue on each row following this until the whole screen is full of these backslashes. The first pixel should be located at location 8192, followed by 8201 for the next pixel down and one to the right, followed by 8210, 8219, 8228, 8236, 8245, and 8254. At this point, you have to calculate the location in RAM for the 8×8 character based cell in the second position of the second row. This should be 41 bytes higher up in RAM. The screen has been set up to start at memory location 8192, so that should give us location 8233, or should it? Apart from this, do we POKE each location with a number from 0-15 representing a colour, or with a bit pattern telling it which dots to turn on, which are 1 pixel down and to the right of the previous pixel we’ve set?

I recently re read parts of the “Commodore 64 Programmer’s Reference Guide”, which was supposed to be like the Bible of Commodore 64 programming. I think the pages looked like it was designed in the 1960′s and that was the way Commodore wanted it to look, in line with their antique BASIC, although that was from 1977. Actually, it doesn’t tell you that much. They had a section on graphics which used SIN and COS functions. Obviously, I hated these because I don’t understand geometry and I used to keep getting kept in half an hour after maths classes because of this. It also meant that these examples of plotting points were useless to me! I think TMR of c64crapdebunk.wordpress.com probably loves geometry!

I’m afraid I’ll have to leave it there for now. I’ve nearly finished what I thought would be Part 1 in this series, demonstrating how to plot a series of points, or in other words a line in Commodore 64 BASIC V2, but the article seemed too long and involved, so I’ve decided to split it into two parts. The second part will be posted in the next couple of days, so look forward to that. Meanwhile, just digest the information in this post.

Advertisements

Posted September 16, 2013 by C64hater in Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: