Some fixes to 32016 emulation in B-Em

discuss bbc micro and electron emulators (including mame) here!
litwr
Posts: 218
Joined: Sun Jun 12, 2016 9:44 am
Contact:

Some fixes to 32016 emulation in B-Em

Post by litwr » Fri Jan 06, 2017 9:05 am

I have found that Carry flag is set wrong sometimes. So I have checked the sources. The problem is in the AddCommon function.

Code: Select all

static uint32_t AddCommon(uint32_t a, uint32_t b, uint32_t cin)
{
   uint32_t sum = a + (b + cin);
   if (b == 0xffffffff && cin == 1)
   {
      C_FLAG = 1;
      F_FLAG = 0;
   }
   else
   {
      switch (OpSize.Op[0])
      {
         case sz8:
            {
               C_FLAG = TEST(sum & 0x100);
               F_FLAG = TEST((a ^ sum) & (b ^ sum) & 0x80);
            }
            break;
         case sz16:
            {
               C_FLAG = TEST(sum & 0x10000);
               F_FLAG = TEST((a ^ sum) & (b ^ sum) & 0x8000);
            }
            break;
         case sz32:
            {
               C_FLAG = TEST(sum < a);
               F_FLAG = TEST((a ^ sum) & (b ^ sum) & 0x80000000);
            }
            break;
      }
   }
   return sum;
}
C_FLAG gets wrong value for negative terms. For example, let R0=2 and we use ADDQB =-1,R0. The code above will make 0xffffffff+2+0=1. The carry flag must be set but it is not because 1 & 0x100 = 0. :( IMHO the next code for CF will work properly.

Code: Select all

               C_FLAG = TEST(((a & 0xff) + (b & 0xff) + cin) & 0x100);
               C_FLAG = TEST(((a & 0xffff) + (b & 0xffff) + cin) & 0x10000);
               C_FLAG = TEST(sum < a || sum < b);         //this maybe used for all three cases      
The branch for (b == 0xffffffff && cin == 1) maybe removed. The overflow flag has the same problem. The arguments and result are always 32 bit. So the code for this flag maybe

Code: Select all

             F_FLAG = (a^b)&0x80000000 ? 0 : !!((a^sum)&0x80000000);
So all code maybe assembled to

Code: Select all

static uint32_t AddCommon(uint32_t a, uint32_t b, uint32_t cin)
{
   uint32_t sum = a + b + cin;
   C_FLAG = TEST(sum < a ||  sum < b);
   F_FLAG = TEST((a ^ sum) & (b ^ sum) & 0x80000000);
   return sum;
}
SubCommon should be also fixed.

User avatar
hoglet
Posts: 9232
Joined: Sat Oct 13, 2012 7:21 pm
Location: Bristol
Contact:

Re: Some fixes to 32016 emulation in B-Em

Post by hoglet » Fri Jan 06, 2017 10:08 am

Thanks for posting this. It does look like I messed this up!

It's likely this problem affects PiTubeDirect as well, as it's the same emulator code base.

Can I ask if you have a high level test case that was failing before and is now passing? If so, can you share this?

It's a shame there isn't a comprehensive test suite for the 32016, like the DORMANN tests for the 6502.

Edit: I'm not sure I agree with your simplified version. Surely the size of the operands (8, 16 or 32 bits) must have some bearing on the flags?

Dave

litwr
Posts: 218
Joined: Sun Jun 12, 2016 9:44 am
Contact:

Re: Some fixes to 32016 emulation in B-Em

Post by litwr » Fri Jan 06, 2017 12:02 pm

hoglet wrote:Can I ask if you have a high level test case that was failing before and is now passing? If so, can you share this?
I will be happy to share but there are several lines of my codes only. I tried a sequence

Code: Select all

L1  
  MOVZBD ='0',TOS
  CXP XWriteByte
  ADDQB =-1,MEM
  BCS L1
...
MEM FCB 3
It should print '0' three times but it printed '0' only one time. I began to check the sources...
It's a shame there isn't a comprehensive test suite for the 32016, like the DORMANN tests for the 6502.
32016 is much more complex than 6502... It will be a very big task...
Edit: I'm not sure I agree with your simplified version. Surely the size of the operands (8, 16 or 32 bits) must have some bearing on the flags?
B-em's code converts any integer value to 32 bit unsigned integer and then extracts if necessary byte or word. However you are right - there was a tiny mistake for a very rare situation. The proper code is a bit bigger in size.

Code: Select all

   uint32_t sum = a + b + cin;
   if (cin == 0)
      C_FLAG = TEST(sum < a || sum < b);
   else
      C_FLAG = TEST(sum <= a || sum <= b);
   F_FLAG = TEST((a ^ sum) & (b ^ sum) & 0x80000000);
I have recompiled B-em with this patch. Panos and my small code works. :) I have still not made the code for SUB it is not required in my program but I am curious to make it too.

litwr
Posts: 218
Joined: Sun Jun 12, 2016 9:44 am
Contact:

Re: Some fixes to 32016 emulation in B-Em

Post by litwr » Fri Jan 06, 2017 2:15 pm

It is not so easy... The code does not use 32-bit sign extension for the every case. :( So my previous fix must be fixed too. For example, the code

Code: Select all

case ADDQ:
         {
            temp2 = (opcode >> 7) & 0xF;
            NIBBLE_EXTEND(temp2);
            temp = ReadGen(0);
            temp = AddCommon(temp, temp2, 0);
maybe replaced by

Code: Select all

case ADDQ:
         {
            temp2 = (opcode >> 7) & 0xF;
            NIBBLE_EXTEND(temp2);
            temp = ReadGen(0);
            SIGN_EXTEND(OpSize.Op[0], temp);
            temp = AddCommon(temp, temp2, 0);
The similar change should be made before every AddCommon invocation. I have also made the code for SubCommon

Code: Select all

static uint32_t SubCommon(uint32_t a, uint32_t b, uint32_t cin)
{
   uint32_t diff = a - b - cin;
   if (cin == 0)
      C_FLAG = TEST(a < b);
   else
      C_FLAG = TEST(a <= b);
   F_FLAG = TEST((a ^ b) & (a ^ diff) & 0x80000000);
   return diff;
}
This code also assumes that a and b are 32 bit sign extended values.
Last edited by litwr on Sat Jan 07, 2017 9:23 am, edited 1 time in total.

litwr
Posts: 218
Joined: Sun Jun 12, 2016 9:44 am
Contact:

Re: Some fixes to 32016 emulation in B-Em

Post by litwr » Sat Jan 07, 2017 9:22 am

I also have to add that the disc #2 in the Panos installation discs from J.G.Harston's mdfs.net is not right. The files have improper names. So they should be renamed before usage.

Code: Select all

Ed6502 -> Ed6502-bbc
EdHelp -> EdHelp-dat
EdInit -> EdInit-dat
Edit   -> Edit-rif
InitDFS -> InitDFS-cmd
Link   -> Link-rif
Panos  -> Panos-lib
It maybe done by RENAME command, for example, RENAME Panos Panos-lib. Filenames are not case sensitive. I may report that Assembler, Linker and Editor work with B-em. However I had met strange behaviors of Assembler sometimes. It maybe caused by the fixed errors mentioned above.

User avatar
BigEd
Posts: 3082
Joined: Sun Jan 24, 2010 10:24 am
Location: West
Contact:

Re: Some fixes to 32016 emulation in B-Em

Post by BigEd » Wed Jan 11, 2017 11:33 am

Thanks for the fixes litwr - they work for PiTubeDirect so I've applied them there.

User avatar
fordp
Posts: 1076
Joined: Sun Feb 12, 2012 9:08 pm
Location: Peterborough, England
Contact:

Re: Some fixes to 32016 emulation in B-Em

Post by fordp » Tue Jan 31, 2017 1:24 pm

BigEd wrote:Thanks for the fixes litwr - they work for PiTubeDirect so I've applied them there.
Is there a reason these patches have not been applied back to the B-Em repo?
FordP (Simon Ellwood)
Time is an illusion. Lunchtime, doubly so!

User avatar
BigEd
Posts: 3082
Joined: Sun Jan 24, 2010 10:24 am
Location: West
Contact:

Re: Some fixes to 32016 emulation in B-Em

Post by BigEd » Tue Jan 31, 2017 1:56 pm

Well, for my part, I didn't even think of it! But possibly also, it would be good to have a solid test of the flags on the 32016, to test the real thing, the FPGA, this software model. Partly because flags are difficult, and partly because this has already been fixed once. Which might be the same statement!

User avatar
fordp
Posts: 1076
Joined: Sun Feb 12, 2012 9:08 pm
Location: Peterborough, England
Contact:

Re: Some fixes to 32016 emulation in B-Em

Post by fordp » Tue Jan 31, 2017 2:12 pm

As my master is dead I thought I would have a go at B-Em :(
FordP (Simon Ellwood)
Time is an illusion. Lunchtime, doubly so!

User avatar
hoglet
Posts: 9232
Joined: Sat Oct 13, 2012 7:21 pm
Location: Bristol
Contact:

Re: Some fixes to 32016 emulation in B-Em

Post by hoglet » Tue Jan 31, 2017 4:06 pm

There are several forks of B-Em around.

I'll try to push these changes to mine later today.
fordp wrote:As my master is dead I thought I would have a go at B-Em :(
What happened to your Master?

Dave

User avatar
fordp
Posts: 1076
Joined: Sun Feb 12, 2012 9:08 pm
Location: Peterborough, England
Contact:

Re: Some fixes to 32016 emulation in B-Em

Post by fordp » Wed Feb 01, 2017 7:10 pm

It died as I was attaching my serial debug lead to my internal Pi Direct.

I shorted two pins out on the serial connector with my scope probe and that was that.

I suspect the short caused a temporary BBC PSU issue and something died. Now the master shows just a cursor with no text at boot even with all but the main ROM and all not needed stuff stripped out.
FordP (Simon Ellwood)
Time is an illusion. Lunchtime, doubly so!

User avatar
hoglet
Posts: 9232
Joined: Sat Oct 13, 2012 7:21 pm
Location: Bristol
Contact:

Re: Some fixes to 32016 emulation in B-Em

Post by hoglet » Thu Feb 02, 2017 8:30 am

fordp wrote:It died as I was attaching my serial debug lead to my internal Pi Direct.

I shorted two pins out on the serial connector with my scope probe and that was that.

I suspect the short caused a temporary BBC PSU issue and something died. Now the master shows just a cursor with no text at boot even with all but the main ROM and all not needed stuff stripped out.
Do you mean the serial connector on the level shifter? I don't understand how a short to ground on that could take out the Master.

Is the Pi still working?

There was a recent thread where mw0uzo got his two Master working again:
Dead BBC Masters - ROM screwup leading to main system damage

Do you have any kind of a logic analyzer?

If not, there are some very cheap 16Ch ones available:
http://hobbycomponents.com/cypress/675- ... ser-bundle

With a suitable 40-pin DIP test clip, you should be able to capture the PHI2, DATA[7:0], RnW, SYNC, nRST from the 65SC12 processor and figure out what's going on.

It should also be possible to write a reverse disassembler for such a trace that would show the instruction stream. I'd be happy to help with this.

Dave

User avatar
fordp
Posts: 1076
Joined: Sun Feb 12, 2012 9:08 pm
Location: Peterborough, England
Contact:

Re: Some fixes to 32016 emulation in B-Em

Post by fordp » Thu Feb 02, 2017 12:27 pm

hoglet wrote:
fordp wrote:It died as I was attaching my serial debug lead to my internal Pi Direct.

I shorted two pins out on the serial connector with my scope probe and that was that.

I suspect the short caused a temporary BBC PSU issue and something died. Now the master shows just a cursor with no text at boot even with all but the main ROM and all not needed stuff stripped out.
Do you mean the serial connector on the level shifter? I don't understand how a short to ground on that could take out the Master.

Is the Pi still working?

There was a recent thread where mw0uzo got his two Master working again:
Dead BBC Masters - ROM screwup leading to main system damage

Do you have any kind of a logic analyzer?

If not, there are some very cheap 16Ch ones available:
http://hobbycomponents.com/cypress/675- ... ser-bundle

With a suitable 40-pin DIP test clip, you should be able to capture the PHI2, DATA[7:0], RnW, SYNC, nRST from the 65SC12 processor and figure out what's going on.

It should also be possible to write a reverse disassembler for such a trace that would show the instruction stream. I'd be happy to help with this.

Dave
Thanks Dave,

I have loads of test gear including logic analysers and two scopes one with logic analyser.

The two things I lack is good eye sight and time. I have ordered a replacement main board for my master so I will pop that in next week. I can keep my presumably broken one for spares then.

I have not tried the Pi but I would guess it is OK. I am just guessing that my master broke when I was attaching cables, I was also swapping keys around on my master keyboard.

Can anybody spare me a couple of master key switches?

Cheers.
FordP (Simon Ellwood)
Time is an illusion. Lunchtime, doubly so!

litwr
Posts: 218
Joined: Sun Jun 12, 2016 9:44 am
Contact:

Re: Some fixes to 32016 emulation in B-Em

Post by litwr » Thu Feb 02, 2017 3:50 pm

Sorry I forgot to upload the fixes.
Attachments
32016-carry-fix.zip
(13.4 KiB) Downloaded 28 times

User avatar
hoglet
Posts: 9232
Joined: Sat Oct 13, 2012 7:21 pm
Location: Bristol
Contact:

Re: Some fixes to 32016 emulation in B-Em

Post by hoglet » Thu Feb 02, 2017 3:55 pm

litwr wrote:Sorry I forgot to upload the fixes.
I think there is a type in ADDC:

Code: Select all

         case ADDC:
         {
            temp2 = ReadGen(0);
            SIGN_EXTEND(OpSize.Op[0], temp2);
            temp = ReadGen(1);
            SIGN_EXTEND(OpSize.Op[1], temp2);
            temp3 = C_FLAG;
            temp = AddCommon(temp, temp2, temp3);
         }
         break;
The second SIGN_EXTEND should be:

Code: Select all

            SIGN_EXTEND(OpSize.Op[1], temp);
Dave

User avatar
fordp
Posts: 1076
Joined: Sun Feb 12, 2012 9:08 pm
Location: Peterborough, England
Contact:

Re: Some fixes to 32016 emulation in B-Em

Post by fordp » Thu Feb 02, 2017 4:26 pm

hoglet wrote:
litwr wrote:Sorry I forgot to upload the fixes.
I think there is a type in ADDC:

Code: Select all

         case ADDC:
         {
            temp2 = ReadGen(0);
            SIGN_EXTEND(OpSize.Op[0], temp2);
            temp = ReadGen(1);
            SIGN_EXTEND(OpSize.Op[1], temp2);
            temp3 = C_FLAG;
            temp = AddCommon(temp, temp2, temp3);
         }
         break;
The second SIGN_EXTEND should be:

Code: Select all

            SIGN_EXTEND(OpSize.Op[1], temp);
Dave
Indeed the old code is wrong for sure!
FordP (Simon Ellwood)
Time is an illusion. Lunchtime, doubly so!

litwr
Posts: 218
Joined: Sun Jun 12, 2016 9:44 am
Contact:

Re: Some fixes to 32016 emulation in B-Em

Post by litwr » Thu Feb 02, 2017 7:53 pm

Sorry. :oops: Of course, it is a typo. I had also accidentally removed comments. :oops:
Attachments
32016-carry-fix-2.zip
(13.57 KiB) Downloaded 26 times

dp11
Posts: 1138
Joined: Sun Aug 12, 2012 9:47 pm
Contact:

Re: Some fixes to 32016 emulation in B-Em

Post by dp11 » Thu Feb 02, 2017 9:43 pm

I think line 23

Code: Select all

int nsoutput = 0;
Can be removed.

I also think in n32016_reset_addr , FSR should be cleared

bsdnazz
Posts: 32
Joined: Mon Nov 07, 2016 9:54 pm
Location: Twickenham, UK
Contact:

Re: Some fixes to 32016 emulation in B-Em

Post by bsdnazz » Tue Feb 07, 2017 2:22 pm

I've just tried to boot the PanOS 1.1 and 1.4 disk images http://mdfs.net/Mirror/Archive/Acorn/PanOS/ using a RetroClinic DataCentre for floppy emulation and a Rasberry Pi running with the SD card from Kjell Sundby.

Unfortunately both PanOS versions hang loading PanOS. Has any one got PanOS booting on the Pi?

dp11
Posts: 1138
Joined: Sun Aug 12, 2012 9:47 pm
Contact:

Re: Some fixes to 32016 emulation in B-Em

Post by dp11 » Tue Feb 07, 2017 2:33 pm

Firstly which version of PiTubeDirect are you using ?

bsdnazz
Posts: 32
Joined: Mon Nov 07, 2016 9:54 pm
Location: Twickenham, UK
Contact:

Re: Some fixes to 32016 emulation in B-Em

Post by bsdnazz » Tue Feb 07, 2017 2:41 pm

While my real 32016 hardly gets any further with the Data Centre. I get an "*** Exception (UND) at PC = XXXXXX" on both versions of PanOS. 000300 for V1.1 and 019A65 for V1.4

This may be due to my 32016 having Pandora 0.6 ROMS.

bsdnazz
Posts: 32
Joined: Mon Nov 07, 2016 9:54 pm
Location: Twickenham, UK
Contact:

Re: Some fixes to 32016 emulation in B-Em

Post by bsdnazz » Tue Feb 07, 2017 2:49 pm

dp11 wrote:Firstly which version of PiTubeDirect are you using ?
I'm not entirely sure! I bought a Tube adaptor from Kjell Sundby in Nov 2016 and ordered an SD card from him at the same time so it's what he was shipping then.

I see that with the Anaconda release of PiTubeDirect I only need a FAT32 formatted SD card so it looks like my first task is to get Anaconda booted and try that.

User avatar
fordp
Posts: 1076
Joined: Sun Feb 12, 2012 9:08 pm
Location: Peterborough, England
Contact:

Re: Some fixes to 32016 emulation in B-Em

Post by fordp » Tue Feb 07, 2017 5:44 pm

bsdnazz wrote:While my real 32016 hardly gets any further with the Data Centre. I get an "*** Exception (UND) at PC = XXXXXX" on both versions of PanOS. 000300 for V1.1 and 019A65 for V1.4

This may be due to my 32016 having Pandora 0.6 ROMS.
Do you mean 0.61 or are they really 0.6?
FordP (Simon Ellwood)
Time is an illusion. Lunchtime, doubly so!

bsdnazz
Posts: 32
Joined: Mon Nov 07, 2016 9:54 pm
Location: Twickenham, UK
Contact:

Re: Some fixes to 32016 emulation in B-Em

Post by bsdnazz » Tue Feb 07, 2017 5:54 pm

I will have to double check when I get home. They just have white pricing labels over the eproms with 'Hi' and 'Lo' written on them.

Edit - it's got 0.61 Pandora Roms.
Last edited by bsdnazz on Tue Feb 07, 2017 6:33 pm, edited 1 time in total.

User avatar
BigEd
Posts: 3082
Joined: Sun Jan 24, 2010 10:24 am
Location: West
Contact:

Re: Some fixes to 32016 emulation in B-Em

Post by BigEd » Tue Feb 07, 2017 6:25 pm

bsdnazz wrote:Has any one got PanOS booting on the Pi?
Just to answer this one: yes! Not just the full PanOS on an ADFS partition, but also mini-PanOS as seen on litwr's SSD images. (Edit: for example this.)
Last edited by BigEd on Tue Feb 07, 2017 6:35 pm, edited 1 time in total.

bsdnazz
Posts: 32
Joined: Mon Nov 07, 2016 9:54 pm
Location: Twickenham, UK
Contact:

Re: Some fixes to 32016 emulation in B-Em

Post by bsdnazz » Tue Feb 07, 2017 6:34 pm

BigEd wrote:
bsdnazz wrote:Has any one got PanOS booting on the Pi?
Just to answer this one: yes! Not just the full PanOS on an ADFS partition, but also mini-PanOS as seen on litwr's SSD images.
Can I beg for some dsd image files? An ADFS for tge datacentre ide would be ace too.

User avatar
BigEd
Posts: 3082
Joined: Sun Jan 24, 2010 10:24 am
Location: West
Contact:

Re: Some fixes to 32016 emulation in B-Em

Post by BigEd » Tue Feb 07, 2017 6:39 pm

Can't help with DSD - I'm too ignorant - but just possibly this 400M download will be an ADFS image for PanOS and DosPlus. (That link won't be live forever - I'm sure there's a more official one - but it might do for now.)

Edit: Dave posted a link to a 7zip compressed image for a 2G or larger SDCard here.

bsdnazz
Posts: 32
Joined: Mon Nov 07, 2016 9:54 pm
Location: Twickenham, UK
Contact:

Re: Some fixes to 32016 emulation in B-Em

Post by bsdnazz » Tue Feb 07, 2017 6:56 pm

BigEd wrote:Can't help with DSD - I'm too ignorant - but just possibly this 400M download will be an ADFS image for PanOS and DosPlus. (That link won't be live forever - I'm sure there's a more official one - but it might do for now.)

Edit: Dave posted a link to a 7zip compressed image for a 2G or larger SDCard here.
Thanks - I'm downloading them now. However, it looks like they are MMC image files and I'll have to see if/how they can be transferred to the RetroClinic DataCentre I have.

bsdnazz
Posts: 32
Joined: Mon Nov 07, 2016 9:54 pm
Location: Twickenham, UK
Contact:

Re: Some fixes to 32016 emulation in B-Em

Post by bsdnazz » Wed Feb 08, 2017 7:49 pm

I've downloaded and booted the Anaconda build of PiTubeDirect but it hangs as the same point as the previous version.

I'm begining to wonder if the DSD images I got from MDFS.NET are corrupt. Has anyone successfully run any of these?

Is there another source of PanOS DSD images?

User avatar
hoglet
Posts: 9232
Joined: Sat Oct 13, 2012 7:21 pm
Location: Bristol
Contact:

Re: Some fixes to 32016 emulation in B-Em

Post by hoglet » Wed Feb 08, 2017 7:55 pm

If you can work with .ssd images, these are known to work:
viewtopic.php?p=133413#p133413

Dave

Post Reply

Return to “8-bit acorn emulators”