Newbie questions: Programming tools and graphics

Discuss all aspects of programming here. From 8-bit through to modern architectures.
Post Reply
User avatar
robcfg
Posts: 30
Joined: Sun Dec 30, 2018 6:23 pm
Contact:

Newbie questions: Programming tools and graphics

Post by robcfg » Fri Jan 11, 2019 8:19 am

Hello everyone!

I'd like to make some programming for the Beeb/Electron and would like to know which tools do you guys use for that, how do I load machine code programs, and also how do I access individual pixels in Mode 1 as the memory organisation is quite peculiar.

Thanks in advance!

RobC
Posts: 2514
Joined: Sat Sep 01, 2007 9:41 pm
Contact:

Re: Newbie questions: Programming tools and graphics

Post by RobC » Fri Jan 11, 2019 10:45 am

For assembly language programming on modern hardware, I think most people tend to use BeebAsm as it'll generate an SSD file that you can load into an emulator.

You'll probably want to know where in memory you can put your code and data - this is a good and comprehensive guide but there's also some useful info in the Advanced User Guide (AUG). This book by Ian Birnbaum was also highly regarded BITD.

The AUG also shows the layout of screen memory in the various modes. For mode 1, each byte of screen memory maps to 4 pixels with bits 7 & 3 controlling the logical colour of the left-most pixel, bits 6 & 2 the next pixel etc. with bits 4 and 0 controlling the colour of the right-most pixel.

So, to only alter an individual pixel, you have to:
a) Read the appropriate byte of screen memory
b) Mask off the bits corresponding to the pixels you aren't changing (e.g. with an AND)
c) Write the bits corresponding to the pixel you are changing (e.g. with an OR)
d) Write the byte back to screen memory

There are lots of shortcuts if you don't care about what was on the screen previously or if you are setting multiple pixels but this is the general case.

Calculating the correct screen memory address is also a bit of a pain. There's a discussion and some example code here. You can make things a bit simpler by manipulating the CRTC registers to reduce the screen width - if that's something you want to do then I can dig out some example code.

User avatar
tricky
Posts: 3263
Joined: Tue Jun 21, 2011 8:25 am
Contact:

Re: Newbie questions: Programming tools and graphics

Post by tricky » Fri Jan 11, 2019 5:09 pm

beebasm has a sample program that I think prints dots on a rotating sphere, by plotting single dots in MODE 1, or a reshaped version of it.
You might also find some useful stuff on http://www.retrosoftware.co.uk which used to be the programming section of *.
Try the wiki or maybe search the forum.

User avatar
robcfg
Posts: 30
Joined: Sun Dec 30, 2018 6:23 pm
Contact:

Re: Newbie questions: Programming tools and graphics

Post by robcfg » Sat Jan 12, 2019 4:36 pm

Thanks for the nice tips!

Seems like I have a lot to read... :mrgreen:

Regarding the CRTC, if you set the screen to a Spectrum-like resolution (256x192), would the memory organization be the same as with a regular mode 1?

Thanks a bunch!

User avatar
tricky
Posts: 3263
Joined: Tue Jun 21, 2011 8:25 am
Contact:

Re: Newbie questions: Programming tools and graphics

Post by tricky » Sat Jan 12, 2019 5:37 pm

Yes, apart from it would be two pages per character row, making pixel X,Y to screen address:

Code: Select all

;; assumes screen starts at &4000, makes things easier (or instead of clc : adc)
addr = &70
.plot_pixel ;; X,Y in pixels
tya : lsr A : lsr A : lsr A : lsr A : ora #HI(scr DIV 2) : sta addr+1 ;; half addr offset
txa : and #&F4 : asl A : sta addr : rol addr+1 ;; shift top bit back into HI doubling it
tya : and #7 : tay ;; could or it into LO to not corrupt Y
txa : and #3 : tax : lda pixels,x : sta (addr),y ;; or maybe eor (addr),y : sta (addr),y
RTS
.pixels : EQUB &88, &44, &22, &11 ;; white
You can start the display anywhere (8 byte aligned) in the lower 32K, but &4000 makes some things a little easier or cheaper. Most of my games use MODE 1 256x256, even if some of it is "hidden" with 6845 trickery.
Last edited by tricky on Sat Jan 12, 2019 5:39 pm, edited 1 time in total.

VectorEyes
Posts: 203
Joined: Fri Apr 13, 2018 1:48 pm
Contact:

Re: Newbie questions: Programming tools and graphics

Post by VectorEyes » Sat Jan 12, 2019 10:45 pm

tricky wrote:
Fri Jan 11, 2019 5:09 pm
beebasm has a sample program that I think prints dots on a rotating sphere, by plotting single dots in MODE 1, or a reshaped version of it.
You might also find some useful stuff on http://www.retrosoftware.co.uk which used to be the programming section of *.
Try the wiki or maybe search the forum.
I could be wrong but I think that rotating sphere demo uses a 'square' Mode 2 (128x256).

(It's still a good place to start with learning Beeb coding though!)

User avatar
tricky
Posts: 3263
Joined: Tue Jun 21, 2011 8:25 am
Contact:

Re: Newbie questions: Programming tools and graphics

Post by tricky » Sun Jan 13, 2019 12:29 am

Your probably right, still same addressing 0, 1 or 2 until you choose the colour/pixel within the byte.
Also, I don't remember if it is "simple"!
Last edited by tricky on Sun Jan 13, 2019 12:29 am, edited 1 time in total.

User avatar
richardtoohey
Posts: 3700
Joined: Thu Dec 29, 2011 5:13 am
Location: Tauranga, New Zealand
Contact:

Re: Newbie questions: Programming tools and graphics

Post by richardtoohey » Mon Jan 21, 2019 8:58 am

Don't forget the fantastic resource in the old magazines like Beebug and The Micro User - lots of great articles in those.

User avatar
robcfg
Posts: 30
Joined: Sun Dec 30, 2018 6:23 pm
Contact:

Re: Newbie questions: Programming tools and graphics

Post by robcfg » Wed Jan 23, 2019 12:23 pm

Progress!

I did a small Basic program that sets mode 1 and then changes the crtc registers. I find Basic quite convenient for trying new stuff

Code: Select all

10 MODE 1
20 REM HORIZONTAL (256 PIXELS)
30 ?&FE00=1
40 ?&FE01=64
50 ?&FE00=2
60 ?&FE01=90
70 REM VERTICAL (192 PIXELS)
80 ?&FE00=6
90 ?&FE01=24
100 ?&FE00=4
110 ?&FE01=42
120 SM=?&034E*256
130 FOR I=0 TO 12287
140 ?(SM+I)=255
150 NEXT I
160 GOTO 160
Should I be changing the memory address as stated in this post.

Now, can I load a raw binary file straight to memory? Does it need any processing or just adding it to a disk/tape image?

User avatar
tricky
Posts: 3263
Joined: Tue Jun 21, 2011 8:25 am
Contact:

Re: Newbie questions: Programming tools and graphics

Post by tricky » Wed Jan 23, 2019 1:36 pm

You shouldn't be changing VerticalTotal otherwise it won't be 50Hz, but you will probably get away with it in some emulators as they don't tend to emulate a 50Hz CRT, but display what is output and assume that the display will cope.
To centre it, instead of:

Code: Select all

100 ?&FE00=4
110 ?&FE01=42
Try adjusting the position of VSYNC, this will move the image up and down.

Code: Select all

100 ?&FE00=7
110 ?&FE01=30
You just need a binary file on disc and *LOAD ... or OSCLI("LOAD ...") if it is not on the end of a line.
Change R12/R13 (big endian) to move the start of screen memory. This is usually set to move the screen to the end of RAM. So (&8000-24*512)/8.
If you want to hardware scroll, it will only wrap around from &7FFF to &3000, &4000, &5800 and &6000 depending on how it is set.

User avatar
jgharston
Posts: 3449
Joined: Thu Sep 24, 2009 11:22 am
Location: Whitby/Sheffield
Contact:

Re: Newbie questions: Programming tools and graphics

Post by jgharston » Wed Jan 23, 2019 10:05 pm

tricky wrote:
Wed Jan 23, 2019 1:36 pm
You just need a binary file on disc and *LOAD ... or OSCLI("LOAD ...") if it is not on the end of a line.
...or if you need to pass it calculated parameters, eg OSCLI "LOAD "+filename$+" "+STR$~address%

Code: Select all

$ bbcbasic
PDP11 BBC BASIC IV Version 0.25
(C) Copyright J.G.Harston 1989,2005-2015
>_

User avatar
robcfg
Posts: 30
Joined: Sun Dec 30, 2018 6:23 pm
Contact:

Re: Newbie questions: Programming tools and graphics

Post by robcfg » Sun Jan 27, 2019 8:06 pm

Stardate 190127-2055.

I managed to set the right parameters and create a small command line program that converts a raw image to the Beeb's screen format.

Now, I'm facing a singular problem...

If I poke values to the computed screen start address, everything works as expected but if I load images I create, I have to add 712 to the start address so my data ends up in the right place.

The program now is as follows:

Code: Select all

10 MODE 1
20 REM HORIZONTAL (256 PIXELS)
30 ?&FE00=1
40 ?&FE01=64
50 ?&FE00=2
60 ?&FE01=90
70 REM VERTICAL (192 PIXELS)
80 ?&FE00=6
90 ?&FE01=24
100 ?&FE00=7
110 ?&FE01=30
120 SM=?&034E*256
130 SM=SM+712
140 OSCLI "LOAD red.dat "+STR$(SM)
150 GOTO 160
This way, it loads the picture fine. But if I remove line 130, then it will look garbled. See attached screenshots.

The funny thing is if I poke SM-712 after loading the image, it will change the first pixel as expected.

Can anyone shed some light on this?
Attachments
ImageLoadBad.gif
ImageLoadBad.gif (5.19 KiB) Viewed 309 times
ImageLoadOk.gif
ImageLoadOk.gif (6.48 KiB) Viewed 309 times

cmorley
Posts: 797
Joined: Sat Jul 30, 2016 7:11 pm
Location: Oxford
Contact:

Re: Newbie questions: Programming tools and graphics

Post by cmorley » Sun Jan 27, 2019 8:13 pm

*LOAD expects a hex address so you need:

Code: Select all

140 OSCLI "LOAD red.dat "+STR$~(SM)
Your decimal number comes out at 13000... you need 3000 hex.

User avatar
robcfg
Posts: 30
Joined: Sun Dec 30, 2018 6:23 pm
Contact:

Re: Newbie questions: Programming tools and graphics

Post by robcfg » Sun Jan 27, 2019 9:22 pm

That was the problem, thank you very much!

I'm not used to BBC Basic, but doesn't it feel a bit weird that you have to specify a hex number without the & in front of it?

User avatar
tricky
Posts: 3263
Joined: Tue Jun 21, 2011 8:25 am
Contact:

Re: Newbie questions: Programming tools and graphics

Post by tricky » Sun Jan 27, 2019 10:26 pm

I occasionally forget and add an &.
The emulator debuggers also often take hex without the &.
BTW, did you find dreamland fantasy's Image2BBC

User avatar
robcfg
Posts: 30
Joined: Sun Dec 30, 2018 6:23 pm
Contact:

Re: Newbie questions: Programming tools and graphics

Post by robcfg » Mon Jan 28, 2019 6:52 am

Yes, I tried it.

Works fine on MacOS under wine but it uses only fixed resolutions for every screen mode, so couldn’t convert a 256x192 image to mode 2 because it scales or pads it to 320x256, which kind of defeats the point.

Also, I found quite interesting to do the conversion myself. Being a command line program allows also to use it in batch processes.

It’s plain C++ so you can compile it in any machine with a compiler.

User avatar
tricky
Posts: 3263
Joined: Tue Jun 21, 2011 8:25 am
Contact:

Re: Newbie questions: Programming tools and graphics

Post by tricky » Mon Jan 28, 2019 7:59 am

I used it ages ago, but pre padded/cropped the image.
For my games I also have a c++ (mostly c) command line program for converting sprites/backgrounds. I set mode, palette, images and conversion. I still add extra modes for each new game as I find different memory layouts and interleaving optimal for different uses.

Post Reply