Tag Archives: C128

Everything about the Commodore 128, or the C128, or the CBM128. It featured BASIC 7.0. I never had one, but my friend Frank did – complete with Modem and 80 column monitor. Buy did we have fun dialling up those BBSes after school.

How to use SD2IEC: a quick command reference

Photo 28-08-2014 19 30 57
I’ve ordered an SD2IEC a few weeks ago from Nic over at http://sd2iec.co.uk. It’s an SD card reader for Commodre computers and emulates many features of the 1541 Floppy Disk Drive.

I went for the Limited Edition made from authentic recycled C64 plastic – so this little critter used to be a real C64! This has to be one of the coolest gadgets for any Commodore fan in the 21st century. And we know many people like gadgets of any kinds, to do many different things, that’s why we have lists of Electronics & Gadgets – Top9Rated.

Nic and several other sellers on eBay build the hardware, while the software was developed by Ingo Korb with contributions from others. In this article I’ll explain how you can get the most out of the SD2IEC.

Reading and Writing Files

The SD2IEC works out of the box with standard disk commands (such as LOAD and SAVE). This will save a BASIC programme as PRG just like your Commodore would save it to floppy disk.

You can create, switch into and remove directories on the SD card with your SD2IEC (or your “contemporary” computer of course). You can also read from and write to D64/D71/D81 image files and I’ll explain how this works further down.

When you’re in a subdirectory (or a disk image) all read/write operations are performed there until you switch images or directories. We can do this by communicating with the SD2IEC via the command channel, as if we’re speaking to a 1541.

Looks like the SD card is hot-swappable without the need to “safe-eject” as long as none of the lights are on, indicating read/write access.

Speaking to your SD2IEC

To issue commands on your SD2IEC we must open that trusty old command channel on the device (number 15), PRINT# the command and then optionally close the channel again. Here’s how to do that:

You can also write this on a single line:

You can also leave channel 15 open and issue more commands. Notice though that if you use any other disk command after opening channel 15 (such as LOAD or SAVE) all channels including 15 are closed automatically. Commands such as DIRECTORY do not close the channel.

I find it helpful to add these things to one my my programmable keys (C128 and Plus/4) via the KEY command:

Hit F1, then type one of the commands below. If the channel is already open, simply issue the command via PRINT#15,”command”.

Directories

The CD command lets you navigate the directory structure much like you would in Linux and Windows from the command line.

You can also create directories to stay organised without the need for other tools. That’s where the MD command comes in handy:

When you’re done with a directory you can delete it with RD. Note that only empty directories can be deleted, otherwise you’ll get a FILE EXISTS error:

Mounting Disk Images

The CD command can also be used to mount D64/D71/D81 image files, just as if they were standard directories. The same syntax applies as with switching directories:

You don’t have to mount disk images of course and can instead use a FAT or FAT32 formatted SD card just like it was a floppy disk. However, CBM DOS can only address a maximum of 144 files in a directory which means that a cheap 4GB SD card will be exhausted by this limitation before it’s anywhere near full.

Changing the Device Address

By default the SD2IEC is set to be drive number 8. But like its vintage counterpart you can change this to 9,10 or 11 using the following command:

Replace the CHR$ value with the desired drive number. Just like a real 1541 drive, the SD2IEC will not remember this change upon reset. You can however save the above to its internal EEPROM which will survive subsequent reboots:

Replace 9 with your actual drive number.

Troubleshooting

If you encounter flashing lights on your device, then the SD2IEC is trying to tell you something. On the C128 and Plus/4 you can read out the disk error channel by looking at the DS$ system variable:

This will show you what went wrong (FILE EXISTS, FILE NOT FOUND, etc). On the C64 this is a little harder and requires you to write a small programme to read those values out. This is necessary because the INPUT# command can not be used in direct mode:

There’s a lot more you can do with this device – check out the full documentation in the README file at Ingo Korb’s website:

How to locate and set the cursor on your Commodore C128

Commodore LogoSadly the Commodore machines don’t offer a routine to locate or set the current cursor position via BASIC. There is however a Kernel routine named PLOT which can do this in Machine Language.

Here’s how we can utilise it.

Getting the Cursor Position

This snippet sets the carry flag, calls the PLOT routine at $FFF0 and returns the cursor position in the X and Y registers. We’ll put them in a safe place into $1400 and $1401 to use.

You can call it from BASIC with

Setting the Cursor Position

Call it from BASIC by POKEing your desired coordinates into $1400 and $1401, then call SYS DEC(“1310”).

This snippet will do the reverse of the above: populate the X and Y registers from our safe place and then call PLOT. We clear the carry flag first, because it decides if the position is read (carry clear) or set (carry set).

PLOT should work fine on the C64 and Plus/4 as well but I didn’t get a chance to test it yet.

How to merge BASIC programmes on your Commodore C64, C128 and Plus/4

Commodore LogoYou can merge / combine / concatenate BASIC programmes on a Commodore computer. It’s often useful to develop shorter chunks of code and put them together for a larger app.

Sadly there’s no built-in command that lets you do this, even though the C128 user guide hints that the CONCAT command can do this (if you’ve ever tried you know that this is not the case and only works for sequential data files).

With a few peeks and pokes we can accomplish what we need. Here’s how:

  • after loading the fist programme, we’ll set the start of BASIC to the end of programme 1
  • then we’ll load the second programme
  • and finally return the start of BASIC to the first one

We’re left with both programmes in memory – without any changes in line numbers. Depending on how you write the code a quick RENUMBER command may be necessary here. Even though you can run a programme when the line numbers are out of sequence, GOSUB and GOTO routines may not work as expected.

The commands are slightly different for each machine.

On the C64

The start of BASIC is stored in 43/44, and ends just before the variables begin – which is stored in 45/46. Load your first programme, then type this:

Load the second part, then reset the BASIC pointers to where the first part resides:

On the Plus/4

The Plus/4 works almost identical to the C64 in regards to storing the BASIC pointers, however the start of BASIC is at a different address. Load your first programme, then type this (just like on the C64):

Load the second part, then reset the BASIC pointers:

On the C128

The C128 stores variables in a different memory bank. Therefore it has a dedicated pointer to the end of BASIC which is missing on the other computers. The memory map is slightly different, and the start of BASIC is stored in 45/46. Load your first programme, then type this:

Load the second part, then reset the BASIC pointers:

And there you have it! Run any of these routines right after a reset or restart.

Thanks to Rick Kephart for this tip. You may also be interested in another (more complex) solution to this puzzle by Jim Butterfield in which he reads each byte and merges them back on disk.

How to load and save binary data on the C128 and Plus/4

Commodore LogoThanks to BASIC 7.0’s powerful commands we can load and save arbitrary sections of memory to disk using BLOAD and BSAVE.

This was useful for dealing with machine code as well as raw image data. Both commands work on the C128 and Plus/4, but sadly the C64 was lacking in this area.

BLOAD and BSAVE work much like DLOAD and DSAVE with the addition that you can specify a section of memory – like this:

BLOAD doesn’t need additional parameters and will load a file back into memory where it originally resided – just like the standard LOAD “FILE”,8,1 (with the 1 at the end). You can however provide the starting memory and make the file load somewhere else instead:

How to set the time on your Commodore C64 / C128

Commodore LogoDid you know that the Commodore computers had a built-in clock? It wasn’t an exact science, and it didn’t show the date, but it was able to display a 24hr clock.

It did this by counting how many seconds your machine was switched on, accessible in two variables TI and TI$ (short for TIME).

TI$ is a string value in the format HHMMss, counting seconds. 112347 would mean 11:23am and 47 seconds. TI$ will be 000000 when you switch on the system. You can set it just like any other variable to set the time:

TI counts the number of 60th seconds since the system was switched on (don’t ask):

Both values reset when you reset the system. Like all internal computer clocks it is relatively accurate, but does drift a second or two per day (we don’t notice this on modern systems because those talk to an NTP server once every day and sync themselves).

TI and TI$ are present on the C64, C128, Plus/4 and many other Commodore variants.

How to load a .PRG file in VICE

Commodore LogoUsually you’d attach a disk image to the VICE emulator and load one of several files from your virtual disk. Occasionally though you’ll come across single files ending in .PRG – those are the files that are contained on a disk image.

Remember those weird three letter abbreviations on the right hand side when you list your floppy directory?

DIrectory

Those are the extensions of single files (there’s PRG, SEQ, USR and REL), and the actual file name appears on the left.

VICE can attach disk images, but it can also display lose files we may have stored in a directory. All we need to do is tell VICE where that directory is and it will make it accessible to the virtual machine.

To do this, start your favourite emulator and head over to Settings – Peripheral Settings. Next pick the drive you’d like to attach the directory to (say Drive 8) and tick the box that reads “Use IEC Device”. Now pick the desired directory and you’re all set!

This will turn the entire directory into a disk image on the fly. Now you can list whatever is in said directory either with DIRECTORY or LOAD”$”,8 followed by LIST. Load and run your .PRG as you usually would.

Capture

This works fine on the C64 and C128 emulators. The Plus/4 seems to have a problem with this approach as of VICE version 2.4.

How to use the Commodore C128 in 2MHz FAST mode

Commodore LogoThe C128 could run twice as fast as its predecessor, the legendary C64. All you had to do was issue the FAST command, or switch back with the SLOW command to its default speed (1MHz).

One drawback of using FAST mode was that the VIC which powered the 40 column (standard) display wasn’t capable of such a high frequency and therefore would go blank when use go FAST. You had to work on the 80 column display to reap the benefits of a faster computer.

Here’s how to first switch into 80 column mode, then go fast:

and to switch back into 40 column slow mode:

Switching GRAPHIC modes was not necessary to perform long running calculations though – as long as that blank screen isn’t a problem for the user. Consider this:

During the “screen outage” keyboard commands were accepted as usual too, so you could type SLOW on the blank screen and see your 40 column display come back to life.

How to switch graphic modes on the Commodore C128

Commodore LogoThe C128 had various chips to display different kinds of graphics that man just stuck to the standard 40 column text display for “ease of use” and let their software handle the rest.

To appreciate why these modes existed one had to understand what was happening behind the scenes. Here are the various modes and how to enter them, followed by a brief explanation of why they came to be:

Optionally you could add a “clear graphics” parameter by adding a comma (like GRAPHIC 1,0 or GRAPHIC 5,1). 1 would clear the graphic portion while 0 would leave it in memory.

Split screen mode could be used when you wanted the top part of the 40 column display to show graphics and the bottom part text – much like the Magnetic Scrolls text adventures did on the C64:

Lab

You could define where this split would occur with another optional parameter between 0 and 25 (0 being no text, and 25 being all text). When no optional parameter was given the split would occur at line 20, leaving 5 lines of text underneath the “super high resolution multimedia graphics” (and an ugly flickering line where said split occured).

For example:

The 80 Column Display

While the VIC chip would take care of the 40 column text and graphic display just like it did on the C64, the 80 coilumn display was powered by a MOS 8563 chip which wasn’t officially supposed to display anything other than text. Even though the hardware did support graphics, BASIC 7 commands did not.

The good news was that this made a dual monitor setup possible – and thanks to VICE we can emulate exactly that. This makes it possible to write programmes on the 80 column display, while creating graphics “live” on the 40 column display. Back in the day with Commodore’s “Dual Monitor” you had to press a button to switch between modes and see your results (most of us just didn’t have two different monitors at our disposal).

Switch to the 80 column display by executing GRAPHIC 5, and type GRAPHIC 0 to switch back to 40 column mode.