A quick question about CHR$().
As a relatively new user of BBCSDL, I came across 'The Towers of Hanoi' from 'rosetta'...
Do not panic. The demonstration ran like clockwork without error.
One section had me a little puzzled.
DIM Disc$(13),Size%(3)
FOR disc% = 1 TO 13
Disc$(disc%) = STRING$(disc%," ")+STR$disc%+STRING$(disc%," ")
IF disc%>=10 Disc$(disc%) = MID$(Disc$(disc%),2)
Disc$(disc%) = CHR$17+CHR$(128+disc%-(disc%>7))+Disc$(disc%)+CHR$17+CHR$128
NEXT disc%
I am going to assume that CHR$(17) is some sort of 'control' function. But the rest of the string is puzzling.
According to this sites "Manual", in this case, the ASCII table... The table only ranges from CHR$(0) to CHR$(127).
Can someone explain the purpose of CHR(128+....) ?
J
CHR$()
Re: CHR$()
Ooh, good spot! I think it is encoding the colour of the disc. I suspect it's a sneaky way of sending VDU codes to reset the colour.
-
- Posts: 327
- Joined: Wed 04 Apr 2018, 06:36
Re: CHR$()
Actually it is to set the background colour. Foreground colour 0 is background colour 128, colour 1 is 129 and so on. VDU17 or CHR$(17) is identical to the keyword COLOUR, so CHR$17+CHR$128 is equivalent to COLOUR128, which does indeed set the background to black.
-
- Posts: 42
- Joined: Sat 28 May 2022, 22:40
Re: CHR$()
A byte is 8 bits; To help detect errors in data storage and transmission the standard ASCII set was defined to use 7 bits with the 8th bit used for parity; odd or even, so the receiving terminal would flag an error if the number of bits set to '1' in any byte didn't match the parity.johnno56 wrote: ↑Wed 14 Jul 2021, 03:28 A quick question about CHR$().
:
I am going to assume that CHR$(17) is some sort of 'control' function. But the rest of the string is puzzling.
According to this sites "Manual", in this case, the ASCII table... The table only ranges from CHR$(0) to CHR$(127).
Can someone explain the purpose of CHR(128+....) ?
J
eg, even parity 10101010 would be accepted, 00101010 would not.
So 01111111 was the largest number that could be stored, representing characters from 0-127, hence the limit to the standardised character set.
Foregoing the parity error detection (because storage medium, transmission methods were more robust) meant that bit 8 could now be used for data, doubling the available codes.
Codes 128-255 can be used for whatever purpose the device/software designers wish.
Of course in the BBC Micro the VDU channel allows for sequences of codes, ie. a control code followed by some data. Just because CHR$(255) is a user-defined graphic character, doesn't mean that's what it always represents, if it follows a control code then it is the value 255 that is significant, eg. following the code for 'plot', then the code for 'single pixel', it would have the meaning of '255 pixels across the plotting area'.
Started using BASIC circa 1981 with CP/M, Video Genie, Sinclair ZX81, Acorn Atom, and progressed with ZX Spectrum, BBC Micro and Sinclair QL, Cambridge Z88, DOS, Windows. Wrote A-level project using school's BBC Micro with dual 800K floppy drive.
Re: CHR$()
I would add that the ability to store 'VDU code sequences' in strings is a very powerful feature of BBC BASIC, but one which is often neglected. It allows you to put an entire graphics 'object' in a string, and so long as the embedded VDU commands only represent 'relative' plotting coordinates you can then plot that object multiple times in different places.
The Towers of Hanoi program uses it only in the most simplistic sense, to set the background colour, but the same technique is used in SDLIDE.bbc (one of the IDEs supplied with BBC BASIC for SDL 2.0) to draw the icons for files and folders in a file selector dialogue. Here's a demonstration which will run in either BBCSDL or BB4W:
Code: Select all
VDU 23,22,640;512;8,16,16,128
REM Create the 'icon' as a VDU stream in string variable BBfile$:
C% = (1.5 * @vdu%!220 + 7) AND -8
VDU 21
OSCLI "spool """ + @tmp$ + "bbfile.vdu"""
GCOL 11+128
PLOT 0,C%,0 : PLOT 99,C%*0.75,-C%*0.75 : PLOT 0,C%/4,C%/4 : PLOT 0,-C%*0.75,0
PLOT 115,-C%/2,-C%/2 : PLOT 0,C%/4,C%/4 : PLOT 98,C%*0.75,C%*0.75 : PLOT 0,0,-C%/2
PLOT 1,C%/4,0 : PLOT 1,-C%/2,-C%/2 : PLOT 1,-C%*0.75,0 : PLOT 1,C%/4,C%/4
PLOT 0,C%*1.25,C%*0.75
*spool
VDU 6
F% = OPENIN(@tmp$ + "bbfile.vdu")
BBfile$ = GET$#F% BY EXT#F%
CLOSE #F%
REM Now demonstrate plotting the 'icon':
VDU 5
REPEAT
MOVE RND(1000), RND(1000)
PRINT BBfile$ " Hello icon!"
WAIT 100
UNTIL FALSE