DRAWING THE LINE (PART 2)   Leave a comment


The beginning of my BASIC V2 program to draw a line across the C64 hires screen

I feel I should start this post by telling you all the news that I’ve just managed to buy a Commodore 128 computer from eBay! I didn’t know if I was doing the right thing in bidding on this, but I was bidding on two or three of them, One of them came with a massive 5.25 inch floppy drive and lots of floppy disks, while this one doesn’t come with a floppy drive or anything else apart from the PSU. The C128 has three modes of operation, so it’s really three computers in one. It has its own native C128 mode, which is almost like a Commodore 64 that’s been fixed. I read this was in response to various requests or demands from Commodore 64 users. I suppose they didn’t want to lose the money they’d invested in their peripherals and software after finding out that the C64 was crap! It will be very interesting to see what I can do with Commodore 64 compatible hardware and a decent built in BASIC. Of course, it also has the C64 mode, which I think is more or less 100% compatible with Commodore 64 software. Apart from this, there’s the CP/M mode, which is CP/M Plus or V3.1, also supplied by Amstrad with their CPC6128, as well as their PCW series computers. This is CP/M with added GSX graphics, the ancestor of GEM, as used on some PCs, as well as the Atari ST. You’ll probably be reading on here how I get on with the C128. I wouldn’t dream of chopping it up with an axe, or chucking it on a bonfire, because at the end of the day it’s a USER FRIENDLY computer with a nice BASIC built in, which even allows you to control the SID synthesizer chip! Finally, I get all of this in a case very similar to the Amiga A500, as well as a PSU that looks identical to the Amiga A500 PSU. Actually, it’s the other way round because the C128 came out in 1985, while the Amiga A500 came out in 1987.

A Commodore 128 computer

Now it’s back to drawing a line across the screen in Commodore 64 BASIC V2. While looking at the details of how to open a hires graphics screen and set the colours in the “Commodore 64 Programmer’s Reference Guide”, I thought it would be fun to cycle through the screen and border colours, so here’s a listing to do just hat, which I developed on the WinVICE emulator V2.2 for Windows. I found out a few weeks ago that V2.4 had some bugs which put me off or even prevented me from using it. BTW, this listing doesn’t work under Commodore BASIC V7 on the C128. It does RUN, but it prints text characters on the screen instead of graphics.

Of course, I must comment on TMR’s stupid debunk of me giving notice a couple of days ago that I was about to post an article about how to draw a line across the C64 hires graphics screen. Commodore 64 BASIC V2 makes it impossible for the vast majority of people to actually draw a line across its hires or lores graphics screens. Of course, as he suggests, you CAN use a FOR…NEXT loop in a program to draw a line across the screen, but this is only AFTER you find out how to turn on and clear the screen, as well as whereabouts in the memory map the pixels you want to set are located. Apart from this, TMR has been refusing for about 13 months now to demonstrate to me how this is done, so why does he finally say he’s going to do it?

The end of my line drawing program

The following listing will open a hires graphics screen, which starts as usual in Commodore BASIC V2 graphics programs full of garbage or coloured squares. It then waits for more than several seconds, then slowly clears, before drawing coloured bars moving across, then down the screen, until the whole screen is the same colour, then it changes the border to that colour, before changing the screen colour from the top down again. Almost any other “home computer” of the time could do all this in a second or less, without you having to watch it clear the screen! It would do this with a simple command such as MODE 1 (BBC, Acorn Electron and Amstrad CPC), GRAPHICS 8 (Atari), SCREEN 2 (MSX), or it may have only one display mode, so wouldn’t need a command at all (Sinclair Spectrum). BTW, in the listing below REM statements which have a line all to themselves are all describing what happens in the previous line.

5 BASE=2*4096:POKE 53272,PEEK(53272) OR 8


10 POKE 53265,PEEK(53265) OR 32




25 FOR C=0 TO 15


30 FOR I=1024 TO 2023:POKE I,C:NEXT I





40 GOTO 40

All this does is show solid bars of colour going down the hires graphics screen, as well as the border changing at the end of each screenful of bars. But there’s something wrong here. It’s a bitmapped graphics screen, so how about plotting some points or drawing a line? Brace yourselves, because the following line does just that! Add this to the listing above.

33 POKE 8192,128:POKE 8193,64:POKE 8194,32:POKE 8195,16:POKE 8196,8

Then type RUN and you’ll be able to see in the top left hand corner that 5 pixels have been plotted in black!!!! OMG!!!!!!!! We can plot some additional pixels, each time by increasing the memory location to POKE by 1 (i.e. 8197 is next in the sequence), while each time halving the number to POKE it with, because this is a bit pattern, as in User Defined Graphics characters (UDGs) on most other “home computers” of the time. Let’s try adding this to our line.

33 POKE 8192,128:POKE 8193,64:POKE 8194,32:POKE 8195,16

I’ve gone through a lot of revisions and I’m typing and doing a bit of reformatting in a word processor now. Unfortunately, word processors nowadays display more than 80 columns. At some stage, without warning, the Commodore 64 will refuse to accept any more characters in a single numbered line, then any characters past the limit just won’t appear when you list the line. I thought the maximum line length was 80 characters, or two lines across the Commodore 64 40 column screen. Just to be on the safe side, we’ll continue on another line, but now I’ve counted the line above has only 55 characters, so it’s fine.

34 POKE 8196,8:POKE 8197,4:POKE 8198,2:POKE 8199,1

Run this program, then as before, the screen slowly clears, before colour cycling AND plotting 8 pixels from the top left hand corner of the screen, moving down and to the right with each successive pixel plotted. The top left 8×8 corner of the screen starts at RAM location 8192, which has to be POKEd with 128 to plot the leftmost pixel. This is followed by POKEing 8192 with 64, and so on for the first 8 RAM locations and first 8 pixels. After that, you have to start POKEing from somewhere else higher up in RAM, but it’s not straightforward working out what location this is. I found I had to do it by trial and error.

Don’t forget, here’s how to draw a line from the top left hand corner to the bottom right hand corner of the screen in Sinclair BASIC on the Spectrum

20 PLOT 0,170
30 DRAW 254,-171

Easy peasey lemon squeezey!

I was playing round with lots of revisions of the program, some of them couldn’t be reloaded and some of them didn’t get saved. Here’s the latest version of the program, which has moved on quite a bit from the last version. I’ll have to explain the additional POKEs to higher memory locations to you. After POKEing 8199 with the number 1, I continued by POKEing 8200 with 128, followed by the next few memory locations with 64, 32, and 16 to check what would happen. As I previously predicted, the pixels this set were in the character cell to the right of the cell where I’d been plotting before. I had to find the memory location of the cell on the next row and one column across. I needed a formula for doing this. No formula I tried seemed to work, but I eventually found by trial and error that the next memory location to POKE to continue my line was 8520. The relationship between this number and the start of the screen memory at 8192 is that 8520-8192=328. This tells me that whenever I want to start plotting in a character cell which is one column to the right and one row down from the character cell I’ve just been plotting in, then I need to add 328 bytes to the address of the lowest byte of my last visited character cell. The memory locations where each character cell I use for my long line starts in the program below are as follows – 8192, 8520, 8848, 9176, 9504, 9832. Let’s check if there’s really a recurring pattern there, because it certainly didn’t seem straightforward when I was trying to work it out! 8520-8192=328 (checked again and it’s correct!), 8848-8520=328, 9176-8848=328, 9504-9176=328, 9832-9504=328!

So, from the above, I think it’s safe to conclude that when drawing a diagonal line from the top left hand corner to the bottom left hand corner of the C64 hires screen, after POKEing each character cell of 8 bytes with the sequence of numbers 128, 64, 32, 16, 8, 4, 2, and 1, THEN you continue with your line by adding 328 to the memory location of the beginning of the previous character cell you plotted in. I also get the impression that when you want to draw a diagonal line from anywhere on the screen down and to the right at an angle of 45 degrees, then when you reach the next character square you also add 328 bytes to the start of the previous character square plotted to continue with your line. This will be explored in a future post.

The following program is adapted from the program listed above, but various changes, line renumbering, and additions have been made. If you’re careful, you can edit the program you’ve already typed. It will display the bit mapped screen, clear it, draw two lines, one much longer than the other, then cycle through all the colours for the screen and border, while the lines stay black. I can’t change the colours of the lines yet, because I’m not sure where to POKE, or how the foreground colour was set to black for the whole screen. Unfortunately, there has been some automatic reformatting in lines 90 and 100 of this program. Please ignore the occurrences of several spaces at a time. Only ONE space at a time should ever occur in the middle of these program lines!

5 BASE=2*4096:POKE 53272,PEEK(53272) OR 8:REM PUT BITMAP AT 8192



30 FOR C=0 TO 15

40 FOR I=1024 TO 2023:POKE I,C:NEXT C

50 POKE 53280,C

60 POKE 8192,128:POKE 8193,64:POKE 8194,32:POKE 8195,16:POKE 8196,8:POKE 8197,4

70 POKE 8198,2:POKE 8199,1:POKE 8200,128:POKE 8201,64:POKE 8202,32:POKE 8203,16

75 POKE 8204,8:POKE 8205,4:POKE 8206,2:POKE 8207,1

80 POKE 8520,128:POKE 8521,64:POKE 8522,32:POKE 8523.16:POKE 8524,8:POKE 8525,4

85 POKE 8526,2:POKE 8527,1

90 POKE 8848,128:POKE 8849,64:POKE 8850,32:POKE8851,16:POKE9952,8:POKE 8853,4

95 POKE 8854,2:POKE 8855,1:POKE 9176,128:POKE 9177,64:POKE 9178,32:POKE 9179,16

96 POKE 9180,8:POKE 9181,4:POKE 9182,2:POKE 9183,1:POKE 9504,128:POKE 9505,64

97 POKE 9506,32:POKE 9507,16:POKE 9508,8:POKE 9509,4:POKE 9510,2:POKE 9511,1

98 POKE 9832,128:POKE 9833,64:POKE 9834,32:POKE 9835,16:POKE 9836,8:POKE 9837,4

99 POKE 9838,2:POKE 9839,1

100 POKE 10160,128:POKE 10161,64:POKE 10162,32:POKE 10163,16:POKE 10164,8

110 POKE 10165,4:POKE 10166,2:POKE 10167,1

1000 NEXT C

1010 GOTO 30

1020 GOTO 50

Of course the program can be shortened by using FOR…NEXT loops, as well as READ and DATA statements, but I thought I should show you it the way I developed it, with all the trial and error. Perhaps your homework could be to rewrite the program using the variable BASE, one or more FOR…NEXT loops, as well as READ and DATA statements. It should be quite simple. In this case, I really mean it. No David Lawrence style mental torture here!

Of course, I think it’s a MIRACLE that I’ve managed to work out how to draw a line across the C64 hires screen and I couldn’t care less if TMR or any of his mates say it’s the WRONG way to do it. At the end of the day, IT WORKS! That’s all that matters. The general impression I get is that only a small percentage of people who have ever owned a Commodore 64 could write a program to do this.

I think that’s enough for Part 2 in this series! Next, how to move your line into more character cells, constantly getting closer to that elusive bottom right hand corner, 8000 bytes away from where we started! After this, I’ll try to draw some lines elsewhere on the screen, but I don’t know if I’ll ever manage to translate the famous BBC BASIC “Rotating Squares” program, as used on the TARDIS console monitor in classic Doctor Who to represent travel through the Time Vortex. If I attempted this, I may just end up like the character Vedek Bareil in Star Trek: Deep Space Nine!

Posted September 17, 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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: