Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 23:58 26 Apr 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : 4 squares and basic - a complete unfunded write up

Author Message
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 3527
Posted: 09:03am 24 Jan 2020
Copy link to clipboard 
Print this post

Introduction

A year ago I adapted a tinybasic interpreter to run on an arduino uno.
The result is that (via a terminal) you can write very long (;)) basic programs of 1kbyte using 26 variables (16bit). And no editor....
But is can do digital and analog IO. I sometime use it if I need a quick sequencer for mechanical relays in a test setup. Few lines of code, and run a few weeks to test whatever product in environmental test chambers.

4 squares

To test this interpreter, I used a basic program that 'just' fit's in the 1k memory, and calculates the 4 squares problem. (you can google it).
The arduino (16MHz 8bit) interpreter solves the 3 problems in 1500ms, 1300ms and 58000ms.
The 4 squares program is written to run on the reduced instruction set of tinybasic, and contains some "tweaks" to make it fit in 1k of memory.

I decided to port the program with minimal changes to the MMBASIC MX170 (since I have one). Did not change the clock frequency, so it runs at 40MHz.
The MX170 runs similar speed, 1600ms, 1400ms, 63000ms.
I did nothing to tune (no option explicit, no 48MHz, not change to make the program more efficient.

Since the PC was ON, and MMBASIC for DOS was installed, I ran the same program on my 4 core I5 laptop. The amazing speed of the PC threw me off my socks: 47ms, 31ms, 516ms. That was blazing fast....

RaspberryPi....

Some months ago I tried to install MMBASIC on my raspberry pi zero (no W) on buster and gave up after many failed attempts (and... to be honest also because the project I wanted t use it for, the hunter tracker, worked already on the MX170 to my liking).

Thanks to PeterB, I received a working setup with a pi zero with stretch, and decided to give it a try with the 4 squares program. The beauty of MMBASIC is that no porting is required, simply load the same program from the MX170 and DOS test, and type run.
The speed was not overwhelming. This can be due to the fact that the pi probably calculates 64 bit floats with option explicit....
The pi zero solves it in 600ms, 500ms, 16000ms. So it is 3 to 4 times faster than the MX170, but 25 slower than the PC.

BBC Basic

In my previous attempts with the pi, after I gave up on rapbian lite buster, and copied RISCOS-Pico (only 3.8 Mbyte, of which 3.3Mbyte is the instruction PDF) on the sd card. This is the BBC basic V basic interpreter on a very thin OS.
It took me all evening to find sufficient information about BBC basic and the Pico build of RISCOS (special release for the 50'th birthday of basic).
It has a clumsy editor, but I did not need to change the MMBASIC program to much to make it run. (TIMER => TIME, and the THEN is back into the IF statement). The timer has a 10ms tick, not 1ms.

But the 4 squares program is FAST: 15ms, 10ms, 150ms (3x faster that the i5 PC, 40x faster than MMBASIC on the same Pi Zero hardware).

There will be very good reasons for this difference. BBC basic uses 32bit integers, where MMBasic probably uses 64bit floats. Linux may have lots more overhead than RISKOS. PIGPIO may eat into the MMBASIC performance. And BBC basic may tokenize, or even pre-compile during editing. All in all ... the speed is amazing.

Not so good:

The SD card was FAT32, but after I stored some programs on it I typed from BBC basic, my laptop could not read the SD card anymore ... so there may be a corrupted or altered filesystem on it now. But the Pi still boots BBC basic V from it.


End of the story:

I will continue to use MMBasic, simply because it has the support for LCD's and peripherals. That speeds up development. And I will use a PIC32 (or STM32) (a real microcontroller). Not a Pi.
PicomiteVGA PETSCII ROBOTS
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 8579
Posted: 09:46am 24 Jan 2020
Copy link to clipboard 
Print this post

Please post the MMBasic version of the code - useful as a benchmark

Thanks
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 3527
Posted: 11:50am 24 Jan 2020
Copy link to clipboard 
Print this post

Hi Peter,

This is the code I ran on the MMBasic platforms

It is a masterpiece of completely unreadable code. Let me explain a bit.

The 4 squares problem is about 4 area's 1,2,3,4

Area 1: contains A and B
Area 2: contains B,C,D (B overlaps 1 and 2)
Area 3: contains D,E,F (D overlaps 2 and 3)
Area 4: contains D,G (F overlaps 3 and 4)

A...G are numbers between Start(S) and Stop(P)

The sum (T) of numbers in 1,2,3 and 4 must be the same.


Execute this for
Start = 3, Stop = 9, all numbers must be unique, no duplicates (U=0)
Start = 1, Stop = 7, all numbers must be unique, no duplicates (U=0)
Start = 0, Stop = 9, all numbers can be used (I used U=11)

To make the code run on my TinyBasic version (memory wise) the unique-not_unique decission is made by addition in lines 11,14,15,17,18,19 and by simple comparison in lines 22,28,36.
In the challenge. the code should be single program, not running the same program 3x.
On internet there is a page describing how this problem can be solved in some 200 different programming languages, and example code.

Anyway.. this code works, and gives the required result.

Knowing you Peter, tomorrow there is a new post with a optimized and very readable version of the algorythm..... That is a compliment !

1 Print" 4 Squares in Basic ":Print
2 S=1:P=7:U=0:'1 to 7
3 GoSub 9
4 S=3:P=9:U=0:'3 to 9
5 GoSub 9
6 S=0:P=9:U=11:'1 to 9 not unique
7 GoSub 9
8 End
9 Timer=0:Print:I=0:For A=S To P
10 For B=S To P
11 If A+U=B GoTo 97
12 T=A+B
13 For C=S To P
14 If C+U=A GoTo 96
15 If C+U=B GoTo 96
16 For D=S To P
17 If D+U=A GoTo 95
18 If D+U=B GoTo 95
19 If D+U=C GoTo 95
20 If T<>(B+C+D) GoTo 95
21 For E=S To P
22 If U GoTo 27
23 If E=A GoTo 94
24 If E=B GoTo 94
25 If E=C GoTo 94
26 If E=D GoTo 94
27 For F=S To P
28 If U GoTo 34
29 If F=A GoTo 93
30 If F=B GoTo 93
31 If F=C GoTo 93
32 If F=D GoTo 93
33 If F=E GoTo 93
34 If T<>(D+E+F) GoTo 93
35 For G=S To P
36 If U GoTo 43
37 If G=A GoTo 92
38 If G=B GoTo 92
39 If G=C GoTo 92
40 If G=D GoTo 92
41 If G=E GoTo 92
42 If G=F GoTo 92
43 If T<>(F+G) GoTo 92
90 If U=0 Then Print A;" ";B;" ";C;" ";D;" ";E;" ";F;" ";G
91 I=I+1
92 Next G
93 Next F
94 Next E
95 Next D
96 Next C
97 Next B
98 Next A
99 Print: Print I;:If u=0 Then Print " UNIQUE";
100 Print" SOLUTIONS USING NUMBERS ";S;" TO ";P;" FOUND IN ";Timer;" ms"
101 Return


Edited 2020-01-24 21:55 by Volhout
PicomiteVGA PETSCII ROBOTS
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 8579
Posted: 02:13pm 24 Jan 2020
Copy link to clipboard 
Print this post

Your code with no changes running in MMBasic on a STM32H743 using 64-bit floats, 64-bit integers run the same speed

4 Squares in Basic


3  7  2  1  5  4  6
4  5  3  1  6  2  7
4  7  1  3  2  6  5
5  6  2  3  1  7  4
6  4  1  5  2  3  7
6  4  5  1  2  7  3
7  2  6  1  3  5  4
7  3  2  5  1  4  6

8 UNIQUE SOLUTIONS USING NUMBERS  1 TO  7 FOUND IN  237.986 ms

7  8  3  4  5  6  9
8  7  3  5  4  6  9
9  6  4  5  3  7  8
9  6  5  4  3  8  7

4 UNIQUE SOLUTIONS USING NUMBERS  3 TO  9 FOUND IN  163.109 ms


2860 SOLUTIONS USING NUMBERS  0 TO  9 FOUND IN  5356.747 ms
 
Tinine
Guru

Joined: 30/03/2016
Location: United Kingdom
Posts: 1646
Posted: 05:36pm 25 Jan 2020
Copy link to clipboard 
Print this post

  Volhout said  Hi Peter,

This is the code I ran on the MMBasic platforms

It is a masterpiece of completely unreadable code. Let me explain a bit.

The 4 squares problem is about 4 area's 1,2,3,4

Area 1: contains A and B
Area 2: contains B,C,D (B overlaps 1 and 2)
Area 3: contains D,E,F (D overlaps 2 and 3)
Area 4: contains D,G (F overlaps 3 and 4)

A...G are numbers between Start(S) and Stop(P)

The sum (T) of numbers in 1,2,3 and 4 must be the same.


Execute this for
Start = 3, Stop = 9, all numbers must be unique, no duplicates (U=0)
Start = 1, Stop = 7, all numbers must be unique, no duplicates (U=0)
Start = 0, Stop = 9, all numbers can be used (I used U=11)

To make the code run on my TinyBasic version (memory wise) the unique-not_unique decission is made by addition in lines 11,14,15,17,18,19 and by simple comparison in lines 22,28,36.
In the challenge. the code should be single program, not running the same program 3x.
On internet there is a page describing how this problem can be solved in some 200 different programming languages, and example code.

Anyway.. this code works, and gives the required result.

Knowing you Peter, tomorrow there is a new post with a optimized and very readable version of the algorythm..... That is a compliment !

1 Print" 4 Squares in Basic ":Print
2 S=1:P=7:U=0:'1 to 7
3 GoSub 9
4 S=3:P=9:U=0:'3 to 9
5 GoSub 9
6 S=0:P=9:U=11:'1 to 9 not unique
7 GoSub 9
8 End
9 Timer=0:Print:I=0:For A=S To P
10 For B=S To P
11 If A+U=B GoTo 97
12 T=A+B
13 For C=S To P
14 If C+U=A GoTo 96
15 If C+U=B GoTo 96
16 For D=S To P
17 If D+U=A GoTo 95
18 If D+U=B GoTo 95
19 If D+U=C GoTo 95
20 If T<>(B+C+D) GoTo 95
21 For E=S To P
22 If U GoTo 27
23 If E=A GoTo 94
24 If E=B GoTo 94
25 If E=C GoTo 94
26 If E=D GoTo 94
27 For F=S To P
28 If U GoTo 34
29 If F=A GoTo 93
30 If F=B GoTo 93
31 If F=C GoTo 93
32 If F=D GoTo 93
33 If F=E GoTo 93
34 If T<>(D+E+F) GoTo 93
35 For G=S To P
36 If U GoTo 43
37 If G=A GoTo 92
38 If G=B GoTo 92
39 If G=C GoTo 92
40 If G=D GoTo 92
41 If G=E GoTo 92
42 If G=F GoTo 92
43 If T<>(F+G) GoTo 92
90 If U=0 Then Print A;" ";B;" ";C;" ";D;" ";E;" ";F;" ";G
91 I=I+1
92 Next G
93 Next F
94 Next E
95 Next D
96 Next C
97 Next B
98 Next A
99 Print: Print I;:If u=0 Then Print " UNIQUE";
100 Print" SOLUTIONS USING NUMBERS ";S;" TO ";P;" FOUND IN ";Timer;" ms"
101 Return



If you need a serious speed boost on the MX170, you could always use Bypic.

@40 MHz it's still faster than the H7 and still interactive.
 
twofingers
Guru

Joined: 02/06/2014
Location: Germany
Posts: 1133
Posted: 12:22pm 26 Jan 2020
Copy link to clipboard 
Print this post

  Tinine said  If you need a serious speed boost on the MX170, you could always use Bypic.

... or you could use a CFunction (MMBasic).

Regards
Michael
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 3527
Posted: 01:43pm 26 Jan 2020
Copy link to clipboard 
Print this post

Hi Tinine,

First of all, I am not looking for speed, the thread was purely to communicate the performance of BBC basic on the Pi Zero

The ByPic is definitely a nice environment. But it is not maintained anymore I read.

You can still download the last "stable' release for the MX170.
I however have no idea if the development environment is usable (I run linux, does it work under wine ?).
It may be worth looking into. It also supports all kind of peripherals.
Since I typically develop using a laptop with geany(linux),there is not much different in using a compiling development environment.

Do you have experience? Do you use it ?

Regards,

Volhout
PicomiteVGA PETSCII ROBOTS
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 3527
Posted: 01:43pm 26 Jan 2020
Copy link to clipboard 
Print this post

Hi Tinine,

First of all, I am not looking for speed, the thread was purely to communicate the performance of BBC basic on the Pi Zero

The ByPic is definitely a nice environment. But it is not maintained anymore I read.

You can still download the last "stable' release for the MX170.
I however have no idea if the development environment is usable (I run linux, does it work under wine ?).
It may be worth looking into. It also supports all kind of peripherals.
Since I typically develop using a laptop with geany(linux),there is not much different in using a compiling development environment.

Do you have experience? Do you use it ?

Regards,

Volhout
PicomiteVGA PETSCII ROBOTS
 
Tinine
Guru

Joined: 30/03/2016
Location: United Kingdom
Posts: 1646
Posted: 04:52pm 26 Jan 2020
Copy link to clipboard 
Print this post

When I first discovered Bypic I was quite excited but it hadn't a ms timer which I totally depend on.

Then they implemented one "tick" but I'd moved on.

I did run the benchmark, out of interest, though


The standard IDE is PSPad, I believe which appears to be Windows only but they show how to hook-up a ESP8266 which allows you to use their web-ide (haven't tried it)

Of course, one can always edit on the device, using a terminal.
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024