RAM Poke Problem

Discussion of issues related specifically to writing code for native mode devices. This includes ZBasic code as well as assembly language code and C code, both inline and standalone.
Post Reply
mikep
Posts: 796
Joined: 24 September 2005, 15:54 PM

RAM Poke Problem

Post by mikep »

Here is a test program:

Code: Select all

Private msgbuf(1 To 64) as Byte
Private bufEnd as UnsignedInteger

Public Sub Main()	
	bufEnd = msgbuf.DataAddress	
	Debug.Print CStrHex(bufEnd); " "; cstrhex(msgbuf.DataAddress)
	Call RAMPoke(&hD7, bufEnd)
	Call RAMPoke(&h11, bufEnd+1)
	Debug.Print CStrHex(bufEnd); " "; cstrhex(msgbuf.DataAddress); " "; CStrHex(RAMPeekWord(bufEnd))
	
	bufEnd = msgbuf.DataAddress	
	Debug.Print CStrHex(bufEnd); " "; cstrhex(msgbuf.DataAddress)	
	Call RAMPokeWord(&h11D7, bufEnd)
	Debug.Print CStrHex(bufEnd); " "; cstrhex(msgbuf.DataAddress); " "; CStrHex(RAMPeekWord(bufEnd))
End Sub
On a ZX24 the output is correct

Code: Select all

00a0 00a0
00a0 00a0 11d7
00a0 00a0
00a0 00a0 11d7
On a ZX-24n it appears to poke the value into the variable rather than the address where the variable is pointing to

Code: Select all

0102 0102
01d7 0102 1100
0102 0102
11d7 0102 1100
Mike Perks
mikep
Posts: 796
Joined: 24 September 2005, 15:54 PM

Re: RAM Poke Problem

Post by mikep »

mikep wrote:Call RAMPokeWord(&h11D7, bufEnd)
The generated code is

Code: Select all

*(uint16_t *)&zv_bufEnd = 4567;
rather than

Code: Select all

*(uint16_t *)zv_bufEnd = 4567;
Mike Perks
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: RAM Poke Problem

Post by dkinzer »

mikep wrote:On a ZX-24n it appears to poke the value into the variable rather than the address where the variable is pointing to
Thanks. It is interesting that the address/dereferencing code generated for RamPeek() is correct. Somehow we missed the problem for RamPoke(). A solution to the problem has been identified and tested preliminarily.

One way to work around the problem is to use a based variable:

Code: Select all

Dim msgWord as UnsignedInteger Based msgBuf.DataAddress
msgWord = &H11D7
If you need to populate several elements of msgBuf, you can use an auxiliary variable:

Code: Select all

Dim addr as UnsignedInteger
Dim msgWord as UnsignedInteger Based addr
addr = msgBuf.DataAddr
msgWord = &H11D7
A third option that may be useful if the message buffer lends itself to being described by a structure is to define a structure matching the message and then define a structure variable based on the address of the message buffer.
- Don Kinzer
mikep
Posts: 796
Joined: 24 September 2005, 15:54 PM

Re: RAM Poke Problem

Post by mikep »

dkinzer wrote:[It is interesting that the address/dereferencing code generated for RamPeek() is correct. Somehow we missed the problem for RamPoke(). A solution to the problem has been identified and tested preliminarily.
Just to point out that all 3 variations have the problem.
dkinzer wrote:One way to work around the problem is to use a based variable:

Code: Select all

Dim msgWord as UnsignedInteger Based msgBuf.DataAddress
msgWord = &H11D7
If you need to populate several elements of msgBuf, you can use an auxiliary variable:
As it turns out I already had the auxillary variable. Here is my code with the work around:

Code: Select all

#if Option.TargetCode="ZVM"
			Call RAMPoke(CByte(length), bufend)
			bufEnd = bufEnd + 1
			Call RAMPokeWord(sequenceNumber, bufEnd)
#else
			Dim msgWord as UnsignedInteger Based bufend
			Dim msgByte as Byte Based bufend
			msgByte = CByte(length)
			bufend = bufend + 1
			msgWord = sequenceNumber
#endif			
			bufEnd = bufEnd + 2
Mike Perks
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: RAM Poke Problem

Post by dkinzer »

mikep wrote:Just to point out that all 3 variations have the problem.
Yes, they use common code since the only difference is the type of the cast. Consequently, the correction fixed all three.
- Don Kinzer
Post Reply