PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Finding the video pointer (i.e. debugging code)

I am trying to learn how to find the video buffer/pointer of the HPCpro devices.  I recieve a little tip from Larry, "The trade secret to my software it debugging my code to find the video
address."  How, exactly do you debug code?  I know that you can get an emulator, but I am pretty sure that there is none for the HPC2000 (which is what Larry has been using), so, I am guess that it has something to do with the device itself.  My guess it involves conecting the device to the comp, and then using evc's debugger to debug it, but the problem is, I was never able to get it to work with my 720.  If you guys could help me out with that, then I could write a tutorial to teach people how to get the video pointer, and then more people could get PQ on their devices.
Just because you have the address of video memory doesn't mean that's all you need to implement GAPI.  Some of those devices have restrictions on how you can access the memory (i.e. only on WORD boundaries, can only read/write WORDs, etc.).  That's why on some devices you'll end up getting a pointer to a internal system memory buffer instead and GAPI copies to video memory on GXEndDraw().

I suggest that if Larry already has a working, debugged library for these devices you're targetting that you just purchase it for the measly $50 sum he's asking and get on with your game programming.

If you can't get the debugger to connect to your device and debug software then you have a much more important problem at hand as a software developer.  What's the problem when you try to run your app under the debugger?
According to another thread, someone wanted a build for a device that even Larry's GAPI does not support. Larry offered to add the support if they are willing to send him their device so he can "analyze" it. Rob is thinking about giving it a shot himself.

Dan East
Rob you have to create registry key `Windows CE Tools' under HKEY_LOCAL_MACHINE on your Jornada and then create string `Platform' with value
`{0F9D255B-97DA-4641-A8E6-7A7411D2472F}'.

Close eVc, disconnect, reconnect and load eVc again.

Connect with the ppp transport (it works for me).

Also, there is no hpc2000 emulator. Microsoft recommends building a second machine and conencting the jornada to that as part of the emulation setup. Read the readme file that comes with the final hp2000 sdk for more descriptive answer.

Also, I am a little suprised that the german 720 has a different video memory address. What happens when they click the ok box on the gapi dialog box?. Lock-up, box re-appears etc? More info on this might help to narrow it down a bit.
I mean if it does reappear then you can be sure GXBeginDraw() is being called again.

Hope that helps.

John.
Thanks for all of your help.  I know have my device debugging.
BTW.
GXEndDraw does the following in asm.
mov       r0, #0
mov       pc, lr
So basicly all it does is return 0 (I wonder why not 1?)
GXBeginDraw does the following (in this order)
1. Loads values on the stack (probably to save them).
2. Finds what machine it is. GXSenseMachine
3. Finds video address
4. Moves some numbers around
5. Displays nag screen (seems like it would be more efficient to do this first).
6. Moves numbers around again
7. returns

Many other functions just return 0 :(

Now, here is something wierd.  The function that checks to see if your display ram is normal returns false, gee that's kind of wierd since this lib was writen for this display.

Oh well, thank god Larry didn't put anything in the license about disassembling his code ;)

I'll keep you posted on what I find out.
Rob, kev had the same problem when he ported mame to the 720, it would always crash on the gapi dialog...he traced it back to a function that checks to see if the display memory is ok, if not boom. Made sure it always returned a yes, and it worked. Then the gapi box would constantly reappear when you clicked ok.

I could load up the source and check what he done to this, but I am sure it involved called gxbegindraw() and copy this value into a temp pointer so any calls to gxbegindraw() were using this pointer instead, so no more dialog box. (Forgive me if this sounds like crap, as I am not a programmer, and probably made it sound daft), but If you like I can look at the source to give a detailed description of what he done.

Anyway, this info may or may not help you, hope so anyway.

John
I understand what you are saying (it took me 2 times, but I got it).  I know what you are saying about check the display memory, and I already made note of that.  The only call that I make to the GAPI that could fail is GXBeginDraw.  It is in this function that it decideds if it will return the buffer or not (after it checks the id).  It uses the id, to get the video buffer.  I know that the nag screen can't be a problem (I could remove it if I want, but that would violate the license with Larry, although it is tempting.  What happens to me if I do this?  Just kidding, I have 5 dollars towords the lib, but noone else has donated.)  I am still looking for where to find the video buffer.  I know that you arn't a programmer, but if you want to see how the HPC GAPI works (and what it seems like it should do but doesn't) then you can go into debug mode, and then look at it in asm.  The easiest way to do this is to include a call like gxbegin draw in a source file, and then set a break point on it, and then go into asm mode, and then hit the step command (I think that's what it is called), and you'll be in the asm source file.
Glad you understood what I said :)

Just a thought but have you access to the full version of the gapi lib. I mean if you could send it to someone with the full version just as a test to see if it helps compiling it without the gapi box showing up. Also, I am sure larry wouldnt mind you removing the gapi box as a test to see what is going on with the german version. After all it would benefit himself aswell.

Also, any german 720 users try out kevs mame port on my site?. It would be interesting to see if they suffer the same problem being that it also uses larrys gapi for hpc.

John
Also, is there any german 720 users that can compile and run larrys gapi demo that comes with the hpc gapi demo. If it runs ok, then they could compile a debug version and send you the value that gxbegindraw() returns for the address to the video mem.

Just a thought.
That's a great idea John.  Could a German 720 owner compile larry's gapi demo, and tell me if it works?
I thin that this is the address of the video buffer.  0x061b0000.  However, when I try to set pScreenBuf to that address and then run the program, I get a fatal application error.  The error occurs the first time that it try's to write to the buffer.  I got that address when running PQ and I got the same address when running PocketSNES.  This is how I set the buffer.
unsigned short *pScreenBuf = (unsigned short *)0x061b0000.  Does anyone have any idea's why?  If used regularly (i.e. how PQ usually sets it with GXBeginDraw).  My guess (and this is only a guess) is that maybe you have to set a value in order to get permission to write to the video buffer.  I'll continue looking.  If you have any idea's then please tell me.
Yeah, you may just have to call VirtualProtect on that pointer.....

Dan East
Success!!!  Well, almost.  Dan, I think taht you are right.  Just to test it out.  I set pScreenBuf to the video address, and then I made a call to GXBeginDraw (without using the return value), and I didn't get any kind of errors!!!  Hehehe.  Have any German 720 users been reading this thread.  If you want, I can send you this version, and we can see what happens.  Please post if you want the test version.  The only thing that I can think of right now is that, the call to GXDisplayProperties (or was that GXGetDisplayProperties) might have the same problem if my original hypothesis is correct.
Dan:
I took away the GXBeginDraw call, and used virtual protect.  Can you please check my syntxt?
//Declarations

unsigned short *pScreenBuf=(unsigned short *)0x061b0000;

DWORD dwOldProtect;

DWORD dwSize = 154240;//640 * 241

//VirtualProtect Function
VirtualProtect(pScreenBuf, dwSize, PAGE_READWRITE, &dwOldProtect);

Please tell me if I did this right.  If so, I'll debug Larry's code and see what other calls he made.  Thanks.

BTW this question is open to anyone.
Looks good to me. You may not even have to calculate and specify the size you want to change the protection of. Access is controlled by pages, not bytes, so you may can just specify a single byte as all the display memory should be on the same page.

Dan East
Rob,

Is VirtualProtect failing when you try to change the attributes of the suspected memory region without calling GXBeginDraw?

Have you checked the previous protection flags returned in lpflOldProtect after the API returns?  What about calling GXBeginDraw first, then calling VirtualProtect just to see what the previous settings were?  That way you might be able to determine if GXBeginDraw was changing the attributes.

Also, your calculation for the number of bytes to protect is wrong.

dwSize = 640 * 240 * 2; // 307200 bytes, 75 4K pages

I'll check to see what virtual protect is returning.  I was looking through the code to GAPI for the HPC (the disassembled version), and it doesn't call virtual protect.  It does call ISBADREADPTR(It's something like that) and the one for writing which returns weather or not acces is avalible.  I don't see anywhere that it changes permission though.  I'll give the idea a try.  Thanks.
I've tried it with and without calling GXBeginDraw, and it always returns 4 for the old setting (PAGE_READWRITE) which is what I wanted to set it to anyways, so that Isn't the problem.  If you have any other idea's please let me know.  Thanks.
You are calling GXOpenDisplay in all your tests, right?  I ask this because GAPI will need to convert the physical display memory address to a virtual address that can be used by your process.  This is normally done with a call to VirtualCopy.  It shouldn't be doing this every frame though (i.e. in GXBeginDraw) because that would be horribly inefficient (so would displaying a splash dialog, but that doesn't stop some).


Look at step #2 in this KB Article for what I'm talking about.  Ignore the part about config.bib, your display memory buffer is already allocated by the OS at boot time.

Again, this isn't something that would need to happen every time the app calls GXBeginDraw.  This could happen in DllMain in response to DLL_PROCESS_ATTACH for gx.dll, or I suppose when the app called GXOpenDisplay.

Actually, GXOpenDisply does nothing in the HPC version.  Also, you only need to call GXBeginDraw once.  But, you may be on to something.  I think what I need to do is use virtual copy like you said to change it from a physical address to a virtual one.  I'll look into that right now and then see what happens.  Just remember, GAPI for the HPC is not nearly as good as the real thing.  The following functions only return 0 (and nothing else).

GXOpenDisplay
GXOpenInput
GXSuspend
DXResume
GXCloseDisplay
GXEndDraw
GX (you know, the one that tells you if the video hardware is "normal."  I think that it is quite funny that it returns 0 when it was designed for this hardware.)

And a couple others that I cannot think of.  I believe that the only functions that actually do something are these.

GXBeginDraw
GXGetDisplayProperties (or is it just GXDisplayProperties)
and
GXGetDefaultKeys (see question above)

The one thing that I really feel it is lacking is the ability to get unfiltered key messages.  If it had that, I could define the hotkeys at the to as F keys (f1..fx)
If the function you're referring to is GXIsDisplayDRAMBuffer(), it probably should return 0, meaning GXBeginDraw will return a pointer to actual display memory instead of a pointer to an intermediate buffer in DRAM.

Let us know how you make out.

I looked for a refrence for VirtualCopy in all of the include files, and I couldn't find anything.  What about virtualAlloc.  Do I actually have to allocate the memory at this address myself, or what?  

The thing about GXIsDisplayDRAMBuffer() is that the code for it is to simply return 0.  That's it.

int (I think) GXIsDisplayDRAMBuffer(){
return 0;
}

Keep in mind, that Larry's GAPI is not MS GAPI.  I wish it was, but it just isn't.
VirtualCopy is declared in pkfuncs.h which is included with Platform Builder.  You might get away with asking Dan East for the header file and/or docs as he has recently obtained the CE source code.

The physical address of the display memory will need to be mapped to a virtual address in order for your process to dereference it properly.  You'll need to use VirtualAlloc to reserve the address space (don't allocate - just reserve), and then VirtualCopy to bind the physical memory to the virtual memory.  I thought there was an example of this in the KB article I provided.  Did you look at it?


You wrote:
GX (you know, the one that tells you if the video hardware is "normal."  I think that it is quite funny that it returns 0 when it was designed for this hardware.)

GXIsDisplayDRAMBuffer is implemented correctly.  What's so funny about it?  Larry obviously knows he's not using an intermediate DRAM buffer, so the function is returning 0, indicating such.  Did I miss something?

VirtualCopy is defined as DoVirtualCopy in kernel.h:
[fixed]
#undef VirtualCopy
#define VirtualCopy DoVirtualCopy
[/fixed]
The definition of DoVirtualCopy is found in KWIN32.H and is:
[fixed]
BOOL DoVirtualCopy(LPVOID lpvDest, LPVOID lpvSrc, DWORD cbSize, DWORD fdwProtect);[/fixed]

An example usage out of the KMISC.C file:
[fixed]LPBYTE lpNLSView = VirtualAlloc(0, cbSize, MEM_RESERVE, PAGE_NOACCESS);
VirtualCopy(lpNLSView, pvPhysical, cbSize, PAGE_READONLY);
[/fixed]
Where pvPhysical is a pointer to the physical memory you want a copy of.

Also, you may want to see MS knowledge base article Q213359. It refers to problems when using VirtualCopy to copy physical memory. It says it applies to "Microsoft Windows CE for the Handheld PC", so I don't know if it is pertinent CE versions greater than 2.0 or not.
The body of the DoVirtualCopy function is 180 lines long, so I'm not going to include it here.

Dan East
So, what your saying is that I need to download the windows ce source code.  Or could someone email me the related files?
It's my guess that from what Dan posted, DoVirtualCopy is exported by coredll.dll.  All you need is the function prototype to keep the compiler happy (Dan has posted that).

Thanks a lot for all of your help so far guys.  I'll work on this somemore tonight, and tell you what happens probably tomorrow after noon.  Thanks again.

Oh, one quick question, should I use lpNLSView, or pScreenBuf after I virtualalloc the me, assuming I allocate it to lpNLSView.  I am pretty sure I need to use lpNLSView, but I just wanted to check.  I would try it myself, but my computer with eVc on it, is not with me right now.
Moose or Chuck · 13 Aug 2001, 18:15 UTC
yup