Sunday, August 6, 2017

Arm the Lasers!

Now that I've just about exhausted the remaining free space in the MC-10's 8K ROM I may take some time to look at the VTEC VZ200 ROM. The message title comes from the fact that the machines were also released as the VTEC Laser if you hadn't figured that out already.

The VZ200 used a blatant ripoff of the LEVEL II BASIC ROM on the TRS-80 Model I.  They just made patches to the ROM to disable some of the commands and to add support of the different hardware.  They even filled in some unused space with sequential numbers.  Something like $FF, $FE, $FD, $FC, etc...  When you are ripping someone off, you don't want to make it too obvious right?

One thing they didn't do, is they didn't actually remove the code for the commands, they just removed them from the keyword table used by the tokenizer.  I released a ROM image back in 2007(?) with the startup message BASIC+ that I had patched with the a hex editor to add the keywords back in. (Someone had patched this before but the ROM image was apparently lost to time)  Any program written under BASIC+ that uses those commands will still run on the original ROM because the commands still have pointers in the token jump table!  This makes it much easier to port programs from LEVEL II BASIC.  There's just one little problem.  The screen editor and line buffer are shorter than the one on the TRS-80.  So you still can't just type in a lot of programs.

I spent a couple months worth of evenings creating a commented disassembly of the ROM that could be reassembled just like the MC-10 ROM I've been working on.  I had the idea of fixing a few things in the ROM including the short line length, and to reassemble the code with the unused space in a single block I could put something else in.  Sadly, that work was lost in a fire, but I may give it another go soon.  This time I'll probably automate the process a bit using comments from a disassembly of the TRS-80 LEVEL II BASIC ROM and a program to grab the comments from a scan and append them to the end of the lines from the disassembly.
The process shouldn't take long once I have the automation code worked out.  The disassembly will have to be recreated first.  FWIW, I don't plan on modifying the line buffer and editor to handle longer lines.  I have a program on the PC that will tokenize them and they will run just fine once they are loaded.  You can't edit the long lines, but it should run them just fine.  But I can still add a few optimizations I used on the MC-10 ROM to speed it up a little.  I can even create an HD64180/Z180 version that uses it's hardware multiply.

I do have several other projects I may work on.  It might be nice to write an entire project in a high level language again.  :D

The fix is in!

This took under a minute to implement.
It makes no noticeable difference on Ahl's benchmark since that depends heavily on the speed of the floating point routines.  But you can see a noticeable difference on the LIFE program I've been using to compare against the old ROM.  Life only uses addition and subtraction so it's a pretty good tool to compare general execution speed.

16 was just the first value I decided to try.  I've tried 32 and it feels okay on the emulator, but I'd want to see if it feels okay with real hardware.  I think response will suffer if I go much higher.




BSCAN  rmb 1 ; BREAK key scan counter



LE519 dec BSCAN ; decrement the counter to see if we should scan for BREAK
bne LE519B ; skip it if it's not time to scan
ldaa   #16 ; the number of times we skip scanning for break
staa          BSCAN ; Reset the counter
bsr           LE566             ; check for BREAK or PAUSE keys

And the keyboard slips through my fingers...

Someone just reminded me of another optimization I planned for the new MC-10 ROM.

It seems that when I hit the OH CRAP I HAVE TO START OVER panic button in the last few days of the contest, I just kind of... sort of... maybe... might have *cough* forgot *cough* about it since I didn't have a written to-do list and hadn't actually written the code yet.  And this is a big one.

You see, Microsoft BASIC scans for the BREAK key during program execution.  In fact, it scans for the BREAK key every time is passes through the main interpreter loop.  Every... single... time.  In fact, it is actually the first thing it does when it passes through the main interpreter loop.  You can find the code in the source/disassembly labeled as LE519.

This is one of the worst time wasters in the ROM and it's in every version of Microsoft BASIC that I've looked at.  You don't need to scan for BREAK that often.  A hack for the TRS-80 Model 1 that disabled this in Level II BASIC supposedly sped up the interpreter considerably, and a patch for the CoCo 3 ROM that uses the keyboard interrupt sped it up a lot as well.

The fix is actually quite easy.  Just have a counter stored on the direct page, decrement it every pass and if it's not zero, BNE past the test.  If it is equal, reset the counter and call the check for BREAK.
That's only 4 new instructions. or around 8 bytes.
Then it's just a matter of tuning the counter setting so eliminates as many calls as we can get away with while keeping the BREAK key feeling responsive.

I believe this is commonly referred to as "low hanging fruit" and it should have been written first.
The good new is, there should still be enough free space in the ROM to implement it.
This should also fit in the original ROM just by dropping the programmer's name and/or the Microsoft Easter eggs.

FWIW, this code hasn't been implemented yet because:
1. It's simple enough I thought I could do it at the last minute
2. I was trying to free up ROM space for ELSE first and that also sped up the ROM.
3. I was trying to work out more difficult optimizations to see if I could work them into the contest.
4. I didn't write down the to-do list.


As far as space for more optimizations goes...

If I create a runtime only version of the interpreter, the break key test could be dropped entirely, CSAVE could be dropped, and part of the parser/tokenizer might go as well.  That would free up some space.  You'd have to write your code on the slower ROM and run it on the faster one after it's debugged.  Without the constant check for BREAK you'd have to wait for the program to perform keyboard input, or hit reset if you want to quit.  

If a sound chip were added, the SOUND() function could be made smaller.
The settings could probably be calculated and by using the hardware timer, the wait to turn off the sound could be simplified.  This seems really large for what it does.

At some point, I would prefer to just go to a 16K ROM and only be backwards compatible at the source level, or through machine language calls via the vectors at the end of the ROM.  I should just redo the Memory setup so interrupt vectors are below video RAM, programs are stored above the screen, and variables/stack are stored below the hi-res screen.  

Other thoughts...

Video really needed to start at a higher or lower memory address to give a larger contiguous block of memory to BASIC.. $1000 would have been a much better and the decoding logic wouldn't have been much different.  It's like they intentionally crippled the machine in as many ways as they could think of!  :/

One of the things I want to do in the future, is to support the 6303, and that would require moving the Floating point vector that resides at the same address as the illegal instruction interrupt vector for the 6303.
Is anything even using that?  I've already broken the memory layout of the floating point registers to make the multiply faster, though that's easy to fix.  

There should really be vectors for the entire floating point library.  Multiply, divide, SQR, ^, SIN, COS, TAN, conversion between integers and floats, etc...  This is one of the major flaws I see with the ROM.  Any program that wants to use these functions must assume they are at the original address.  If you want to use them for a floating point library for C, Pascal, whatever, your program can only run on the original ROM.

Saturday, August 5, 2017

Something different

From another project I've been working on.  A cross platform library for old computers.
This shows off the code that lets you print text on a "hi-res" graphics screen.  The code prints 64 characters per line on a 256 pixel wide screen or 80 characters on a 320 pixel wide screen.  I have versions for several machines.  The video shows the regular 6502 Atari version, 6803 MC-10 version, 65816 enhanced Atari version, and Z80 VZ200 version. Some of the code has been ported to the 68hc11, 6809, and several other machines.  As you can see, it's pretty fast for being software based, so it shouldn't slow down projects that use it.

The original goal for this part of the library was to create portable text adventure engines for the Infocom (Zork) amd similar games in C.  Being able to print in upper and lower case with a reasonable number of characters per line seemed like a good idea where most 8 bit computers only print 32 or or 40 characters per line, and many only support upper case text.  After loosing a lot of code in a house fire, the original project took a major step back.  Since then I've changed goals for the cross platform library.  I'll discuss that at some point in the future.

Part of the reason this caught my interest is it's something I wrote for an embedded system years ago. Versions of the Z80, 6803 an 6502 source code are already posted on the internet, but it's not the most recent versions of the code..
Here is a side by side comparison of the old and new MC-10 BASIC ROMs running the same program.  Microsoft's version is on the left, and my updated version is on the right.

Friday, August 4, 2017

The fast multiply seems to be working now.  It still needs extensive testing and I still have to fix a couple related issues, but that should be easy compared to what I've already had to fix.

So, how fast is it?  Well, we need to do some benchmarking to figure that out.

Ahl's benchmark was a popular BASIC benchmark back in the day.  It was one of the few speed comparisons for Personal Computers in the late 70s an early 80s.  It's not really a great benchmark by today's standards, but it did a decent job of measuring the speed and accuracy of the BASIC math library.  It does zero testing of things like string handling, array manipulation, etc...  Math yeah, no to just about everything else.

Ahl's Benchmark currently lists the accuracy of the new math library at 8.66413117E-4.  That's a higher error than the original ROM, but it's still better than the standard 6502 versions. (FWIW, the accuracy might improve before the ROM image is released.)  The benchmark takes 67 seconds, which is 52 seconds faster than the factory ROM.  That's almost a 44% faster.  In the Creative Computing article that published results for 140 different computers from fastest to slowest, the MC-10 was originally listed about 2/3 of the way down.  With this and the other optimizations I've made, it jumps past over 30 machines on the list and into the top half .  Radio Shack was closing these out for as little as $10 when it was discontinued even though it is faster than machines costing in the thousands at the time.

I also ran a sieve test.  With the first test, the new version is only slightly faster because sieve uses division rather than multiplication.  It totally depends on the other optimizations I've made which make a much smaller difference.  This brings up the possibility of another optimization.  It is normally faster to calculated the reciprocal of the divisor and multiply rather than to use division. Thanks to the hardware multiply, the difference should be even greater.  Example... if we want to do this  C = A/B it will be faster to do this C=A*1/B.   But isn't 1/B division?  It is.  This is mostly for pre-calculating constants you divide by or where the reciprocal can be calculated outside a loop the value is used repeatedly in.

That brings up another issue.  As of now, there are only 14 bytes of free space remaining in the ROM, and that's without printing "MICROCOLOR BASIC", so the 8K version is pretty much done unless I find some additional code size optimizations.  That's too bad, the fast SQR function would probably put the Ahl results under 60 seconds.  Even a faster ^ (power) function might do that.  If I could get it under 50 seconds, that's normally the realm of systems clocked twice as fast.  That's around what the Model 4 benchmarked at and it runs a 4MHz Z80.

I have to admit, it felt good to get this working!  This was one of my original personal goals for the project and it was a bit disappointing that I didn't have time to put it in the contest release.  Also, things weren't looking good this morning when Ahl's Benchmark listed accuracy at over 4000... that's with no exponent.  The error was that bad!  I tried looking up 6803, 6809, or 68hc11 floating point routines that use the hardware integer multiply and found nothing.  The CPU and it's siblings have been out almost 40 years and nobody has even tried this?  I couldn't even find something for other processors.  I'm not saying it doesn't exist, but I eventually gave up looking for it.  I deleted some code and wrote it from scratch... problem solved.

The code is pretty straightforward, but people might wonder why I did a couple things.   If you get to look at the code, just be aware there is one semi-clever thing in the multiply I did to save a few instructions, and you'll be wondering what the heck I was thinking until you figure it out.

Once a "final" release version is out, maybe I'll take some time to make this blog look a little neater.
I'm just not that into the whole blogging thing.


*edit*

I finally found a floating point library that uses the multiply instruction.  It's in the 68hc12 floating point library posted in the GNU 68HC11/HC12 group on yahoo.  The code makes a similar optimizations to the multiply as my code and uses the additional divide instructions from the 68hc12.

Thursday, August 3, 2017

After a couple month hiatus, I'm working on the last few optimizations that were written during the retro challenge contest but didn't make it into the code.  That includes a fast multiply that uses the MUL instruction in place of the usual looped add and shift code, and storing the pointer to the next line to speed up line parsing a bit among other things.

I don't think I said much if anything about the fast multiply during the contest because I didn't know if I had enough time or ROM space for it.  In other words, don't promise what you can't deliver.  I really wanted it to be in the contest release because that's where the most obvious speed improvement was going to come from.  Sadly, there wasn't enough time to finish it.  It still needs work, but initial tests show Ahl's Benchmark finishing in about 1 minute 7 seconds.  That's 52 seconds less than the Microsoft ROM, and over 30 seconds faster than the 1MHz 6502 machines.

The code that saves the address of the next line probably works, I just need to make sure the stack frame is correct.  This would have been in earlier, but I wanted to get the math optimizations right first.  Too many changes were introduced in a short time at the end of the contest and I had to back everything out and debug what I had time for.  I'm not sure it will fit anymore though.

There have been some other optimizations that may work their way into a release eventually.
I experimented with 16 bit memory moves.  Just one of those cut a full second off the startup time of a game.  It had a bug though, so it's on hold.  
The fast SQR function is way too big.  It will require a larger ROM, but it would make a noticeable difference on Ahl's benchmark and anything else that uses SQR.  Maybe if I create a 16K ROM.
A more intelligent ^ (power) function might offer a speedup as well, but I haven't looked at that yet..

If I just had another 1K-2K to work with.