6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Thu Nov 21, 2024 9:25 pm

All times are UTC




Post new topic Reply to topic  [ 12 posts ] 
Author Message
PostPosted: Mon Jul 30, 2007 12:55 pm 
Offline

Joined: Thu Jul 26, 2007 7:07 pm
Posts: 7
Location: Amsterdam
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.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon Jul 30, 2007 3:42 pm 
Offline

Joined: Sat Jan 04, 2003 10:03 pm
Posts: 1706
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.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon Jul 30, 2007 7:24 pm 
Offline
User avatar

Joined: Sun Feb 13, 2005 9:58 am
Posts: 85
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.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Jul 31, 2007 1:49 pm 
Offline

Joined: Thu Jul 26, 2007 7:07 pm
Posts: 7
Location: Amsterdam
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...


Top
 Profile  
Reply with quote  
 Post subject: Emulation speed factor
PostPosted: Tue Jul 31, 2007 2:38 pm 
Offline

Joined: Sun Jul 01, 2007 8:57 pm
Posts: 9
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.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Jul 31, 2007 8:35 pm 
Offline
User avatar

Joined: Sun Feb 13, 2005 9:58 am
Posts: 85
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!


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Jul 31, 2007 9:07 pm 
Offline

Joined: Sat Jan 04, 2003 10:03 pm
Posts: 1706
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.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Aug 02, 2007 7:25 pm 
Offline

Joined: Thu Jul 26, 2007 7:07 pm
Posts: 7
Location: Amsterdam
OK, thanks. I downloaded the archive. I will let you know about any progress/problems.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri Aug 03, 2007 9:14 pm 
Offline
User avatar

Joined: Sun Feb 13, 2005 9:58 am
Posts: 85
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... ;)


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Aug 09, 2007 8:48 pm 
Offline

Joined: Thu Jul 26, 2007 7:07 pm
Posts: 7
Location: Amsterdam
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...


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri Aug 10, 2007 7:42 pm 
Offline
User avatar

Joined: Sun Feb 13, 2005 9:58 am
Posts: 85
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!


Top
 Profile  
Reply with quote  
PostPosted: Sat Aug 14, 2010 3:45 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10985
Location: England
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.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 10 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: