Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 03:00 22 Apr 2026 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 : PicoMite BASIC Structure UNION?

     Page 1 of 2    
Author Message
Mark
Regular Member

Joined: 26/11/2022
Location: United States
Posts: 99
Posted: 08:00pm 29 Mar 2026
Copy link to clipboard 
Print this post

Is it possible to have two (or more) variables with different STRUCT definitions refer to the same area in memory?

I'd like to use one layout for disk storage and another for in memory processing. The issue is that they are not the same size. I need to have a few extra bytes at the end for temporary processing that are not needed on disk.  I know I could just save the extra bytes to disk as there is plenty of room on the SD card, but I thought I'd ask.

Thanks,
Mark
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 11215
Posted: 08:19pm 29 Mar 2026
Copy link to clipboard 
Print this post

No: If you want to program in C then it is avilable but this is Basic and structures are already way beyond what is normally available
 
bfwolf
Senior Member

Joined: 03/01/2025
Location: Germany
Posts: 235
Posted: 09:48pm 29 Mar 2026
Copy link to clipboard 
Print this post

If I understand your problem correctly, the following might be a solution for your use case:

A "container struct" in RAM that has the on-file struct as a member, followed by the members you need for management in RAM.
 
Mark
Regular Member

Joined: 26/11/2022
Location: United States
Posts: 99
Posted: 11:55pm 29 Mar 2026
Copy link to clipboard 
Print this post

matherp,

That makes sense. I am so used to STRUCT's in other languages, I take them for granted, not realizing how limited BASIC was.

bfwolf,

I thought about that, but it appears that STRUCT SAVE only works with entire STRUCT, not fields.

Mark
 
bfwolf
Senior Member

Joined: 03/01/2025
Location: Germany
Posts: 235
Posted: 04:20pm 31 Mar 2026
Copy link to clipboard 
Print this post

  Mark said  matherp,

That makes sense. I am so used to STRUCT's in other languages, I take them for granted, not realizing how limited BASIC was.

bfwolf,

I thought about that, but it appears that STRUCT SAVE only works with entire STRUCT, not fields.

Mark


Hello @Peter, @Mark
Once again, I experimented:

Type TPerson
 FirstName As string * 15
 LastName As string * 15
End Type

Type TPersonContainer
 Person As TPerson
 NodeId As integer
End Type

Dim PersonList(10) As TPersonContainer
Dim P1 As TPerson

PersonList(1).Person.FirstName = "Peter"
PersonList(1).Person.LastName = "Marther"
PersonList(1).NodeId = 1

'The following 2 statements give errors (if enabled):
'Struct Print PersonList(1).Person 'Err: Cannot print a structure member...
'printPerson(PersonList(1).Person) 'Err: Structure type mismatch...

'Works, my be used as workaround:
P1 = PersonList(1).Person
printPerson(P1)

'Works:
Print PersonList(1).NodeId

Sub printPerson(p As TPerson)
 Struct Print p
End Sub


> run
TPERSON:
 .FIRSTNAME = "Peter"
 .LASTNAME = "Marther"
1
>


I think instead of "Struct Print" it could just as easily be "Struct Save" or "Struct Load"?

@Peter: So the two statements reported as errors could be bugs? Perhaps one could also live with the fact that it's not allowed to pass a struct, that is part of another struct, as a "member" parameter to subs and functions.

Then one would have to take the "detour" via an intermediate variable, which is of course less efficient, but works as a "workaround".

If only we had pointer variables (or reference variables) in MMBasic...
But yes, I know, there's practically no BASIC that supports pointer variables (or reference variables). Not even VB.NET offers that. OK - BASIC is not C..

Regards, bfwolf

Not to forget:
> option list
PicoMite MMBasic RP2350B V6.02.01
OPTION FLASH SIZE 16777216
OPTION COLOURCODE ON
OPTION CONTINUATION LINES ON
OPTION PICO OFF
OPTION CPUSPEED (KHz) 200000
OPTION PSRAM PIN GP47
>
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 11215
Posted: 04:23pm 31 Mar 2026
Copy link to clipboard 
Print this post

Not a bug but a limitation. This is why it gives an error as the code can't handle that level of complexity
 
bfwolf
Senior Member

Joined: 03/01/2025
Location: Germany
Posts: 235
Posted: 04:27pm 31 Mar 2026
Copy link to clipboard 
Print this post

  matherp said  Not a bug but a limitation. This is why it gives an error as the code can't handle that level of complexity


That's what I suspected. And one "can live with it" since it's possible via a "detour" (as shown above).
 
PhenixRising
Guru

Joined: 07/11/2023
Location: United Kingdom
Posts: 1849
Posted: 10:18pm 31 Mar 2026
Copy link to clipboard 
Print this post

What is the argument for structures?

Does a micro CONTROLLER need this?
Does it mean greater efficiency?

Asking for a friend  
 
EDNEDN
Senior Member

Joined: 18/02/2023
Location: United States
Posts: 288
Posted: 10:32pm 31 Mar 2026
Copy link to clipboard 
Print this post

  PhenixRising said  What is the argument for structures?


The ability to write cleaner, more easily understood code.
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3742
Posted: 10:58pm 31 Mar 2026
Copy link to clipboard 
Print this post

  PhenixRising said  What is the argument for structures?

Does a micro CONTROLLER need this?


Well, you can write a shell sort, or some variety of sort or use the MMBasic SORT command if it suits your needs.

Or you can have all your data (maybe a large amount) in an array of structures, and say
STRUCT SORT bigarray().city

Then you can do: index = Struct(FIND bigarray().city, "St. Louis")

If you don't have lots of data, it may not be all that useful. If you do, structures are quite valuable.

But could you do it on a PC rather than a microcontroller? Probably.
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on FOTS
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 3156
Posted: 11:33pm 31 Mar 2026
Copy link to clipboard 
Print this post

  Quote  Does a micro CONTROLLER need this?
My limited experience is large amounts of data may be generated by a controller that logs its inputs and outputs but analysis is not done onboard.
The data will typically be in CSV files or similar that you periodically download then put the controller back to work. Minimum downtime.

If the data is not just being archived for quality control tracking, analysis is done in a PC spreadsheet or database program.
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 8769
Posted: 07:13am 01 Apr 2026
Copy link to clipboard 
Print this post

STRUCT was designed for writing adventure games, I think. Much less boring than ordinary database stuff.  ;)  As such, having it on a baby computer-controller makes some sense.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
PhenixRising
Guru

Joined: 07/11/2023
Location: United Kingdom
Posts: 1849
Posted: 07:27am 01 Apr 2026
Copy link to clipboard 
Print this post

I am still keeping var-names to < five characters, believing that this is the most efficient way for Basic to identify them. Am I out of date?
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 11215
Posted: 08:33am 01 Apr 2026
Copy link to clipboard 
Print this post

  Quote  I am still keeping var-names to < five characters, believing that this is the most efficient way for Basic to identify them. Am I out of date?

Yes: variable names are hashed so the length is pretty much irrelevant
 
bfwolf
Senior Member

Joined: 03/01/2025
Location: Germany
Posts: 235
Posted: 12:32pm 01 Apr 2026
Copy link to clipboard 
Print this post

  PhenixRising said  What is the argument for structures?

Does a micro CONTROLLER need this?
Does it mean greater efficiency?

Asking for a friend  


There are basically two types of arguments for this:

1. It allows you to implement the rule "keep data together that belong together!".

Imagine a table like in a spreadsheet:

There are many rows of data records with different types of values ​​(strings, numbers) in the columns.

With a two-dimensional array, all fields in the table must have the same type. This would mean that even columns with numbers in the fields would have to be stored as strings! You would then have to convert them repeatedly for calculations and then back into strings. That wouldn't be very efficient!

and

2. It can also be more performant, since instead of multiple parameters, you only need to pass a reference to a struct to a sub/function.

Regards, bfwolf.
 
NPHighview

Senior Member

Joined: 02/09/2020
Location: United States
Posts: 218
Posted: 03:32pm 01 Apr 2026
Copy link to clipboard 
Print this post

As a developer of embedded software for medical devices, I can definitively say that STRUCTs are extraordinarily useful.

I used STRUCTs in C to develop the task scheduler for a cardiac ultrasound imager, avoiding the overhead of more general OOP constructs. An essential feature was the ability to embed pointers to the functions that handled the event triggered by a bitmapped mask to a bit array that indicated a change in the inputs collected from all of the UI/UX elements. There were twenty or thirty such events that could trigger actions, so the array of STRUCTs contained twenty or thirty elements.

The imager used a commercial RTOS on a Motorola 68000, probably at the upper end of what you might consider a microcontroller :-)
Live in the Future. It's Just Starting Now!
 
PhenixRising
Guru

Joined: 07/11/2023
Location: United Kingdom
Posts: 1849
Posted: 03:57pm 01 Apr 2026
Copy link to clipboard 
Print this post

Thanks guys, you just made me a huge fan of structures, especially now that I don't need to worry about var-name length  
 
bfwolf
Senior Member

Joined: 03/01/2025
Location: Germany
Posts: 235
Posted: 04:11pm 01 Apr 2026
Copy link to clipboard 
Print this post

  NPHighview said  The imager used a commercial RTOS on a Motorola 68000, probably at the upper end of what you might consider a microcontroller :-)


Calling the M68000 a "microcontroller" is an understatement!
It was designed for workstations and servers in its days. Unfortunately, in addition to ROM and RAM, it also required several external support and peripheral chips.
Only the later M68332 was truly a "microcontroller," and even later, of course, the ColdFire microcontrollers/processors, which omitted the slow CISC 68k instructions (which require multiple clock cycles and couldn't be executed using a hardware multiplier, for example). However, these omitted instructions could be emulated via a library using an illegal instruction trap, in case older 680xx software needed to run. The ColdFire compilers simply didn't generate the missing instructions.

The M68000 was certainly the father of the ARM architecture and, in my opinion, is still one of the most beautiful CPUs from a programmer's perspective. However, ARM microcontrollers like the Pi Pico are now orders of magnitude faster.

Like the ARM, the M68000 supports structs particularly well through its indirect addressing modes such as offset16(An).
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3742
Posted: 05:18pm 01 Apr 2026
Copy link to clipboard 
Print this post

  bfwolf said  ... omitted the slow CISC 68k instructions (which require multiple clock cycles


I don't remember multi-clock instructions on the 68000. It was around 45 years ago, but I wire-wrapped a 68000 single-stepper which, as I recall, depended on the one-instruction-per-cycle characteristic. Or maybe it was mostly one cycle per instruction, with some requiring multiple button-pushes. As I say, a long time ago.

A great chip, though, with a flat 1MB memory architecture. It was a disappointment to me that the IBM PC didn't use the 68000.
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on FOTS
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 8769
Posted: 07:13pm 01 Apr 2026
Copy link to clipboard 
Print this post

IMHO the 68000 was a microprocessor or, at a pinch, almost a microcomputer. :)

Nothing is a microcontroller unless it has IO pins that can be attached directly to external devices. Having to drive external IO chips over an external address/data bus doesn't count. A microcontroller *includes* a microprocessor and IO peripherals on-chip.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
     Page 1 of 2    
Print this page
The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2026