PEEK/POKE Video buffer
#1
Hello everyone,
In the good old days we used PEEK and POKE to read and write on the screen address. See also the example below.

Code: (Select All)
SCREEN 0
WIDTH 80, 25
DEF SEG = &HB800
DO
  FOR a = 0 TO 4000 STEP 4
      x = INT(RND * 2) + 48
      y = ((INT(RND * 2)) * 8) + 2
      POKE a, x
      POKE a + 1, y
  NEXT
LOOP UNTIL INKEY$ <> ""
SYSTEM


If we use SCREEN _NEWIMAGE(80, 25, 0) it no longer works. See example below.
Any suggestions on how to use PEEK/POKE?

Code: (Select All)
SCREEN _NEWIMAGE(80, 25, 0)
DEF SEG = &HB800
DO
  FOR a = 0 TO 4000 STEP 4
      x = INT(RND * 2) + 48
      y = ((INT(RND * 2)) * 8) + 2
      POKE a, x
      POKE a + 1, y
  NEXT
LOOP UNTIL INKEY$ <> ""
SYSTEM
Reply
#2
PEEK/POKE emulate the old DOS/QBasic system so I'd expect it to work with SCREEN 0.

With the newer screen modes you probably need to investigate _MEM and _MEMIMAGE usage. I'm just starting to play with mem commands myself, but here's what I came up with. It seems to be doing what you're trying, plus it's safer, memory wise.


Code: (Select All)
SCREEN _NEWIMAGE(80, 25, 0)
'DEF SEG = &HB800
DIM m AS _MEM
m = _MEMIMAGE(0)
DO
    FOR a = 0 TO 3996 STEP 4
        x = INT(RND * 2) + 48
        y = ((INT(RND * 2)) * 8) + 2
        'POKE a, x
        _MEMPUT m, m.OFFSET + a, x AS INTEGER
        'POKE a + 1, y
        _MEMPUT m, m.OFFSET + a + 1, y AS INTEGER
    NEXT
LOOP UNTIL INKEY$ <> ""
_MEMFREE m
SYSTEM


Oops, changed that STEP back to 4 for the spacing.
DO: LOOP: DO: LOOP
sha_na_na_na_na_na_na_na_na_na:
Reply
#3
Swap over to _MEM, which is the modern version of PEEK/POKE.

It's been ages since I last used PEEK and POKE as your example, so I'm having trouble sorting out what it's supposed to do.

Our screen is 80 * 25 * 2bytes per character in size, so 4000 bytes.

FOR a = 0 TO 4000 STEP 4 <-- this would address every other pixel on the screen
x = INT(RND * 2) + 48 <-- x is either 49 or 50, which is ASC for either "0" or "1"
y = ((INT(RND * 2)) * 8) + 2 <-- y is either 2 or 10... light green and dark green in color?
POKE a, x <-- set the character to CHR$(x)
POKE a + 1, y <-- set the color to y
NEXT

All this does is place 0's and 1's at alternating points of the screen and change their color from light green to dark green? Yes?

I'm thinking this is more-or-less, what you're looking for with modern mem commands:

Code: (Select All)
Screen 0
Dim m As _MEM: m = _MemImage(0) 'modern equiv of DEF SEG.  Point our mem block at our screen image
Dim As _Unsigned _Byte x, y 'make our variable type unsigned bytes
Do
    For a = 0 To 3996 Step 4
        x = Int(Rnd * 2) + 48
        y = ((Int(Rnd * 2)) * 8) + 2
        _MemPut m, m.OFFSET + a, x
        _MemPut m, m.OFFSET + a + 1, y
    Next
Loop Until InKey$ <> ""
System

Edit: OldMoses beat me to it while I was typing up my answer. Wink
Reply
#4
Oh, that's right. Unsigned byte, not integer as I did in my example.
DO: LOOP: DO: LOOP
sha_na_na_na_na_na_na_na_na_na:
Reply
#5
The program with the green 1 and 0 was actually an example of PEEK/POKE.
But I see that the same thing can be achieved with _MEMPUT. So a _MEMGET does the opposite. So I can and will change this.
Can you still use PCOPY to copy screens? Or do you use _MEMCOPY? I think PCOPY is going faster?
Reply
#6
PCOPY still works, though there's also a _COPYIMAGE routine now which you could make use of. 99% of the old QBASIC commands work exactly as they used to, with the main exceptions being the ones which relied on OS specific memory addresses and such (such as several of the PEEK/POKE calls), and DEF Fn, which hasn't ever been added to the language yet.
Reply
#7
OK, I just did a test with PCOPY, it works. Even with screens of 80x25 or 80x50 characters. And yes, even with 80x53 characters.
Reply
#8
(08-25-2022, 11:50 AM)BDS107 Wrote: The program with the green 1 and 0 was actually an example of PEEK/POKE.
But I see that the same thing can be achieved with _MEMPUT. So a _MEMGET does the opposite. So I can and will change this.
Can you still use PCOPY to copy screens? Or do you use _MEMCOPY? I think PCOPY is going faster?

Yes, _MEMGET is pretty much like PEEK. It has a statement and a function form depending on the use of parenthesis.

_MEMGET m, m.offset, c%

or

c% = _MEMGET(m, m.offset, INTEGER)
DO: LOOP: DO: LOOP
sha_na_na_na_na_na_na_na_na_na:
Reply




Users browsing this thread: 3 Guest(s)