ejolson
Posts: 8628
Joined: Tue Mar 18, 2014 11:47 am

Re: BBC BASIC on the Raspberry Pi Pico?

Wed Sep 22, 2021 1:49 am

scruss wrote:
Wed Sep 22, 2021 12:56 am
Hey, can we please have a sticky on where the current uf2s are? It took me ages to find what I was looking for, and even that may not be current: Re: BBC BASIC on the Raspberry Pi Pico? - 2021-09-11 18:41:25
I think having a sticky would be a good idea. As far as I know none of the existing uf2's available were built from the master repository. I've just updated my download, which is now built from a clone of master with the addition of the above mentioned strbin system call. The links are Please check the md5sum hashes, as there was a slight delay putting the files in the right place.

ejolson
Posts: 8628
Joined: Tue Mar 18, 2014 11:47 am

Re: BBC BASIC on the Raspberry Pi Pico?

Wed Sep 22, 2021 4:22 am

ejolson wrote:
Tue Sep 21, 2021 9:29 pm
That was easy and leads to a pretty quick way of adding any C extension that one wants.
In order to not clutter the present thread with more palindromes, the application of the strbin C extension developed above appears in

viewtopic.php?p=1916343#p1916343

Note that it was possible to almost double the speed of my previous BBC Basic program running on the Pico by replacing one function with a C version.

Although the Pico ceases to be a self-contained development environment when this approach is used, a more obvious drawback is the complicated nature of maintaining a program in which different programming languages have been mixed together. Even so, I'm looking forward to trying the same thing after some time using the inline assembler.
Last edited by ejolson on Thu Sep 23, 2021 4:43 pm, edited 1 time in total.

Memotech Bill
Posts: 380
Joined: Sun Nov 18, 2018 9:23 am

Re: BBC BASIC on the Raspberry Pi Pico?

Thu Sep 23, 2021 8:22 am

The stand-alone BBC Pico with USB keyboard and VGA output now has most of the features I intend to implement, although much more testing is required. Latest source code can be found here.

However I have an irritating problem. Writing a file to flash (e.g saving a program) crashes the video generation. The file is successfully saved, and in fact the interpretor is still running on core 0, it is just the video generation on core 1 that has crashed.

The problem does not occur if the file is on SD card, only on flash.

I need to see if I can either resolve the problem or see whether I can find a way of restarting the video generation afterwards.

Memotech Bill
Posts: 380
Joined: Sun Nov 18, 2018 9:23 am

Re: BBC BASIC on the Raspberry Pi Pico?

Thu Sep 23, 2021 8:43 am

Memotech Bill wrote:
Thu Sep 23, 2021 8:22 am
Writing a file to flash (e.g saving a program) crashes the video generation.
Not surprising, the SDK documentation says of the flash writing code
Note these functions are unsafe if you have two cores concurrently executing from flash. In this case you must perform your own synchronisation to make sure no XIP accesses take place during flash programming.
So either I need to ensure that all my core 1 code and data is in RAM, or find some way to make the core busy-wait in RAM while the flash is being programmed.

Memotech Bill
Posts: 380
Joined: Sun Nov 18, 2018 9:23 am

Re: BBC BASIC on the Raspberry Pi Pico?

Thu Sep 23, 2021 9:34 am

Memotech Bill wrote:
Thu Sep 23, 2021 8:43 am
So either I need to ensure that all my core 1 code and data is in RAM, or find some way to make the core busy-wait in RAM while the flash is being programmed.
There is a solution in the SDK, although for some reason it does not appear to be documented.

In my core 1 setup code, call:

Code: Select all

multicore_lockout_victim_init ();
And then surround the core 0 flash write or erase calls by:

Code: Select all

    multicore_lockout_start_blocking ();
    	:
    	:
    multicore_lockout_end_blocking ();
These routines are defined in "pico/multicore.h".

There is a noticeable loss of video signal while the flash is being programmed, but it does come back afterwards.

I discovered this while doing the research to write my own equivalent code.

ejolson
Posts: 8628
Joined: Tue Mar 18, 2014 11:47 am

Re: BBC BASIC on the Raspberry Pi Pico?

Thu Sep 23, 2021 2:59 pm

Memotech Bill wrote:
Thu Sep 23, 2021 8:43 am
Memotech Bill wrote:
Thu Sep 23, 2021 8:22 am
Writing a file to flash (e.g saving a program) crashes the video generation.
Not surprising, the SDK documentation says of the flash writing code
Note these functions are unsafe if you have two cores concurrently executing from flash. In this case you must perform your own synchronisation to make sure no XIP accesses take place during flash programming.
So either I need to ensure that all my core 1 code and data is in RAM, or find some way to make the core busy-wait in RAM while the flash is being programmed.
I thought the original idea was for core 1 to run entirely from RAM.

viewtopic.php?p=1907044#p1907044

Is that a lot of code?

Do the fill and ellipse drawing routines run on core 0? Other than the VGA refresh what does core 1 do?

Memotech Bill
Posts: 380
Joined: Sun Nov 18, 2018 9:23 am

Re: BBC BASIC on the Raspberry Pi Pico?

Thu Sep 23, 2021 3:34 pm

ejolson wrote:
Thu Sep 23, 2021 2:59 pm
I thought the original idea was for core 1 to run entirely from RAM.
It clearly does not run entirely from RAM as writing flash crashes the code. GDB reports "double fault".

I must admit I am not entirely sure why. The main VGA render_loop and the assembler routines that calls are RAM resident (confirmed from the map file). I can only assume that it must be somewhere within the SCANLINE code from the SDK.

I will have to have another look with GDB to see what I can deduce.

If it was in MODE 7 it would be more understandable as render_mode7 directly accesses the font definition, which is in flash.
ejolson wrote:
Thu Sep 23, 2021 2:59 pm
Do the fill and ellipse drawing routines run on core 0? Other than the VGA refresh what does core 1 do?
Yes, all the routines that write to the framebuffer run on core 0. Core 1 only renders the framebuffer to VGA.

MODE 7 is somewhat different in that the framebuffer actually contains character codes rather than pixels. As for the original BBC micro this means that only a very small buffer is required. Which would be good if I ever work out how to dynamically allocate the framebuffer.

Memotech Bill
Posts: 380
Joined: Sun Nov 18, 2018 9:23 am

Re: BBC BASIC on the Raspberry Pi Pico?

Thu Sep 23, 2021 7:28 pm

Memotech Bill wrote:
Thu Sep 23, 2021 3:34 pm
It clearly does not run entirely from RAM as writing flash crashes the code. GDB reports "double fault".

I must admit I am not entirely sure why. The main VGA render_loop and the assembler routines that calls are RAM resident (confirmed from the map file). I can only assume that it must be somewhere within the SCANLINE code from the SDK.

I will have to have another look with GDB to see what I can deduce.
It certainly appears to be a problem with the pico_scanline code. I have raised an issue https://github.com/raspberrypi/pico-extras/issues/26

ejolson
Posts: 8628
Joined: Tue Mar 18, 2014 11:47 am

Re: BBC BASIC on the Raspberry Pi Pico?

Sat Sep 25, 2021 4:45 pm

Memotech Bill wrote:
Thu Sep 23, 2021 7:28 pm
Memotech Bill wrote:
Thu Sep 23, 2021 3:34 pm
It clearly does not run entirely from RAM as writing flash crashes the code. GDB reports "double fault".

I must admit I am not entirely sure why. The main VGA render_loop and the assembler routines that calls are RAM resident (confirmed from the map file). I can only assume that it must be somewhere within the SCANLINE code from the SDK.

I will have to have another look with GDB to see what I can deduce.
It certainly appears to be a problem with the pico_scanline code. I have raised an issue https://github.com/raspberrypi/pico-extras/issues/26
Not being able to run core 1 out of RAM is bad news as this means writing any file to the Pico's internal flash disrupts the video. After two seconds of ZX80 nostalgia, this seems a significant problem with the SDK as it would disrupt any real time task one might want to reserve for the second core.

In other news, I'm really happy how well BBC Basic is running on the Pico. I have not seen a crash or any unexpected behavior in more than a week.

When the Pi first came out, I thought how great it was that it runs the exact same Linux software used on super computers, the cloud and my desktop. However, I may have been wrong. According to the dog developer all those my computer won't boot threads, computer security, backup, desktop disappeared and network doesn't work threads could have been avoided. Becoming a Linux system administrator, though good and useful, should not be a prerequisite to learning how to program.

The discussion turned to horses, forest fires, demilitarized air zones and then the silicon Armageddon before returning to Basic. Though not as revolutionary as the BARK™ with MEOW™ composable attachment running FidoBasic, BBC Basic on the Raspberry Pico is a reliable self-contained programming environment that allows any person to learn and experiment without first needing a certificate in Linux system administration. If only those other problems were as easy to solve.

tjc
Posts: 2
Joined: Fri Mar 29, 2013 11:57 am

Re: BBC BASIC on the Raspberry Pi Pico?

Sun Sep 26, 2021 8:31 am

Hi, As the Pico has a RTC can this be used to set the time and date.

At the moment TIME$ prints
Wed.31 Dec 1969,23:59:59
Trying to set the time with TIME$ = "12:00:00" results with Mistake being printed.

User avatar
RichardRussell
Posts: 1112
Joined: Thu Jun 21, 2012 10:48 am

Re: BBC BASIC on the Raspberry Pi Pico?

Sun Sep 26, 2021 9:37 am

tjc wrote:
Sun Sep 26, 2021 8:31 am
Trying to set the time with TIME$ = "12:00:00" results with Mistake being printed.
That's true in all my recent versions of BBC BASIC, the code doesn't even bother to check for the presence of a '$' when TIME is encountered in the left context. On most platforms setting a global property like date/time requires admin privileges, although of course that doesn't apply to the Pico.

If there is an RTC and a function is provided to set the date/time then it should be made accessible using SYS. Eric may correct me but I don't think an attempt has been made to produce a list of 'useful' OS/Clib functions that can be accessed that way on the Pico. Currently it's just the GPIO functions plus a couple of others like memcpy().

Memotech Bill
Posts: 380
Joined: Sun Nov 18, 2018 9:23 am

Re: BBC BASIC on the Raspberry Pi Pico?

Sun Sep 26, 2021 1:52 pm

ejolson wrote:
Sat Sep 25, 2021 4:45 pm
Not being able to run core 1 out of RAM is bad news as this means writing any file to the Pico's internal flash disrupts the video. After two seconds of ZX80 nostalgia, this seems a significant problem with the SDK as it would disrupt any real time task one might want to reserve for the second core.
I have so far identified two causes of the VGA code not being entirely in RAM:
  • Routine default_scanvideo_scanline_repeat_count_fn() not being declared _time_critical and therefore being in Flash. This can be fixed either by patching the SDK, or by calling scanvideo_set_scanline_repeat_fn() to use my own RAM resident routine instead.
  • My render_loop() included a few integer divisions which the compiler implemented as calls to routines resident in Flash. I have revised my code to avoid all integer divisions in the render loop.
However, having made those changes (and removed the multicore_lockout guards) I am now worse off. Saving to Flash now both corrupts the video (although the render_loop seems to be still running) and also crashes core 0:

Code: Select all

(gdb) rp2040.core0 -- clearing lockup after double fault
target halted due to debug-request, current mode: Thread 
xPSR: 0x21000000 pc: 0x200014e2 msp: 0x20041fc4

Thread 1 received signal SIGINT, Interrupt.
0xfffffffe in ?? ()
(gdb) bt
#0  0xfffffffe in ?? ()
#1  <signal handler called>
#2  0x1400000c in ?? ()
#3  0x20003166 in boot2_copyout ()
#4  0x00049000 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)
So far I have no clue as to the cause of that.
ejolson wrote:
Sat Sep 25, 2021 4:45 pm
BBC Basic on the Raspberry Pico is a reliable self-contained programming environment that allows any person to learn and experiment without first needing a certificate in Linux system administration. If only those other problems were as easy to solve.
It is ducking the problem, but for that application I would suggest that working off SD Card is perhaps more useful. You can swap cards for different projects, and for backups.
Last edited by Memotech Bill on Sun Sep 26, 2021 4:46 pm, edited 1 time in total.

ejolson
Posts: 8628
Joined: Tue Mar 18, 2014 11:47 am

Re: BBC BASIC on the Raspberry Pi Pico?

Sun Sep 26, 2021 2:03 pm

Memotech Bill wrote:
Sun Sep 26, 2021 1:52 pm
ejolson wrote:
Sat Sep 25, 2021 4:45 pm
Not being able to run core 1 out of RAM is bad news as this means writing any file to the Pico's internal flash disrupts the video. After two seconds of ZX80 nostalgia, this seems a significant problem with the SDK as it would disrupt any real time task one might want to reserve for the second core.
I have so far identified two causes of the VGA code not being entirely in RAM:
  • Routine default_scanvideo_scanline_repeat_count_fn() not being declared _time_critical and therefore being in Flash. This can be fixed either by patching the SDK, or by calling scanvideo_set_scanline_repeat_fn() to use my own RAM resident routine instead.
  • My render_loop() included a few integer divisions which the compiler implemented as calls to routines resident in Flash. I have revised my code to avoid all integer divisions in the render loop.
However, having made those changes (and removed the multicore_lockout guards) I am now worse off. Saving to Flash now both corrupts the video (although the render_loop seems to be still running) and also crashes core 0:

Code: Select all

(gdb) rp2040.core0 -- clearing lockup after double fault
target halted due to debug-request, current mode: Thread 
xPSR: 0x21000000 pc: 0x200014e2 msp: 0x20041fc4

Thread 1 received signal SIGINT, Interrupt.
0xfffffffe in ?? ()
(gdb) bt
#0  0xfffffffe in ?? ()
#1  <signal handler called>
#2  0x1400000c in ?? ()
#3  0x20003166 in boot2_copyout ()
#4  0x00049000 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)
So far I have no clue as to the cause of that.
ejolson wrote:
Sat Sep 25, 2021 4:45 pm
BBC Basic on the Raspberry Pico is a reliable self-contained programming environment that allows any person to learn and experiment without first needing a certificate in Linux system administration. If only those other problems were as easy to solve.
It is ducking the problem, but for that application I would suggest that working off SD cCrd is perhaps more useful. You can swap cards for different projects, and for backups.
When you removed the multicore lockout guards did you put the disable interrupts back in for core 0 when writing to flash memory? Sorry for the obvious question, but just thought I'd check.

Maybe the the VGA demo board has an SD card slot because nobody has been able to write to internal flash while the second core services real-time events.

Memotech Bill
Posts: 380
Joined: Sun Nov 18, 2018 9:23 am

Re: BBC BASIC on the Raspberry Pi Pico?

Sun Sep 26, 2021 2:49 pm

ejolson wrote:
Sun Sep 26, 2021 2:03 pm
When you removed the multicore lockout guards did you put the disable interrupts back in for core 0 when writing to flash memory? Sorry for the obvious question, but just thought I'd check.
Even with the multicore_lockout guards (which prevent core 1 from accessing Flash) you still need save_and_disable_interrupts() to prevent core 0 interrupt routines from attempting to access Flash. So I never removed the call to save_and_disable_interrupts().

As an aside, the SDK documentation does not specify. I assume that save_and_disable_interrupts() only disables the interrupts for the core on which it is called, not both cores.

ejolson
Posts: 8628
Joined: Tue Mar 18, 2014 11:47 am

Re: BBC BASIC on the Raspberry Pi Pico?

Sun Sep 26, 2021 3:04 pm

Memotech Bill wrote:
Sun Sep 26, 2021 2:49 pm
As an aside, the SDK documentation does not specify. I assume that save_and_disable_interrupts() only disables the interrupts for the core on which it is called, not both cores.
That's my understanding as well. It would be nice to find it in the documentation, as that would imply the assumed behaviour is less likely to change.

Memotech Bill
Posts: 380
Joined: Sun Nov 18, 2018 9:23 am

Re: BBC BASIC on the Raspberry Pi Pico?

Sun Sep 26, 2021 7:44 pm

ejolson wrote:
Sun Sep 26, 2021 3:04 pm
Memotech Bill wrote:
Sun Sep 26, 2021 2:49 pm
As an aside, the SDK documentation does not specify. I assume that save_and_disable_interrupts() only disables the interrupts for the core on which it is called, not both cores.
That's my understanding as well. It would be nice to find it in the documentation, as that would imply the assumed behaviour is less likely to change.
Found a one-liner near the top of section "4.1.14 hardware_irq" of "Raspberry Pi Pico C/C++ SDK"
That all IRQ APIs affect the executing core only (i.e. the core calling the function).

ejolson
Posts: 8628
Joined: Tue Mar 18, 2014 11:47 am

Re: BBC BASIC on the Raspberry Pi Pico?

Tue Sep 28, 2021 2:39 am

I was trying to implement a family of Weyl-sequence middle-square random number generators and got stuck not knowing how to multiply two 64-bit values to obtain a 64-bit result that has been reduced modulo 2^64. This is the way integer arithmetic works in many other languages but in BBC Basic I obtain the error Number too big.

I'm probably missing something obvious, but is there a way to perform integer arithmetic modulo 2^64 in BBC Basic?
Last edited by ejolson on Tue Sep 28, 2021 4:09 am, edited 2 times in total.

ejolson
Posts: 8628
Joined: Tue Mar 18, 2014 11:47 am

Re: BBC BASIC on the Raspberry Pi Pico?

Tue Sep 28, 2021 2:42 am

ejolson wrote:
Tue Sep 28, 2021 2:39 am
I was trying to implement a family of Weyl-sequence middle-square random number generators and got stuck not knowing how to multiply two 64-bit values to obtain a 64-bit result that has been reduced modulo 2^64. This is the default way integer arithmetic works in many other programming languages but in BBC Basic I obtain the error Number too big.

I'm probably missing something obvious, but is there a way to perform integer arithmetic modulo 2^64 in BBC Basic?
Aha! I found a hint here:

http://www.bbcbasic.co.uk/wiki/doku.php ... arithmetic

Can that be adapted to multiplication?

Another interesting discussion is at

https://stardot.org.uk/forums/viewtopic ... 54&t=15617

It would be helpful to see the code for the standard hash function mentioned in

https://stardot.org.uk/forums/viewtopic ... 03#p213403

as I'm not seeing how to use BBC Basic for any type of cryptography, hash or random number generator.

For example, how would I evaluate modulo 2^64 the polynomial defined by

Code: Select all

func ipoly8(x uint64) uint64 {
    x2:=x*x
    return (((x2*x2+
        0x4cca10b00bc5da27)*x2+
        0x872a4954ca3a3537)*x+
        0x2bf0e3ddcc4fb44f)*x+
        0xb5ad4eceda1ce2a9
}
in BBC Basic?

Sorry for cluttering this thread, but this problem came up during my testing.

pidd
Posts: 2798
Joined: Fri May 29, 2020 8:29 pm
Location: Wirral, UK
Contact: Website

Re: BBC BASIC on the Raspberry Pi Pico?

Tue Sep 28, 2021 5:46 am

For 64 bit multiply you might have to split into four 32 bit numbers, do three multiplications, shift left32 two of the numbers then add the other.

Unfortunately BBC basic error trapping doesn't let you resume on error which in some languages ignores the overflow.

ejolson
Posts: 8628
Joined: Tue Mar 18, 2014 11:47 am

Re: BBC BASIC on the Raspberry Pi Pico?

Tue Sep 28, 2021 6:15 am

pidd wrote:
Tue Sep 28, 2021 5:46 am
For 64 bit multiply you might have to split into four 32 bit numbers, do three multiplications, shift left32 two of the numbers then add the other.

Unfortunately BBC basic error trapping doesn't let you resume on error which in some languages ignores the overflow.
It seems I need to split the numbers into six 24-bit numbers as multiplication of 0xFFFFFFFF by 0xFFFFFFFF, for example, results in a 64-bit number which is too big.

The code

Code: Select all

 1000 *hex64
 1020 FOR i%%=1 TO 20
 1030   PRINT STR$~(FNipoly8%%(i%%))
 1040 NEXT i%%
 1999 END
 2000 DEF FNmul64%%(a%%,b%%)
 2010 LOCAL al%%,am%%,ah%%
 2020 LOCAL bl%%,bm%%,bh%%
 2030 LOCAL zl%%,zm%%,zh%%
 2040 al%%=a%% AND &FFFFFF
 2050 am%%=(a%%>>24) AND &FFFFFF
 2060 ah%%=a%%>>48
 2070 bl%%=b%% AND &FFFFFF
 2080 bm%%=(b%%>>24) AND &FFFFFF
 2090 bh%%=b%%>>48
 2100 zl%%=al%%*bl%%
 2110 zm%%=al%%*bm%%+am%%*bl%%
 2120 zh%%=(al%%*bh%%+am%%*bm%%+ah%%*bl%%) AND &FFFF
 2130 =FNadd64%%(FNadd64%%(zh%%<<48,zm%%<<24),zl%%)
 2200 DEF FNadd64%%(c%%,d%%)
 2210 LOCAL cl%%,ch%%
 2220 LOCAL dl%%,dh%%
 2230 LOCAL yl%%,yh%%,yc%%
 2240 cl%%=c%% AND &FFFFFFFF
 2250 ch%%=c%%>>32
 2260 dl%%=d%% AND &FFFFFFFF
 2270 dh%%=d%%>>32
 2280 yl%%=cl%%+dl%%
 2290 yc%%=yl%%>>32
 2300 yh%%=(ch%%+dh%%+yc%%) AND &FFFFFFFF
 2310 =yh%%<<32 OR (yl%% AND &FFFFFFFF)
 2400 DEF FNipoly8%%(x%%)
 2410 LOCAL x2%%,r%%
 2420 x2%%=FNmul64%%(x%%,x%%)
 2430 r%%=FNadd64%%(FNmul64%%(x2%%,x2%%),&4cca10b00bc5da27)
 2440 r%%=FNadd64%%(FNmul64%%(r%%,x2%%),&872a4954ca3a3537)
 2450 r%%=FNadd64%%(FNmul64%%(r%%,x%%),&2bf0e3ddcc4fb44f)
 2460 r%%=FNadd64%%(FNmul64%%(r%%,x%%),&b5ad4eceda1ce2a9)
 2470 =r%%
produces the output

Code: Select all

>RUN
B5928CB17C6CA657
F6D946DE5802C393
45EBD61714B1FE7D
A226239E74DA2E55
3DD5A938556A29AB
7E397129ADE49EAF
FB8216389069FEE1
80D1C3AC29CA42E1
C3C354CC1A1E89F
CEC6B763BA862BAB
2C6826BB924514E5
BC08F09FE23E9C4D
478312DD5FDCB533
CBA21BC1DD30BA87
78232A1C49BD4E89
AFB4ED3CB3745E99
7F7A4F447F19967
497D219555FA4343
6FC8C3F34F4BF1CD
A94F7D62CAC455C5
Julia confirms this is correct.

Code: Select all

julia> function ipoly8(p)
           x=UInt64(p)
           x2=x*x
           return (((x2*x2+
               0x4cca10b00bc5da27)*x2+
               0x872a4954ca3a3537)*x+
               0x2bf0e3ddcc4fb44f)*x+
               0xb5ad4eceda1ce2a9
       end
ipoly8 (generic function with 1 method)

julia> using Printf

julia> for i=1:20
           @printf("%x\n",ipoly8(i))
       end
b5928cb17c6ca657
f6d946de5802c393
45ebd61714b1fe7d
a226239e74da2e55
3dd5a938556a29ab
7e397129ade49eaf
fb8216389069fee1
80d1c3ac29ca42e1
c3c354cc1a1e89f
cec6b763ba862bab
2c6826bb924514e5
bc08f09fe23e9c4d
478312dd5fdcb533
cba21bc1dd30ba87
78232a1c49bd4e89
afb4ed3cb3745e99
7f7a4f447f19967
497d219555fa4343
6fc8c3f34f4bf1cd
a94f7d62cac455c5
I did some of my debugging using the Linux Console version of BBC Basic and obtained a segment fault from something like

Code: Select all

>PRINT STR$!(q%%)
at one point. Note the exclamation point was meant to be a tilde.

While I was able to reproduce the typo, I was unfortunately not able to reproduce the crash. No errors occurred with the Pico.

User avatar
RichardRussell
Posts: 1112
Joined: Thu Jun 21, 2012 10:48 am

Re: BBC BASIC on the Raspberry Pi Pico?

Tue Sep 28, 2021 8:33 am

ejolson wrote:
Tue Sep 28, 2021 2:39 am
I'm probably missing something obvious, but is there a way to perform integer arithmetic modulo 2^64 in BBC Basic?
Not in a straightforward way. BBC BASIC automatically promotes a number to a float if it becomes too large to fit in an integer, rather than throwing away the high bit(s). This is essential to avoid 'silent' overflows in the middle of an expression, which could result in a completely incorrect answer without any errors or warnings being reported.

The best way for you to handle this depends on the likelihood of an overflow happening in your multiplication. If it's relatively unlikely, then the most efficient solution is probably to perform a regular 64-bit integer multiplication with errors trapped, and handle the overflow cases as and when they arise. This uses BBC BASIC's equivalent of a TRY...CATCH clause:

Code: Select all

      ON ERROR LOCAL IF FALSE THEN
        a%% = b%% * c%%
      ELSE
        REM Handle the overflow here
      ENDIF : RESTORE ERROR
  
But if overflows happen all or most of the time you will have no choice but to break down the calculation into terms that cannot exceed the 64-bit range. Sorry.

User avatar
jahboater
Posts: 7551
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: BBC BASIC on the Raspberry Pi Pico?

Tue Sep 28, 2021 11:17 am

RichardRussell wrote:
Tue Sep 28, 2021 8:33 am
ejolson wrote:
Tue Sep 28, 2021 2:39 am
I'm probably missing something obvious, but is there a way to perform integer arithmetic modulo 2^64 in BBC Basic?
Not in a straightforward way. BBC BASIC automatically promotes a number to a float if it becomes too large to fit in an integer, rather than throwing away the high bit(s). This is essential to avoid 'silent' overflows in the middle of an expression, which could result in a completely incorrect answer without any errors or warnings being reported.
Sounds a reasonable approach to me.

For Thumb2, the compiler emits this sequence:

Code: Select all

  53                    @ try.c:12:   int64_t c = a * b;
  54 0022 5543                  muls    r5, r2, r5      
  55 0024 04FB0653              mla     r3, r4, r6, r5 
  56 0028 A2FB0424              umull   r2, r4, r2, r4  @ c, c, b, a
  58 002c 2344                  add     r3, r3, r4      @, c
So perhaps it will be possible when the assembler is done.

User avatar
RichardRussell
Posts: 1112
Joined: Thu Jun 21, 2012 10:48 am

Re: BBC BASIC on the Raspberry Pi Pico?

Tue Sep 28, 2021 11:26 am

jahboater wrote:
Tue Sep 28, 2021 11:17 am
So perhaps it will be possible when the assembler is done.
Yes, and I am still working on it... slowly. Unfortunately I'm currently occupied with Colour Recovery work with a broadcast deadline, so everything else has had to be put on the back burner.

pidd
Posts: 2798
Joined: Fri May 29, 2020 8:29 pm
Location: Wirral, UK
Contact: Website

Re: BBC BASIC on the Raspberry Pi Pico?

Tue Sep 28, 2021 11:36 am

ejolson wrote:
Tue Sep 28, 2021 6:15 am
pidd wrote:
Tue Sep 28, 2021 5:46 am
For 64 bit multiply you might have to split into four 32 bit numbers, do three multiplications, shift left32 two of the numbers then add the other.

Unfortunately BBC basic error trapping doesn't let you resume on error which in some languages ignores the overflow.
It seems I need to split the numbers into six 24-bit numbers as multiplication of 0xFFFFFFFF by 0xFFFFFFFF, for example, results in a 64-bit number which is too big.

If everything is 64-bit unsigned, two 32-bit integers multiplied together shouldn't overflow.

0xFFFFFFFF X 0xFFFFFFFF = 0xFFFFFFFE00000001

User avatar
RichardRussell
Posts: 1112
Joined: Thu Jun 21, 2012 10:48 am

Re: BBC BASIC on the Raspberry Pi Pico?

Tue Sep 28, 2021 1:05 pm

pidd wrote:
Tue Sep 28, 2021 11:36 am
If everything is 64-bit unsigned, two 32-bit integers multiplied together shouldn't overflow.
BBC BASIC doesn't have unsigned integers (well, it has unsigned 8-bit integers but nothing else). So the biggest positive 64-bit integer is &7FFFFFFFFFFFFFFF and the largest integer you can square whilst remaining in range is &B504F333:

Code: Select all

      *hex 64
      a%% = &B504F333
      b%% = a%% * a%%
      PRINT ~b%%
which prints 7FFFFFFE9EA1DC29

Return to “General”