Page 1 of 1

Running 6502 code as a subroutine in a C program?

Posted: Mon Jul 30, 2007 12:55 pm
by H.G.Muller
Hi, I am new here!

Does there exist a convenient way to use routines written in 6502 assembly in modern C programs for running on a PC?

I was tinking about some general 6502 interpreter routine, available in C source, to which I could pass an array with 6502 code, which would then run that code, and afterwards return to the caller. (Which then could fetch results produced by the 6502 code, for further processing.)

The reason I would want this is to revive my old 1980s Chess program, to be able to play it on a PC in a modern environment (using the interface to communicate its moves to the outside world according to modern standards from my new Chess program, just using the 6502 code to think up a move). All this for nostalgic reasons.

Posted: Mon Jul 30, 2007 3:42 pm
by kc5tja
Your interpreter idea is the only way to achieve this.

Consider, what you're doing (interpreting bytecodes) is precisely the same thing as what any other high-level language callback mechanism is doing -- deferring to an interpreter to compute some result, then returning said result.

Although, in your case, it'll be fetched by peeking bytes into the VM's memory area, which is also passed as a raw character array in most cases.

I would offer my own lib65816 emulator library, but alas, it is not optimized for this mode of operation. The next rewrite of lib65816 will be, however, because doing so will allow me to emulate multiple CPUs in a single system.

Posted: Mon Jul 30, 2007 7:24 pm
by ptorric
Hi!

if you are interested, i've done a little 6502 interpreter that can run without any modification the original binary code of the famous 1k chess 6502 program.
it's in C and maybe not too much accurate for critical timing simulation.

because using vice or similar code is complex and it's not so simple isolate the 6502 core, i choose to write a new one.

let me know if it's interesting for you.

Posted: Tue Jul 31, 2007 1:49 pm
by H.G.Muller
Yes, it sounds interesting!

If I have the C source code of the core of a 6502 emulator, i.e. the part that advances the state of a byte array that represents the memory content of the emulated 6502, I could simply compile it with the rest of my program, and call it from there.

Like you remark: the simpler the better. It would be a pain to have to peel the small part I need out of an elaborate 6502 emulator / assembler / debugger environment with a complex user interface. Plus that I am really new to 6502 emulation, so I would not even know what is around (in C-source form).

Speed is not much of an issue, as I would not require the Chess program to search any deeper than it used to do on my AIM 65. So with current CPU speeds that means I can afford to lose a factor 1000 in emulation...

Emulation speed factor

Posted: Tue Jul 31, 2007 2:38 pm
by sje
I wrote a Z80 emulator that works at the microcode level (if the Z80 had had a microcode implementation) along with a complete front panel window that updated at 60 Hz. I managed to run the emulated Z80 at one eighth of the host machine clock rate. One trick employed was the use of lookup tables for setting the parity and decimal adjust flags; the 6502 should be easier as it doesn't have these although tables may be useful for add/subtract when the 6502 BCD flag is set.

Posted: Tue Jul 31, 2007 8:35 pm
by ptorric
you can find sources + exe + uchess.bin in http://www.tsc4.com/6502.rar

this is a temporary url, i haven't a personal web so i put it in my company one.

lot of 6502 code is derived from an ancient aminet source, you can find the original author in a header.

i made a "special" instruction: when the emulator get a 0 (zero), it execute a kind of syscal, byte follow is the parameter call.

this is useful for i/o purpose.

if you need help please write me!
bye!

Posted: Tue Jul 31, 2007 9:07 pm
by kc5tja
This is, in fact, the newly intended interpretation for the BRK instruction (it 'breaks' to the OS). There is also COP ($02) as well, which is intended for different things, but which works similarly on the 65816.

Posted: Thu Aug 02, 2007 7:25 pm
by H.G.Muller
OK, thanks. I downloaded the archive. I will let you know about any progress/problems.

Posted: Fri Aug 03, 2007 9:14 pm
by ptorric
kc5tja wrote:
This is, in fact, the newly intended interpretation for the BRK instruction (it 'breaks' to the OS). There is also COP ($02) as well, which is intended for different things, but which works similarly on the 65816.
thank you for the info!
i really know nothing about the "new" 65816, maybe during summer vacation... ;)

Posted: Thu Aug 09, 2007 8:48 pm
by H.G.Muller
Well, so far I succeeded in bringing my old Chess program in machine-readable form (by scanning the machine code from a book with OCR), and it seems to run on an emulator I downloaded (after adapting the I/O to suite the needs of this particular emulator).

So I am in business...

Posted: Fri Aug 10, 2007 7:42 pm
by ptorric
H.G.Muller wrote:
Well, so far I succeeded in bringing my old Chess program in machine-readable form (by scanning the machine code from a book with OCR), and it seems to run on an emulator I downloaded (after adapting the I/O to suite the needs of this particular emulator).

So I am in business...
Congratulations!

hey...but...now we wanna play!

Re: Running 6502 code as a subroutine in a C program?

Posted: Sat Aug 14, 2010 3:45 pm
by BigEd
H.G.Muller wrote:
Does there exist a convenient way to use routines written in 6502 assembly in modern C programs for running on a PC?

I was thinking about some general 6502 interpreter routine, available in C source, to which I could pass an array with 6502 code, which would then run that code, and afterwards return to the caller.
I think Ian Puimarta's lib6502 could be used this way - I don't know if it would be preferable or not compared to ptorric's (Trzynadlowski's) version above.

Note also Bill Forster's approach which he applied to Peter Jenning's Microchess - downloadable here - which is more of a manual translation, using macros for opcodes and functions for subroutines.