PDA

View Full Version : is there any topic about how programming GBA


gfdooler
January 1st, 2012, 18:25
hello,

i want to know how to make a an arm7 emulator but i didn't find any thread talk or explain how can i proramming CPU , because the opcode is difficult
is there who explain to me how instruction work?

gfdooler
January 1st, 2012, 22:11
I found this explanation but it still not completed maybe there is who explain it better than that explanation :???:

Decoding the ARM instruction set (http://emucode.blogspot.com/2010_09_01_archive.html)

Exophase
January 3rd, 2012, 02:09
http://nocash.emubase.de/gbatek.htm

Or, if you want to program for the GBA, like the thread title seems to suggest:

http://forum.gbadev.org/

gfdooler
January 3rd, 2012, 18:09
thank you Exophase for your help

the first link doesn't work

and in the second i didnt what i want ,i want to make an emulator for GBA
and i want to decoding the ARM instructions ?

Exophase
January 3rd, 2012, 18:33
Well I guess he took it down. Here's a relink http://dsemu.oopsilon.com/gbatek.htm But you should try using google yourself..

I know you want to emulate GBA, but your thread title is definitely not worded well.

There is a CPU test ROM called armwrestler which has builds for GBA and DS. You need mode 0 graphics support to run it on GBA. I have it uploaded here:

http://exophase.devzero.co.uk/armwrestler.zip

Focus on getting that to work.

gfdooler
January 3rd, 2012, 22:55
i didn't find any informatons explain how decoding ARM7TDI instructions

for example in chip8 we fetch OP like this


switch (opcode & 0xFF)
{
case 0x0F:
switch(opcode & 0xF)
{
case 0xE: break; // do something
...
}
break;


but how can i fetch the arm instructon

for example let's take the branch BNE ,its opcode is 1AFFFFF9
1 = Condition: not equal
A = Branch (Link = 0 if =1 the curent address it will save in R14)
offset = FFFFF9

Exophase
January 3rd, 2012, 23:20
The information for how ARM instructions are encoded is all there, you just aren't looking hard enough. It's here (ARM):

http://dsemu.oopsilon.com/gbatek.htm#arminstructionsummary

And here (Thumb):

http://dsemu.oopsilon.com/gbatek.htm#thumbinstructionsummary

If you want to know how to fetch opcodes that's another question entirely, but your example doesn't show fetching.

gfdooler
January 4th, 2012, 22:14
I am sorry to ask you so many questions i read many documents until i give myself a headache :D

for example let's take the CMP instraction

cmp r3,#0

in HEX: e3530000

in binary:

1110 00 1 1010 1 0010 0000 000000000000
opcode s


instruction = 1110 0011 010 100100000000000000000


opcode = instruction>>21
switch (opcode & 0x00F)
{
case 0x0: //AND

{
Rd=Op1 AND O2;
break;
}
case 0x1: //EOR
{
Rd=Op1 EOR O2;
break;
}
case 0x2: //sub
{
Rd=Op1 - O2;
break;
}
case 0x3: //RSB
{
Rd=Op2 RSB Op1;
break;
}
case 0x4: //ADD
{
Rd=Op1 + O2;
break;
}
.
.
.
.


Is this the method to fetch witch instruction to execute or there is another better way to do it ?

i know its hard to emulate the ARM7 processor but i want to program it and i need your help

Prads
January 5th, 2012, 07:43
Is this the method to fetch witch instruction to execute or there is another better way to do it ?

Yeah that's one way to fetch and execute instruction, it's called interpreter. A better way to do it would be dynarec, which is much more complex. ;)

And no offense but I think moving from chip8 emulator to GBA emulator is a HUGE step. GBA is much more complex. Why not do a simple but challenging emu project like GBC or NES first before moving to GBA? Just my advice. :)

cottonvibes
January 5th, 2012, 18:29
I also am of the opinion that he should not be doing a GBA emulator at this time, if he is having difficulties with fetching/decoding the opcodes.
Although I do admit that arm instruction decoding is pretty hard compared to other instruction sets, since the instruction encodings have overlapping bit-fields, and try to make use of every available 2^32 encoding that the 32bits allow (if an instruction doesn't make sense (or effectively becomes a NOP), like a cmp without S-bit (which means a CMP that doesn't set flags), then arm will instead reserve/use that encoding space for other instructions).

If you do insist on doing a gba emulator, you might want to take a look at how other emulators do their instruction decoding.
From my quick reviewing of the arm instruction encoding, it looks like you'd first want to divide the instructions with something like:
u32 op = fetch32();
switch ((op>>25) & 7) {
case 0:
switch ((op>>4) & 9) {
case 0: // Data Processing Immediate Shift or Misc Instruction
case 8: // Data Processing Immediate Shift or Misc Instruction
if (((op>>20) & 0x19) == 0x10) { // Extended / Misc Instruction
}
else { // Data Processing Immediate Shift
}
break;
case 1: // Data Processing Register Shift or Misc Instruction
if (((op>>20) & 0x19) == 0x10) { // Extended / Misc Instruction
}
else { // Data Processing Register Shift
}
break;
case 9: // Multiply or Extra Load/Store
break;
}
break;
case 1: // Data Processing with Immediate
break;
case 2: ...
case 7:
break;
}

See page A3-2 for more details:
http://www.scss.tcd.ie/~waldroj/3d1/arm_arm.pdf

I'm not going to spend the time to decode the full instruction set for you, if you still don't get it then like already mentioned, you should tackle an easier emulation project instead.
Chip-8 doesn't compare to the effort a real emulator takes. I did a Chip-8 emu in a weekend, and have spent years working on my NES emulator (although to get the emulator to the state where it actually runs something took like a month).

From the looks of it, I'd say you're at the point where you should write a Chip-16 emulator:
http://forums.ngemu.com/showthread.php?t=145620

gfdooler
January 5th, 2012, 21:44
A better way to do it would be dynarec, which is much more complex. ;)


thank you Prads for your advice :thumb:
do you mean the chip8 emulator it's just an intrepter please advice me and explain to me the better way 'dynarec'



If you do insist on doing a gba emulator, you might want to take a look at how other emulators do their instruction decoding.
From my quick reviewing of the arm instruction encoding, it looks like you'd first want to divide the instructions with something like:
u32 op = fetch32();
switch ((op>>25) & 7) {
case 0:
switch ((op>>4) & 9) {
case 0: // Data Processing Immediate Shift or Misc Instruction
case 8: // Data Processing Immediate Shift or Misc Instruction
if (((op>>20) & 0x19) == 0x10) { // Extended / Misc Instruction
}
else { // Data Processing Immediate Shift
}
break;
case 1: // Data Processing Register Shift or Misc Instruction
if (((op>>20) & 0x19) == 0x10) { // Extended / Misc Instruction
}
else { // Data Processing Register Shift
}
break;
case 9: // Multiply or Extra Load/Store
break;
}
break;
case 1: // Data Processing with Immediate
break;
case 2: ...
case 7:
break;
}



thank you very much cottonvibes for your help

oK i will take a look at how other emulators do their instruction decoding.

but i want to understand how decoding the instruction of ARM7.

Prads
January 6th, 2012, 02:47
thank you Prads for your advice :thumb:
do you mean the chip8 emulator it's just an intrepter please advice me and explain to me the better way 'dynarec'

What I meant was the code your wrote for fetching and executing instruction is an interpreter. And chip8 was also an interpreted programming language but that is a different topic...

If you want to learn dynamic recompilation, here are couple of articles (mind you I am also a beginner in dynarec so I won't be able to answer all your questions...):
http://www.noxa.org/blog/2011/08/21/building-an-xbox-360-emulator-part-6-code-translation-techniques/
http://forums.pcsx2.net/Thread-blog-Introduction-to-Dynamic-Recompilation

Exophase
January 6th, 2012, 05:15
No offense Prads but telling this guy about dynarecs now is not all that helpful when he can't figure out how to decode instructions..

gfdooler your questions are just too open ended. We can't really figure out what it is that you're struggling with. The best thing for you to do is try writing something as much as you can and figuring things out as you go. If you get stuck then you can ask specific questions.

Maybe what you should do is, instead of starting with a GBA emulator, write something that is nothing more than an ARM interpreter. Then do this:

1) Write a little test program in C that does some math on some values that are initialized to constants. Don't use global variables. Have it return the answer in main. Now grab an ARM cross compiler (like Code Sourcery), compile, and use objdump to convert it to binary format.

2) Get an assembly listing (-S or use objdump) and make sure you understand what every instruction is doing.

-- OR just do it all in ARM ASM to begin with. --

3) Run it through your interpreter. Have r13 start pointing to the top of some big block of emulated memory. Run the whole program until it terminates by trying to return and look for the answer in r0.

4) When it doesn't work step through each instruction one at a time and look at the results. Find out why.

gfdooler
January 6th, 2012, 18:20
thank you very much for your helps and for your advices ,i will follow it and if i get stuck a will ask you