home *** CD-ROM | disk | FTP | other *** search
/ Der Mediaplex Sampler - Die 6 von Plex / 6_v_plex.zip / 6_v_plex / DISK3 / DFUE_100 / MEGATXT.ZIP / EMU-MECH.TXT < prev    next >
Text File  |  1994-02-07  |  102KB  |  2,091 lines

  1. Due to the discussion on the software emulation of other systems on the
  2. MEGADEV mailing list, I am uploading this comp.sys.amiga.emulations
  3. discussion which occured in 1991.  It does contain quite a bit of useful
  4. information and ideas for anyone interested in producing their own
  5. emulators.  It specifically concentrates on the 6502 and Z-80, although
  6. the concepts are generally applicable for other systems as well.
  7.  
  8.                         Ian.
  9.  
  10. >From: jnmoyne@lbl.gov (Jean-Noel MOYNE)
  11. >Newsgroups: comp.sys.amiga.emulations
  12. >Subject: Re: Emulator Mechanics (sorry long post)
  13. >Message-ID: <10652@dog.ee.lbl.gov>
  14. >Date: 6 Mar 91 03:03:52 GMT
  15. >Organization: Lawrence Berkeley Laboratory
  16. >References:<4992@mindlink.UUCP> <1991Mar6.010141.5905@mintaka.lcs.mit.edu>
  17. >
  18. >
  19. >            Yes, insteresting indeed. I've _heard_ (i.e. don't quote me on 
  20. >that) that once a company did such a "application-translator", on unix. 
  21. >Where you could take an Ms/Dos exe file, and the program would generate a 
  22. >unix C source code, maping all the Ms/Dos int traps into legal unix os 
  23. >calls. I've heard this program was able to translate well-known commercial 
  24. >programs, and that these programs were able to run after that under normal 
  25. >unix multi-user configuration, the end of the story is that the people 
  26. >that made this soft could never even try to sell it (guess why).
  27. >
  28. >        Anyway, doing such a thing wouldn't be impossible technically (but 
  29. >it'd be hard for sure ! (-:), especially for ms/dos programs. All these 
  30. >big softs are made to run on all the different flavors of IBM clones, that 
  31. >means they use no dirty tricks, they stick to BIOS calls so it'll run even 
  32. >on an Amstrad (or under the transformer or IBeM for instance). That means 
  33. >you could quite easily put all the BIOS ints in an Amiga shared lib, and 
  34. >taking the 8088 binary, generate C code from it. Programs a'-la 
  35. >"re-sourcer" allready exist, they take the .exe file, and generate a 8088 
  36. >source code file from it, at this point it's possible to generate a C 
  37. >source code that would do the same thing. You could optimise it to 
  38. >recognise patterns in the ASM instructions (don't forget most of these big 
  39. >programs, the one you're interested in to run on the Amiga, are usually 
  40. >written in C or some other high-level language, and so the binary is 
  41. >generated by a compiler, thus creating patterns ...), and eventually build 
  42. >a C source code not to far from the original (the guys who wrote Microsoft 
  43. >C could eventually write a reverse compiler that would generate a code 
  44. >very close to the original, and such a program wouldn't be machine 
  45. >dependent at all).
  46. >
  47. >         An other problem who be to catch the busy wait loops in these 
  48. >mono-tasking programs (I think most of these loops are in the BIOS, like 
  49. >waiting for a key, easy to take care of). So these programs would be 
  50. >multitasking friendly, and so that you could eventually run more than one 
  51. >at the same time without loosing performance.
  52. >
  53. >        Anyways, all this is my mind wandering sideways (I've thought 
  54. >about that before), and most of what I wrote is closer to dreams than to 
  55. >reality (and in bad English, sorry about that, still learning (-:). Mostly 
  56. >because, if it's not impossible to write such a program, it for sure very 
  57. >hard ! And so, it'll take a lot of time and effort, more than what a 
  58. >standalone guy doing that on the side of his regular job could invest. A 
  59. >team in a company could do it, but no company's gonna take such a risk, 
  60. >there's too much potential danger on that, even if the potential sales are 
  61. >very big ! Sure one could say that if you buy the original program, you 
  62. >should be able to run it ... even on another machine, but I'm sure the 
  63. >army of lawyers from let's say Microsoft or Aston-Tate would have 
  64. >something to say about that ! And they could sue you for years without 
  65. >sweat, preventing you from selling the program before the end of the 
  66. >trial... etc ... etc ... 
  67. >
  68. >         Eventually, if the program was freeware, written by a bunch of 
  69. >programers not earning money for it, this would be a different figure, 
  70. >much different. But I'm really no lawyer, I'm still thinking sideways .... 
  71. >(-:
  72. >
  73. >
  74. >            JNM
  75. >
  76. >--
  77. >These are my own ideas (not LBL's)
  78. >
  79. >
  80. >From: Chris_Johnsen@mindlink.UUCP (Chris Johnsen)
  81. >Newsgroups: comp.sys.amiga.emulations
  82. >Subject: Emulator Mechanics (sorry long post)
  83. >Message-ID: <4992@mindlink.UUCP>
  84. >Date: 4 Mar 91 15:20:48 GMT
  85. >Organization: MIND LINK! - British Columbia, Canada
  86. >
  87. >
  88. > The following messages were shared in an email environment between Charlie
  89. >Gibbs and myself over the last couple of days.  We decided that it would be a
  90. >good idea to share these thoughts with the readership of
  91. >comp.sys.amiga.emulations.  The messages speak for themselves for the most
  92. >part.
  93. >
  94. >         ------------ Included text of messages ------------
  95. >
  96. >Sun Mar 3 6:40:53 1991  From: Chris Johnsen [547]  Subject : Emulators
  97. >
  98. >Hello Charlie!  Long time no talk to you eh?  I haven't seen you for quite some
  99. >time.  I was particularly fond of those after-PaNorAmA meetings at Wendy's a
  100. >few years ago.  I trust you recall me from back then.
  101. >
  102. >I have been following the comp.sys.amiga.emulations newsgroup on Usenet for
  103. >quite awhile now and was quite interested in the IBeM program that is being
  104. >posted about recently.  In one of the near Alpha/Theta states that, being a
  105. >programmer,  such as yourself, one recognizes as some kind of "source"; I had
  106. >an idea. Since there are not many "new" ideas spawned I wanted to get some
  107. >feedback from a person with the expertise you possess.  The reasons I thought
  108. >of you for this private consultation are:  1) you've written an emulator  -
  109. >SimCPM 2) you've written an assembler - A68K  3) you have a broad knowledge  of
  110. >the computing field beyond lowly PC's and  4) you're such a nice guy!
  111. >
  112. >I was musing about how an emulator would work.  I must confess I have no
  113. >concrete knowledge of this.  It has been said that "Not knowing how to do a job
  114. >'right' frees you to discover new and better ways of accomplishing the desired
  115. >result".  Freeman Patterson (a Canadian photographer), in his book Photography
  116. >and the Art of Seeing, called this "thinking sideways".  I  believe I've
  117. >thought of a way of writing an emulator that would run at least  as fast on the
  118. >Amiga as it would on the source machine.
  119. >
  120. >My questions for you are:
  121. >
  122. >Would you be interested in being a mentor to me in developing further this idea
  123. >or in fact ascertaining whether my concept has any validity?
  124. >
  125. >I'm reticent to spill this on the net until I'm somewhat confident that it is
  126. >indeed practicable.  I have thought about the possible legal considerations of
  127. >producing an emulator.  There are potential commercial possibilities that one
  128. >could consider also.
  129. >
  130. >Would you please give me just a quick, superficial rundown of the basic
  131. >algorithms used in developing an emulator?
  132. >
  133. >I have assumed that you would read in the object module of say an IBM
  134. >executable, read it by opcode or routine, decipher the intent and then call a
  135. >library of glue routines to do the job that the program would have on an  IBM
  136. >or clone.  I have no idea how the interrupt structure would be handled but know
  137. >that you have done it with SimCPM.
  138. >
  139. >I don't want to waste your time, but I would appreciate this information. I
  140. >would like to get your feedback on these questions to see if you are interested
  141. >in further discussion with me on this matter.  If you are not interested or
  142. >don't have the time I'll certainly understand.  I realize I'm asking you for
  143. >more information than I'm sharing but an idea is a tiny property and I'd like
  144. >to at least savor this for a while before I decide what to do with it.  That is
  145. >the sole reason I have not gone public with it yet.
  146. >
  147. >If you are indeed interested, after you tell me on the highest level how an
  148. >emulator functions, I'll be able to describe this idea on some kind of
  149. >comprehensible basis. I'm looking forward to your response!  Thanks Charlie.
  150. >
  151. >csj
  152. >
  153. >Sun Mar 3 23:12:32 1991  From: Charlie Gibbs [218] Subject : Emulators
  154. >
  155. >     Indeed I am somewhat short of time these days, but I wouldn't mind kicking
  156. >the odd idea around without getting too wrapped up in it.  I do understand your
  157. >idea of "thinking sideways" and enjoy being able to do it myself from time to
  158. >time.  A similar way I've heard it described is that even though it's been
  159. >proven that bumblebees can't fly, they don't realize this and so do it anyway.
  160. >Or to put it another way, I like to write programs that are too stupid to know
  161. >what they're doing, so they can do anything.
  162. >
  163. >     Your ideas on emulation are basically in line with what's considered the
  164. >standard way of doing things.  A machine instruction is analyzed as to just
  165. >what it's supposed to do, and appropriate code then carries out the operation.
  166. >The guts of SimCPM appeared in Dr. Dobb's journal as an emulator that was meant
  167. >to run under CP/M-68K.  This made the job a bit easier for the original author,
  168. >since the CP/M-68K system calls were quite similar to the CP/M calls that he
  169. >was emulating.  I had to replace this portion of code with appropriate AmigaDOS
  170. >routines.  In addition, I extended the code to handle the full Z-80 instruction
  171. >set, since the original code could only handle the 8080 subset.
  172. >
  173. >     Since emulating another processor in software is quite a CPU-intensive
  174. >process (several machine instructions have to be executed to emulate a single
  175. >machine instruction on the target machine) I tried to optimize SimCPM for speed
  176. >at the expense of memory and redundant code.  The overhead of a single
  177. >subroutine call, plus any extraction and interpretation of arguments, would
  178. >require several times as much time as a hand-picked set of instructions
  179. >dedicated to a single opcode.
  180. >
  181. >     For system calls there's easy out - as soon as I recognize what the
  182. >emulated program is trying to do (e.g. read a block from a disk file), I call
  183. >the corresponding AmigaDOS routine, so I/O can proceed pretty well at native
  184. >speeds.  Therefore, even though CPU-bound programs might run 10% as fast as
  185. >they would on the target machine, I/O bound programs might get up to 50% of
  186. >speed.
  187. >
  188. >     Interrupts were easy - since most CP/M systems don't use hardware
  189. >interrupts, except possibly for a very few hardware-dependent programs, I
  190. >simply didn't worry about them.  Software interrupts (the RST instruction) were
  191. >a snap on the other hand, since they're basically a special-purpose subroutine
  192. >call.
  193. >
  194. >     The Intel 8080 is fairly easy to emulate because the opcode is uniquely
  195. >determined by the first byte of the instruction.  Some instructions might have
  196. >register numbers encoded in a few bits of that first byte, but I just treat
  197. >them as special cases.  To decode the byte, I just multiply its value by 4
  198. >(shift left 2 bits) and use the result as an index into a table of 256 pointers
  199. >to the actual emulation routines.  Since there are 64 possible MOV instructions
  200. >(well, 63 because one of the bit combinations is actually HLT) I actually have
  201. >63 MOV emulations, one for each combination of registers.  This means that I
  202. >don't have to do any register decoding, since each routine consists of a
  203. >dedicated 68000 MOVE instruction, followed by a jump back to the main emulation
  204. >loop.  Lots of almost-redundant code, but it's about as fast as it can get.
  205. >
  206. >     This is getting kind of long-winded.  I'd be interested in hearing any
  207. >ideas you may have; although I wouldn't have time to get into the programming
  208. >of such stuff, I'm willing to act as a sounding board.  Talk to you soon... CJG
  209. >
  210. >Sun Mar 3 23:18:48 1991  From: Charlie Gibbs [218]  Subject : Emulators
  211. >
  212. >     Another approach I've heard of is to "compile" the code to be emulated
  213. >into native machine code.  This would involve a front-end program which would
  214. >read the target machine's program and analyze the instructions.  For instance,
  215. >if the "compiler" detects an instruction that does a move between two of the
  216. >emulated machine's registers, it would simply generate a move instruction in
  217. >the emulating machine's code.  It could generate either a translated assembly
  218. >language source file or a machine-language file ready to load into the
  219. >emulating machine.  This would require the "compilation" process to be run once
  220. >on the program to be emulated, and you'd then run the output of this
  221. >"compiler."  There are special tricks to consider here, such as resolving
  222. >addresses - you couldn't just copy the memory addresses across because the
  223. >emulated routines would likely be a different size.  It might be easier to
  224. >generate a label (e.g. Axxxx where xxxx is the hex address in question) in an
  225. >assembly source file and let the emulating machine's assembler sort it all out.
  226. >
  227. >     I've never actually seen this process in action, but it's another
  228. >possibility.  --CJG
  229. >
  230. >Mon Mar 4 12:38:31 1991  From: Chris Johnsen [547]  Subject : Emulators
  231. >
  232. >Thanks a lot for your effort in explaining SimCPM to me man.  As you  describe
  233. >it, it would seem that I had intuitively understood the basic concepts.  I
  234. >would think that interrupts would be the hardest part to get down to reliable
  235. >operation.  What I had in mind, while thinking about  this, in general terms,
  236. >was an emulator that was non-specific as to the  machine, therefore I was
  237. >attempting to contemplate it handling say IBM,  Mac, (hey it may even be
  238. >possible to deal with Amiga emulation!) and  Atari ST on the Amiga and
  239. >imagining what the various architectures would  require.  All this on a very
  240. >abstract level.
  241. >
  242. >Your second message hit the nail on the head!  I got bogged down at about the
  243. >level you describe in your first message.  Lots of details to be sought and
  244. >worked out.  Gee, I'm really not even available to code another program just
  245. >yet anyway.  I was giving the whole concept a rest when, what I  thought of,
  246. >kind of sideways (lazy minds tend to look for an easier way  around an
  247. >obstacle, sometimes unconsciously, even though this can lead to  harder, though
  248. >more elegant solutions to problems), was to read the opcodes from the "source
  249. >executable" of the emulated machine, producing an assembly listing of the
  250. >program.  This I imagine would be a two pass process, sort of like a C
  251. >compiler, followed by an assembler's two passes, and finished off with a
  252. >linker.
  253. >
  254. >I thought that, if the compiler was "intelligent" enough, the output, though
  255. >likely larger, would be much faster than the common "interpreter type
  256. >emulator".  I had never heard of such an idea and since there are none out
  257. >there, wanted to discuss this with you.  I have developed the idea no further
  258. >than this in essence.
  259. >
  260. >I did think of a few other considerations however.  If one could, indeed,
  261. >compile an executable image of say Lotus 123 from the IBM into a program which,
  262. >on a base Amiga, could run at half speed, or on a A2500 or A3000 at twice the
  263. >speed, it would be a viable alternative, besides being a neat toy.  However,
  264. >the standalone program generated would likely infringe on the copyright of
  265. >Lotus because the Amiga executable would actually be Lotus 123.  Take
  266. >WordPerfect for instance.  The latest version available is 4.1 or just a
  267. >micro-point higher, no problem, get hold of the IBM version 5.1, I believe it
  268. >is, and compile it and you have something some other people are wailing for. Of
  269. >course the rebuttal (I can hear you thinking?) is that, if a person owns
  270. >WordPerfect he has an inalienable right to run it. Run it on an IBM.  Run it on
  271. >a clone.  Run it through an Amiga compiler.  You know, if it's for personal
  272. >use, etc.
  273. >
  274. >As to the increase in size of the "compiled emulation" program, I have a couple
  275. >of ideas.  First, the executable, though larger, would be standalone, except
  276. >for any support libraries.  This doesn't mean that this "form" of emulator,
  277. >more like a "translator of executables", would be any less efficient than the
  278. >"interpreter type".  Perhaps more memory efficient in a couple of possible
  279. >ways.  Since the interpretation section of the program is in the compiler, and
  280. >the source executable is not required at runtime, memory usage may well be less
  281. >with a "compiled emulation".  The second concept is to use link libraries which
  282. >would bind only the emulation routines required to the final program.  Possibly
  283. >a combination of bound-at-link-time modules of less frequently used routines
  284. >and a shared  library of essential routines all programs would need.  A solely
  285. >link library approach would leave this concept open to claims that pirates
  286. >could produce "warez" that need no extra code or setup to work.  Of course,
  287. >pirates appear to be capable of ripping anything off anyway!
  288. >
  289. >This "compiled emulation" would, given sufficient memory and CPU
  290. >speed/efficiency, allow the running of multiple programs.  Both emulated
  291. >programs and standard Amiga programs.  Through the use of a shared library more
  292. >than one emulating program could be run without the overhead of multiple
  293. >"emulation interpreters" resident in memory.
  294. >
  295. >The compiler could generate C statements so that you could take advantage  of
  296. >the advancements in technology in the compiler, assembler and linker,  without
  297. >having to deal, directly, with those parts of the system.  I know  this would
  298. >make the compiler operation more unwieldy.  More operations,  therefore it
  299. >would take longer, but theoretically the source is bugless,  so you would
  300. >expect the output of this "emulation compiler" to either  succeed or fail.
  301. >You'd run the emulator on the program only once.  The  beauty of producing
  302. >assembler (C would be better here), is that if it  didn't work first time, a
  303. >programmer type could patch it up in source and get it running.  I'm really
  304. >intrigued by this idea.  Where did you hear about it, do you remember?
  305. >
  306. >My knee-jerk reaction initially was to file the idea, but then I got to
  307. >wondering why no one had done it.  There were many emulators out there for
  308. >various source machines.  Why were none of them compilers?  Another idea I had
  309. >was to contact the dude in Oz (or is he a Kiwi?) that wrote IBeM.  He already
  310. >has the emulation working except for the parallel and serial ports. It would
  311. >appear that he reads the IBM object code, deciphers it and runs a routine, or
  312. >simply does a MOVE using an opcode lookup table, as you suggest; an
  313. >interpreter.  If he instead simply wrote out an instruction in ASCII to do the
  314. >call or move instead, using a shared library of his emulation routines, he'd
  315. >basically have it.  The end user would also have to have an assembler or C
  316. >compiler, however.  This type of approach has got to produce faster emulation,
  317. >if it is possible.  I believe it to be.
  318. >
  319. >Anyway, that's what I had in mind Charlie.  I really do appreciate your
  320. >feedback on this.  Care to comment on any directions you think could be
  321. >followed?  Do you know anyone with enough venture capital to fund the further
  322. >development of this concept? ;-)  Do you think I should approach the author of
  323. >IBeM (cute name) directly?  Or, should this private discussion we've been
  324. >having be moved to Usenet?  Thanks again Charlie!   I appreciate you man.
  325. >
  326. >csj
  327. >
  328. >Mon Mar 4 15:36:53 1991  From: Charlie Gibbs [218] Subject : Emulators
  329. >
  330. >     I can't remember where I first heard of the idea.  The converted code
  331. >won't necessarily be smaller than the original, depending on the relative sizes
  332. >of corresponding machine instructions on both machines.  However, if you could
  333. >make the compiler really smart it might be able to recognize certain sequences
  334. >of instructions and replace them by sequences designed to accomplish the same
  335. >thing more efficiently.  For instance, since the 8080 doesn't have a multiply
  336. >instruction it needs to fake it with a bunch of adds and shifts.  A smart
  337. >compiler, if it could recognize such a routine, could replace it with a single
  338. >68000 multiply instruction and see huge savings.
  339. >
  340. >     I'd stay away from calling subroutines; the overhead could kill you.
  341. >
  342. >     The copyright issue could be a sticky one, although I can't see any
  343. >problems if you run the converter on your own copy of the emulated software and
  344. >don't try to sell the result.  It would no doubt be classified as a "derivative
  345. >work".
  346. >
  347. >     Perhaps it might be interesting to throw this discussion out to Usenet. It
  348. >won't be a trivial job, which is probably why we haven't seen it done
  349. >elsewhere.  Remember that a straight machine-code emulation duplicates all the
  350. >register fiddling that is required by the target machine's architecture (and
  351. >the 80x86 family needs a LOT of register fiddling).  This code is replaced by
  352. >the 680x0's own internal fiddling if you're re-compiling source code.  One way
  353. >of looking at it is to decompile the original machine code, then recompile it
  354. >for the new machine.
  355. >
  356. >     Interesting stuff...  CJG
  357. >
  358. >     ------------ End of included text of messages. -----------
  359. >
  360. >Both Charlie Gibbs and myself frequent this newsgroup and look forward to any
  361. >additions to this discussion with which others may respond.  Sorry that  the
  362. >posting is so long but I felt there was little enough chaff contained  in the
  363. >messages to warrant including all of them.
  364. >
  365. >csj
  366. >
  367. >The hard way is usually the disguised easy way, you take your choice. Usenet:
  368. >a542@mindlink.UUCP Phone: (604)853-5426 FAX: (604)854-8104
  369. >
  370. >
  371. >From: rjc@pogo.ai.mit.edu (Ray Cromwell)
  372. >Newsgroups: comp.sys.amiga.emulations
  373. >Subject: Re: Emulator Mechanics (sorry long post)
  374. >Message-ID: <1991Mar6.010141.5905@mintaka.lcs.mit.edu>
  375. >Date: 6 Mar 91 01:01:41 GMT
  376. >References: <4992@mindlink.UUCP>
  377. >Organization: None
  378. >
  379. >
  380. >  Very interesting article. I myself have been tempted several times
  381. >to try and write an emulator. Since I programmed 6502 assembly on the
  382. >C64 for 4 years, and I know 68000 on the Amiga, I was tempted to
  383. >try to beat the speed of the other emulators. Then I realized
  384. >the sheer magnitude of the project. Emulating the instruction set is
  385. >easy. In fact, I am quite confident I can make a 6502 emulator run
  386. >faster on the Amiga then the C64. The hard part is the hardware.
  387. >Most C64 programs discard the oS entirely and bang on the hardware.
  388. >Further more, most of them use polling loops, like polling the 
  389. >raster beam register and using precisely cycle timed delays. Moreover,
  390. >the VIC chip contains several glitches that allow programmers to
  391. >use tricks to remove the borders, vertically scroll the screen to
  392. >ANY raster location, horizontally shift the screen, vertically and
  393. >horizonally interlace the screen, stretch pixels (double, triple,
  394. >quaduple) length vertically. This is virtually IMPOSSIBLE to
  395. >detect, unless the emulator is artifically inteligent.  And, any
  396. >program that has a fastloader won't work. This is because fastloaders
  397. >usually transfer data over the serial clock line, and data line. This
  398. >doubles bandwidth, unforunately it requires PERFECT timing, so perfect
  399. >in fact that it won't work on PAL computers, and vice versa.
  400. >Sprites are another problem, since Amiga sprites are only 16 pixels wide, and
  401. >C64 sprites can have their width and heigth doubled, and they rely
  402. >on a chunky pixel format.  Text is another problem since the
  403. >C64 has a builtin Text mode.
  404. >
  405. >   The Mac is the easiest computer to emulate because it's not a computer
  406. >at all. The Macintosh computer does not exist, it's nothing more
  407. >than a ROM chip.
  408. >
  409. >A few days ago, I was impressed. I downloaded a demo from ab20 called
  410. >C64Music.zap. This demo emulates 6502 at 100% (in fact, it emulates it
  411. >at perfect timing because the music is exactly the same speed.) This
  412. >demo emulates the SID chip PERFECTLY, and I mean perfect. These guys
  413. >should join together with the maker of A64.
  414. >
  415. >
  416. > I can't speak for other 6502 emulators, but if I wrote one, the fastest
  417. >method looks like table lookup, with careful optimization to make
  418. >sure things are long word aligned. For instance, I might do something like
  419. >
  420. >pc6502 equr a5
  421. >accum  equr d5
  422. >xindex equr d6
  423. >yindex equr d7
  424. >stack  equr a4 ; 6502 stack, which is base address + $0100 on the C64
  425. >stat   equr d4 ;status register
  426. >
  427. >
  428. >
  429. >              [allocmem the 6502's address space, load in an
  430. >            executable and set the pc to it's start]
  431. >
  432. >       lea jumptbl(pc),a2
  433. >       sub.l d0,d0
  434. >loop   move.b (pc6502)+,d0
  435. >       lsl.l #2,d0
  436. >       move.l (a2,d0),a3
  437. >       jmp (a3)
  438. >
  439. >
  440. >then every instruction would be emulated (even undoc's) and put into
  441. >the jumptbl. The code for 'LDA address' might look like:
  442. >
  443. >lda    sub.l d0,d0
  444. >       move.b (pc6502)+,d0
  445. >       lsl.l #8,d0
  446. >       or.b (pc6502)+,d0
  447. >       add.l addresspace,d0 ;this code inverts the 6502 little-endian
  448. >                        ; and then add's the base address of the
  449. >                    ; memory that was alloc's for it
  450. >       move.l d0,a3
  451. >       move.b (a3),accum
  452. >       jsr GetCC() ?  ;It might be better to use move SR, providing you
  453. >                  ; check the machine you were running on and did
  454. >                      ; and did a move ccr otherwise
  455. >                     ; status reg is now in d0
  456. >       and #mask,d0   ; mask off everything but the Z bit
  457. >       bne whatever
  458. >whatever bclr #1,stat
  459. >       jmp loop
  460. >       bset #1,stat
  461. >       jmp loop
  462. >
  463. >(note: this code can be optimized, its off the tip of my tongue, and
  464. >probably bugged since I haven't coded in asm in awhile)
  465. >
  466. >>From my quick calculations, the jump table dispatcher incurs about a 3-4
  467. >microsecond delay in the fetch of each instruction. This is equivelent
  468. >to about 4 cycles on a 6502 @1.02mhz. If you had infinite amounts of ram,
  469. >the object code loader could 'inline' the code for each instruction
  470. >and get rid of this delay, I beleive this is probably how the C64Music
  471. >demo does it, since music players on the C64 were only about 1k of code.
  472. >
  473. >The Lda routine itself looks like about 2.2 times slower than a true 6502
  474. >delay which 4 cycles. However a 25mhz 68030 would run more than 
  475. >twice as fast.
  476. >
  477. >
  478. >Theoretically speaking, an IBM emulator running on an Amiga3000
  479. >should be running at atleast 5mhz 80286 speed. Consider SoftPC on
  480. >the NeXT which runs at 12mhz UNDER UNIX. 68040's are about 2-3 times
  481. >faster then 68030's, so SoftPC on the Amy should run at about 5mhz.
  482. >
  483. >
  484. > Maybe we all should trying something like 'NetIBM'. What I mean, is
  485. >like Nethack, we should all participate in coding an IBM emulator.
  486. >Each person might post a small code segment (in assembler), the rest
  487. >of us can compete optimize it. I remember having a contest with
  488. >some trying to optimize the 'size' of a binary to decimal print
  489. >routine, the final result was the code was reduced 300% in size.
  490. >(we kept passing the optimized source back and forth, each shedding
  491. >a few bytes.)
  492. >
  493. >
  494. >Regardless of what happens. Let's keep the discussion up, it's interesting
  495. >and educational.
  496. >
  497. >
  498. >       
  499. >
  500. >
  501. >From: ifarqhar@sunb.mqcc.mq.oz.au (Ian Farquhar)
  502. >Newsgroups: comp.sys.amiga.emulations
  503. >Subject: Re: Emulator Mechanics (sorry long post)
  504. >Message-ID: <1303@macuni.mqcc.mq.oz>
  505. >Date: 6 Mar 91 12:57:34 GMT
  506. >References: <4992@mindlink.UUCP> <1991Mar6.010141.5905@mintaka.lcs.mit.edu>
  507. >Organization: Macquarie University, Sydney, Australia.
  508. >
  509. >In article <1991Mar6.010141.5905@mintaka.lcs.mit.edu> rjc@pogo.ai.mit.edu (Ray Cromwell) writes:
  510. >>   The Mac is the easiest computer to emulate because it's not a computer
  511. >>at all. The Macintosh computer does not exist, it's nothing more
  512. >>than a ROM chip.
  513. >
  514. >The best description of a Mac I've ever read! :-)
  515. >
  516. >> I can't speak for other 6502 emulators, but if I wrote one, the fastest
  517. >>method looks like table lookup, with careful optimization to make
  518. >>sure things are long word aligned. For instance, I might do something like
  519. >
  520. >How about this?
  521. >
  522. >Write code for every one of the 256 possible opcodes, and make sure that
  523. >it follows these important considerations:
  524. >
  525. >1)  The code is responsible for updating all registers, the 6502PC,
  526. >    and also for checking special functions if a hardware location
  527. >    is called.
  528. >2)  It should be 256 bytes per opcode or less.
  529. >3)  It should jump back to STLP (see below) when done.
  530. >4)  It should intelligently introduce timing delays (very hard)
  531. >5)  Something else I've forgotten.
  532. >
  533. >Take each piece of code, and distribute it in a 64K memory area, so that
  534. >the code for opcode N starts at base + (n << 8).  There may be a lot of
  535. >wasted space, but on any reasonable Amiga, this space can be sacrificed
  536. >for speed IMO.
  537. >
  538. >    ; Base reg of instruction code in A0.  6502PC in A1.
  539. >
  540. >STLP    MOVE.B    (A1), lcna    ; lcn is the upper byte of the
  541. >                ; dsp displacement below
  542. >    JMP    dsp (A0)
  543. >
  544. >
  545. >Excuse my poor 68K code.  I think this will work, but it has been three
  546. >years since I even looked at assembler on the 68000.  Before anyone
  547. >screams about this code failing on a 68030, remember that the
  548. >modification made above is only needed until the JMP.  After that, we do
  549. >not need to see it again.  If I read the info on the 68030 cache
  550. >properly, this should be okay.  At worst, the instruction cache can be
  551. >disabled.
  552. >
  553. >This should be considerably faster than any table driven system.  On any
  554. >system with multi-byte opcodes (eg. the 8088), this system can be easily
  555. >extended, and minor modifications can be made to emulate most modern CPU
  556. >with reasonable speed.
  557. >
  558. >Now a suggestion for dealing with hardware locations (harder on memory
  559. >mapped systems than on things like Intel and Zilog chips which have I/O
  560. >instructions.)  This is another memory-expensive solution, BTW.
  561. >
  562. >For every location, have two flags, one for read and one for write.  If
  563. >the opcode handlers write to the location, they can check the write flag
  564. >and if it is set, call a handler which determines which location is
  565. >being modified, and pass control to a routine to do it.
  566. >
  567. >Now for the VERY memory expensive solution.
  568. >
  569. >Have one 16 bit displacements per memory location (ie. for 1M of
  570. >simulated RAM you must have 3M of real RAM.)  This location contains the
  571. >displacement from a base register to a routine which handles that memory
  572. >location.  If this word is non-zero, an instruction that reads or write
  573. >to it calls this routine.  You could even have two displacements, one
  574. >for read and one for write, but this would only be practical for systems
  575. >that have small amounts of simulated memory (eg. C64).
  576. >
  577. >At a rough estimate, for a C64 emulator (IMO the hardest of all
  578. >emulators.)
  579. >
  580. >    Simulated Memory     64K
  581. >    Read Displacements    128K
  582. >    Write Displacements    128K
  583. >    Opcode handlers         64K
  584. >    Memory Location Handlrs     28K (?)
  585. >    Rest            100K (??)
  586. >                ----
  587. >                512K
  588. >
  589. >So, for an average 1M Amiga, this is quite possible.
  590. >
  591. >As for the project, why not start with something much simpler?  An Atari
  592. >2600 emulator.  The hardware is quite simple to simulate, though the
  593. >timing will have to be *very* precise.  This would be a good starting
  594. >point.  Opinions, anyone?
  595. >
  596. >--
  597. >Ian Farquhar                      Phone : + 61 2 805-9400
  598. >Office of Computing Services      Fax   : + 61 2 805-7433
  599. >Macquarie University  NSW  2109   Also  : + 61 2 805-7420
  600. >Australia                         EMail : ifarqhar@suna.mqcc.mq.oz.au
  601. >
  602. >
  603. >From: ifarqhar@sunb.mqcc.mq.oz.au (Ian Farquhar)
  604. >Newsgroups: comp.sys.amiga.emulations
  605. >Subject: Re: Emulator Mechanics (sorry long post)
  606. >Message-ID: <1304@macuni.mqcc.mq.oz>
  607. >Date: 6 Mar 91 13:52:25 GMT
  608. >References: <4992@mindlink.UUCP> <1991Mar6.010141.5905@mintaka.lcs.mit.edu> <1303@macuni.mqcc.mq.oz>
  609. >Organization: Macquarie University, Sydney, Australia.
  610. >
  611. >An idea about code compilation, rather than interpretation.
  612. >
  613. >If you assume that a program does not employ self-modifying code, this
  614. >may not be nearly as had as it first appears (I am not saying that it is
  615. >trivial, just not too bad.)  This assumption can be made if the program
  616. >runs out of ROM, and also in 99% of non-ROM cases.
  617. >
  618. >The problems of compiling code for one CPU into that for another are
  619. >fairly easy, but a major problem occurs when table-driven code is
  620. >employed (common.)
  621. >
  622. >What I am suggesting is this.  It is reasonably elegant, and quite
  623. >simple to perform:
  624. >
  625. >Store the loaded memory image of the original program as well as the
  626. >compiled code.  For every instruction in this image, a pointer to the
  627. >compiled code is generated indicating where the corresponding code
  628. >actually is.  As the program executes, the working store is this memory
  629. >image, and if a piece of data is read, it can be dealt with normally.
  630. >However, if a program attempt to jump, branch or call a location using
  631. >data loaded (as would happen with a table), then the corresponding
  632. >location can be determined and the correct code called.
  633. >
  634. >This has a high memory overhead (3-5 times original image size *plus*
  635. >compiled code).
  636. >
  637. >I am sure that there is a more memory-effective way of doing this by
  638. >identifying such tables during the compile, but it seems a difficult
  639. >problem.  Has anyone got any better solutions?
  640. >
  641. >BTW, an idea for semi-compiled code.  Have two branches for every location,
  642. >and simulate the machine that way.  Reading an opcode calls the
  643. >appropriate opcode handler, writing it calls a reevaluation routine that
  644. >changes the pointers for the two locations.  System and special
  645. >locations are handled similarly.  This may actually be a quite
  646. >reasonable solution, as inelegant as it first sounds.
  647. >
  648. >--
  649. >Ian Farquhar                      Phone : + 61 2 805-9400
  650. >Office of Computing Services      Fax   : + 61 2 805-7433
  651. >Macquarie University  NSW  2109   Also  : + 61 2 805-7420
  652. >Australia                         EMail : ifarqhar@suna.mqcc.mq.oz.au
  653. >
  654. >
  655. >From: rjc@geech.ai.mit.edu (Ray Cromwell)
  656. >Newsgroups: comp.sys.amiga.emulations
  657. >Subject: Emulator Mechanics
  658. >Message-ID: <1991Mar7.093149.18707@mintaka.lcs.mit.edu>
  659. >Date: 7 Mar 91 09:31:49 GMT
  660. >Organization: None
  661. >
  662. >
  663. > I got an idea. How about combining both methods? (compile and interpret)
  664. >
  665. > First compile the executable with flow analysis into 68k code. Any
  666. >instruction that tries to touch a hardware register will become
  667. >a trap instruction(or use an MMU to trap it) to emulate the hardware.
  668. >
  669. >  Where does the interpreter come in? Any code that tries to self modify
  670. >or do a change of state (table lookup, indirect jumps, etc) with be
  671. >interpreted.
  672. >
  673. > Sounds hard? You bet. The easier solution of all is to throw faster
  674. >CPUs at the problem.
  675. >
  676. >
  677. >Now for another question:
  678. >
  679. > Why doesn't AMAX multitask? It should be EASY to run multiple copies
  680. >of the Mac ROM as a task (since its a rom, its mostly pure code, except
  681. >each task would need to alter where the ROM things the system memory is.)
  682. >
  683. > For things like the Atari emulator, or another 68000 emulator, the compile
  684. >technique should work great! Just recompile the parts of the code
  685. >that bang on the hardware, or use an MMU to trap them.
  686. >
  687. >
  688. >The whole compilation process is made easier when the machine
  689. >your trying to emulate has an object code format that contains
  690. >seperate data and code chunks, and perhaps some relocation data.
  691. >Further more, if the machine has rules against self modifying code,
  692. >and a device independent OS it becomes trivial.
  693. >
  694. >Could someone run SI or dhrystone on IBeM and tell me how it performs?
  695. >Itis said that SoftPC on a Mac @25mhz 68030 runs at 6mhz AT speed.
  696. >
  697. >Does anyone know if IBM code uses self modifying code, or
  698. >jump tables? What kind of code does their compilers produce?
  699. >And, does the IBM has a special object code format that seperates
  700. >code and data?
  701. >
  702. >
  703. >The compilation technique could work well, but the compiler would have
  704. >to be VERY smart in its flow analysis of detecting code/data, jump
  705. >tables, self modifying code, PC counter and stack manipulation, etc.
  706. >
  707. >I'll clairfy my thoughts tommorow, it's going on 4:30am here, and I 
  708. >need to get some well deserved sleep. :-)
  709. >
  710. >
  711. >From: stevek@amiglynx.UUCP (Steve K)
  712. >Newsgroups: comp.sys.amiga.emulations
  713. >Subject: Re: Emulator Mechanics (sorry long post)
  714. >Message-ID: <stevek.3380@amiglynx.UUCP>
  715. >Date: 6 Mar 91 23:12:59 GMT
  716. >References: <4992@mindlink.UUCP>
  717. >Organization: Amiga-Lynx Bbs Multi-Node 201-368-0463 NJ,USA
  718. >
  719. >The idea of a "translator compiler" is very interesting.  I could have sworn I
  720. >saw some talk about a IBM->Atari ST translator that exists on FidoNET.  But
  721. >why is everyone talking about IBM?  Why not Macintosh programs?  Though I am
  722. >not anyting close to a professional programmer, I'd imagine programs that run
  723. >off different computers with the same processor would share some op-codes,
  724. >right?  Even if that is not true, they do both have similar GUIs, windows,
  725. >gadgets, and menu bars, ect. which shold make the translation easier and more
  726. >like the original.  I sincerly hope someone will pick up and persue these
  727. >ideas, it would be very benificial for me and others - I'd love to run a nice
  728. >pascal compiler like Turbo Pascal, and an excellent ANSI editor like TheDraw!
  729. >
  730. >- Steve Krulewitz - Fair Lawn, NJ -
  731. >
  732. >
  733. >From: dclemans@mentorg.com (Dave Clemans @ APD x1292)
  734. >Newsgroups: comp.sys.amiga.emulations
  735. >Subject: Re: Emulator Mechanics (sorry long post)
  736. >Message-ID: <1991Mar6.212548.9641@mentorg.com>
  737. >Date: 6 Mar 91 21:25:48 GMT
  738. >References: <4992@mindlink.UUCP>
  739. >Organization: Mentor Graphics Corporation
  740. >
  741. >On the "compiled" approach to emulation:
  742. >
  743. >There definitely has been previous work in this area.  The
  744. >system I've heard of was an internal tool that was used experimentally
  745. >to port some games from the Intel world to the 68K world.
  746. >
  747. >To use it, you basically had to develop enough information to
  748. >get a clean disassembly of the Intel code; i.e., so that you "knew"
  749. >where all the code and data was in the source object file.
  750. >That then was used to drive the tool that produced the "compiled"
  751. >68K file.  After that was done you had to go over the output
  752. >for correctness, system dependencies, etc.; it was not intended 
  753. >as a turn key system.
  754. >
  755. >...
  756. >
  757. >As a side issue, to bring over some of the bigger DOS packages
  758. >you'll have to worry about more than just converting a single
  759. >file.  You have to find and convert all of their code overlay files....
  760. >
  761. >dgc
  762. >
  763. >
  764. >From: ifarqhar@sunb.mqcc.mq.oz.au (Ian Farquhar)
  765. >Newsgroups: comp.sys.amiga.emulations
  766. >Subject: Re: Emulator Mechanics (sorry long post)
  767. >Message-ID: <1312@macuni.mqcc.mq.oz>
  768. >Date: 8 Mar 91 03:51:21 GMT
  769. >References: <4992@mindlink.UUCP> <1991Mar6.212548.9641@mentorg.com>
  770. >Organization: Macquarie University, Sydney, Australia.
  771. >
  772. >In article <1991Mar6.212548.9641@mentorg.com> dclemans@mentorg.com (Dave Clemans @ APD x1292) writes:
  773. >>To use it, you basically had to develop enough information to
  774. >>get a clean disassembly of the Intel code; i.e., so that you "knew"
  775. >>where all the code and data was in the source object file.
  776. >>That then was used to drive the tool that produced the "compiled"
  777. >>68K file.  After that was done you had to go over the output
  778. >>for correctness, system dependencies, etc.; it was not intended 
  779. >>as a turn key system.
  780. >
  781. >Well, on a 6502, it is practically impossible to truly decide what is
  782. >code and what is data.  Let's imagine that you are (as I have done),
  783. >writing a 6502 disassembler.  For every byte you store a status thst
  784. >says DATA, OPCODE, ARGUMENT, and also non-exclusive flags (eg.
  785. >BRANCHENTERSHERE, BRANCHISNOTHERE.)  Initially, all bytes are set to DATA,
  786. >and BRANCHISNOTHERE.
  787. >
  788. >Write a recursive procedure that starts at the program entry point, and
  789. >goes through the code.  For every byte read as an opcode, tag it as an
  790. >OPCODE, and the bytes following as ARGUMENT.  If you get to a branch
  791. >instruction, recursively call the new branch point, and continue
  792. >processing that until you hit a byte that has already been processed
  793. >(ie. not tagged DATA.)  You should continue until the whole procedure
  794. >exits, then run the same thing on the RESET, INT and NMI vectors.  Calls
  795. >are treated the same way as branches, except that the routine exits to a
  796. >higher level invocation when it hits a RET or RTI.
  797. >
  798. >Now, you should have all the data tagged as either program (OPCODE and
  799. >ARGUMENT), or DATA.  Right?  Wrong.  Why?  Because the 6502 has no
  800. >branch always instruction, and your program may continue past what
  801. >appears to be a conditional branch, into data, and screw everything up
  802. >completely.
  803. >
  804. >I experimented with using a two pass approach to this problem.  First,
  805. >the program was scanned sequentially, treating every byte as an opcode,
  806. >and tagging every point referenced by some branch, call, jump or vector.
  807. >Then, when a branch was found during the second recursive pass, the
  808. >program would backtrack and examine every last opcode till it hit a
  809. >branch in point (after which no assumptions could be made), to see if
  810. >the flags were left in a deterministic state.  At this point I lost
  811. >interest in the whole idea.
  812. >
  813. >Anyway, on the 6502 and anything without a BRA or equivalent, the
  814. >problem of automatically determining what is data and what is code is
  815. >extremely difficult.
  816. >
  817. >However, on the 68K, this approach is probably quite profitable.  Why?
  818. >Because there is enough correspondence between the 6502 and 68K
  819. >instructions sets (both having the same ancestor, the 6800) to mean that
  820. >the compilation process is reasonably simple.
  821. >
  822. >Simulating the hardware is still a problem, and I'll have to give that
  823. >one some thought...  I still tend to favor the idea that I presented in
  824. >a previous article, carrying around a compiled image (of code only), and
  825. >uncompiled data with labels to the compiled code and handlers for the
  826. >I/O locations.
  827. >
  828. >--
  829. >Ian Farquhar                      Phone : + 61 2 805-9400
  830. >Office of Computing Services      Fax   : + 61 2 805-7433
  831. >Macquarie University  NSW  2109   Also  : + 61 2 805-7420
  832. >Australia                         EMail : ifarqhar@suna.mqcc.mq.oz.au
  833. >
  834. >
  835. >From: drysdale@cbmvax.commodore.com (Scott Drysdale)
  836. >Newsgroups: comp.sys.amiga.emulations
  837. >Subject: Re: Emulator Mechanics (sorry long post)
  838. >Message-ID: <19614@cbmvax.commodore.com>
  839. >Date: 7 Mar 91 20:15:07 GMT
  840. >References: <4992@mindlink.UUCP> <stevek.3380@amiglynx.UUCP>
  841. >Reply-To: drysdale@cbmvax.commodore.com (Scott Drysdale)
  842. >Organization: Commodore, West Chester, PA
  843. >
  844. >In article <stevek.3380@amiglynx.UUCP> stevek@amiglynx.UUCP (Steve K) writes:
  845. >>The idea of a "translator compiler" is very interesting.  I could have sworn I
  846. >>saw some talk about a IBM->Atari ST translator that exists on FidoNET.  But
  847. >>why is everyone talking about IBM?  Why not Macintosh programs?  Though I am
  848. >
  849. >IBM to Atari ST ports should be relatively simple.  the ST essentially runs
  850. >CP/M 68K with several extensions, much like ms-dos.  so low level calls
  851. >will pretty much translate directly, and you only have to worry about access
  852. >to hardware.
  853. >
  854. >>- Steve Krulewitz - Fair Lawn, NJ -
  855. >
  856. >  --Scotty
  857. >-- 
  858. >=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  859. >Scott Drysdale           Software Engineer
  860. >Commodore Amiga Inc.     UUCP {allegra|burdvax|rutgers|ihnp4}!cbmvax!drysdale
  861. >                 PHONE - yes.
  862. >"Have you hugged your hog today?"
  863. >=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  864. >
  865. >
  866. >From: cg@ami-cg.UUCP (Chris Gray)
  867. >Newsgroups: comp.sys.amiga.emulations
  868. >Subject: Re:  Emulator Mechanics (sorry long post)
  869. >Message-ID: <cg.7040@ami-cg.UUCP>
  870. >Date: 9 Mar 91 08:11:28 GMT
  871. >References: <4992@mindlink.UUCP> <1991Mar6.212548.9641@mentorg.com> <1312@macuni.mqcc.mq.oz>
  872. >Organization: Not an Organization
  873. >
  874. >In article <1312@macuni.mqcc.mq.oz> ifarqhar@sunb.mqcc.mq.oz.au (Ian Farquhar)
  875. >writes:
  876. >
  877. >[Discussion of using a recursive approach to run through all branches and
  878. >calls to tag bytes as being code/data.]
  879. >
  880. >>However, on the 68K, this approach is probably quite profitable.  Why?
  881. >>Because there is enough correspondence between the 6502 and 68K
  882. >>instructions sets (both having the same ancestor, the 6800) to mean that
  883. >>the compilation process is reasonably simple.
  884. >
  885. >My Amiga disassembler, Dis (which is available somewhere or other; I believe
  886. >I sent it out last year some time) does this. The thing it has troubles
  887. >with are things like function variables. In writing disk-resident libraries,
  888. >a lot of that is done, so it doesn't work too well on such things. It has
  889. >special code to handle case/switch statements generated by several compilers,
  890. >but an assembler program doing a branch table will usually mess it up. I
  891. >don't think there is much hope of an automated translater. Perhaps some
  892. >kind of interactive one, so that the user can aid in identifying what is
  893. >code and what is data.
  894. >
  895. >--
  896. >Chris Gray   alberta!ami-cg!cg     or   cg%ami-cg@scapa.cs.UAlberta.CA
  897. >
  898. >
  899. >From: finkel@TAURUS.BITNET
  900. >Newsgroups: comp.sys.amiga.emulations
  901. >Subject: Re: Emulator Mechanics (sorry long post)
  902. >Message-ID: <2406@taurus.BITNET>
  903. >Date: 9 Mar 91 21:26:03 GMT
  904. >References: <4992@mindlink.UUCP> <1991Mar6.004247.8964@cpsc.ucalgary.ca>
  905. >Reply-To: finkel%math.tau.ac.il@CUNYVM.CUNY.EDU (Udi Finkelstein)
  906. >Organization: Tel-Aviv Univesity Math and CS school, Israel
  907. >
  908. >as someone who wrote a 8085 emulator/disassembler/monitor for the C64
  909. >I would like to contribute my on thoughts on the subject.
  910. >
  911. >I have toyed the idea of writing an IBM PC program translator that
  912. >would take an IBM program and 'translate' it to run on the amiga, but
  913. >after careful examination of the idea I decided to drop it for a few
  914. >reasons:
  915. >
  916. >1. There is no way to find out at compile time which memory references
  917. >are accessing special RAM areas such as the text/graphics video screen.
  918. >
  919. >2. self modifying code breaks such schemes easily
  920. >
  921. >3. code/data seperation can be tough. for example, it's very hard to detect
  922. >if memory block contains code ( translate it) , data ( don't touch it)
  923. >or worse - near or far pointers.
  924. >
  925. >(2) is rare, but (1) and (3) are common, so I guess many programs will
  926. >break.
  927. >
  928. >
  929. >even commercial systems claiming the ability to 'compile' PC binaries
  930. >into UNIX programs such as XDOS (anyone heard of them lately??) aren't
  931. >automatic.
  932. >
  933. >Instead, I decided to try concentating on the 'usual' type of emulators
  934. >such as IBeM and Transofrmer.
  935. >
  936. >What caught my attention is that a large portion of the time an emulator
  937. >is spending while emulating an instruction is to check whether a memory
  938. >read/write accesses video memory. every address being written to memory must
  939. >be checked whether it lies in the $BXXXXX range, and if it does, it should
  940. >be written to the screen.
  941. >
  942. >What I really wanted to do if I had an MMU based machine is to write an
  943. >emulator that will use the MMU to track such memory accesses. The emulator's
  944. >memory will be referenced without translation, but every address in the range
  945. >where the video memory is located will be caught by the MMU and special code
  946. >will be run to handle it. This would speed things up.
  947. >
  948. >Udi
  949. >
  950. >
  951. >From: ifarqhar@sunb.mqcc.mq.oz.au (Ian Farquhar)
  952. >Newsgroups: comp.sys.amiga.emulations
  953. >Subject: Re: Emulator Mechanics (sorry long post)
  954. >Message-ID: <1323@macuni.mqcc.mq.oz>
  955. >Date: 10 Mar 91 03:33:49 GMT
  956. >References: <4992@mindlink.UUCP> <1991Mar6.004247.8964@cpsc.ucalgary.ca> <2406@taurus.BITNET>
  957. >Organization: Macquarie University, Sydney, Australia.
  958. >
  959. >In article <2406@taurus.BITNET> finkel%math.tau.ac.il@CUNYVM.CUNY.EDU (Udi Finkelstein) writes:
  960. >>as someone who wrote a 8085 emulator/disassembler/monitor for the C64
  961. >>I would like to contribute my on thoughts on the subject.
  962. >
  963. >A 8085 toolkit on a C64?  The mind boggles.  Why?! :-)
  964. >
  965. >>2. self modifying code breaks such schemes easily
  966. >
  967. >I have yet to see a piece of self-modifying code on an IBM PC, and it
  968. >will cause problems on the many 386DX and 486 systems that have instruction 
  969. >caches without write-through.
  970. >
  971. >>What I really wanted to do if I had an MMU based machine is to write an
  972. >>emulator that will use the MMU to track such memory accesses. The emulator's
  973. >>memory will be referenced without translation, but every address in the range
  974. >>where the video memory is located will be caught by the MMU and special code
  975. >>will be run to handle it. This would speed things up.
  976. >
  977. >Check my article on using lots of memory to very quickly tag this
  978. >occurence on non-PMMU systems, by vectoring every location to a
  979. >handler.
  980. >
  981. >--
  982. >Ian Farquhar                      Phone : + 61 2 805-9400
  983. >Office of Computing Services      Fax   : + 61 2 805-7433
  984. >Macquarie University  NSW  2109   Also  : + 61 2 805-7420
  985. >Australia                         EMail : ifarqhar@suna.mqcc.mq.oz.au
  986. >
  987. >
  988. >From: ecarroll@maths.tcd.ie (Eddy Carroll)
  989. >Newsgroups: comp.sys.amiga.emulations
  990. >Subject: Re: Emulator Mechanics (sorry long post)
  991. >Message-ID: <1991Mar9.225807.26560@maths.tcd.ie>
  992. >Date: 9 Mar 91 22:58:07 GMT
  993. >References: <4992@mindlink.UUCP>
  994. >Organization: Dept. of Maths, Trinity College, Dublin, Ireland.
  995. >
  996. >In article <4992@mindlink.UUCP> Chris_Johnsen@mindlink.UUCP (Chris Johnsen)
  997. >writes:
  998. >>
  999. >> [ Interesting discussion about a new sort of PC emulator ]
  1000. >
  1001. >My final year project for college last year tackled exactly this problem, i.e.
  1002. >given a PC executable file, produce an Amiga executable file which will
  1003. >perform the same function. The end result worked, in a limited sort of way;
  1004. >it was fairly easy for it to be misled by any program of reasonable size.
  1005. >
  1006. >There are a few tricky problems that must be handled if you want to achieve
  1007. >a good degree of success:
  1008. >
  1009. >  o  Given the instruction MOV reg,VAL do you treat VAL as a constant value
  1010. >     or as an offset to an address in one of the PC's segments? The 8086
  1011. >     uses the same opcode for both cases, and it is far from easy to work out
  1012. >     which meaning to use. It's important to get it right because in the
  1013. >     translated program, all the constant values should remain the same
  1014. >     but address offsets will be different.
  1015. >
  1016. >  o  How do you handle jump tables? Most compilers implement switch/case
  1017. >     statements with jump tables, but it is not always clear what the bounds
  1018. >     of such tables are.
  1019. >
  1020. >  o  On the PC, byte values can be pushed onto the stack as byte values. On
  1021. >     the 68000, you can only push word values. You can see why this might be
  1022. >     a problem if you consider a function accessing a parameter at an offset
  1023. >     of 8 from the stack pointer. If the preceding parameters were four words,
  1024. >     then this is okay. If they were two words and four bytes however, then
  1025. >     the offset on the 68000 needs to be 12 to access the same data.
  1026. >
  1027. >  o  Given that most major PC programs access the PC's video RAM directly
  1028. >     for speed reasons, such accesses must be mapped to act equivalently
  1029. >     on the Amiga. The problem is that the usual method for doing this is to
  1030. >     set a segment register to point to the video RAM and then to access the
  1031. >     RAM through this. The same register is likely used for for all sorts of
  1032. >     other data access elsewhere in the program.
  1033. >
  1034. >Of course, there are other problems too. The main thing I discovered during
  1035. >the course of the project was that it's a lot harder to do translation than
  1036. >it might first seem.
  1037. >
  1038. >My program performs the translation in two passes. The first pass traces out
  1039. >all possible paths through the PC code, building up a symbol table and
  1040. >separating code from data as it goes. The second pass then walks sequentially
  1041. >through the code, producing 68K assembly source code for each line. Certain
  1042. >instructions generate macros instead of opcodes, which are later expanded
  1043. >to calls to library functions. The resulting source is fed into Charlie's A68K
  1044. >and then linked with a library that sets up a PC-like environment (custom
  1045. >screen, ibm.font etc.) and handles the INT calls. The result is a standalone
  1046. >executable file.
  1047. >
  1048. >If anyone else has a shot at it, I'd be interested in seeing how it turns out.
  1049. >Perhaps a more practical route to take is to produce a translator that can
  1050. >take reasonably well written PC source code (say, C or assembly) and
  1051. >automatically convert it to Amiga source code. If such a tool existed and
  1052. >worked well, it might encourage more companies to port their products to the
  1053. >Amiga.
  1054. >
  1055. >In the meantime, back to IBem.
  1056. >--
  1057. >Eddy Carroll           ----* Genuine MUD Wizard  | "You haven't lived until
  1058. >ADSPnet:  cbmuk!cbmuka!quartz!ecarroll           |    you've died in MUD!"
  1059. >Internet: ecarroll@maths.tcd.ie                  |   -- Richard Bartle
  1060. >
  1061. >
  1062. >From: srwmpnm@windy.dsir.govt.nz
  1063. >Newsgroups: comp.sys.amiga.emulations
  1064. >Subject: 8 methods to emulate a Z80
  1065. >Summary: 8 methods to emulate a Z80 on a 68000
  1066. >Keywords: Z80 68000 Amiga
  1067. >Message-ID: <18847.27d80900@windy.dsir.govt.nz>
  1068. >Date: 8 Mar 91 21:58:24 GMT
  1069. >Organization: DSIR, Wellington, New Zealand
  1070. >
  1071. >Ok folks, here are 8 methods for doing z80 emulation on a 68000, in software.
  1072. >(Well, 8 methods to get to decode a z80 instruction and get to the right
  1073. >emulation routine, anyway.)
  1074. >
  1075. >Trade-offs are speed, space and cleanliness.  They all fall short of
  1076. >"compiling and optimising", but most of these methods will speed up most
  1077. >existing emulators.  As you might expect, the largest and dirtiest code is
  1078. >usually the fastest (and least portable).  The same methods should work with
  1079. >emulation of 6502, PDP-11 and any other 16-bit processors.
  1080. >
  1081. >In all methods, I assume there is a 64kb block of memory representing the z80's
  1082. >address space, allocated by AllocMem (say), and pointed to by "z80ram".
  1083. >
  1084. >-------------------------------------------------------------------------------
  1085. >Method 1: The "standard" method:
  1086. >
  1087. >I call this method "standard" because it's used in both of the CP/M z80
  1088. >emulators I know about.  The general idea is to decode the current instruction
  1089. >and jump to the appropriate emulation routine via a vector table.  That is,
  1090. >like a CASE statement with 256 selections.  The code is clean and re-entrant.
  1091. >
  1092. >; Setup
  1093. >    move.l    z80ram,a2        ;   load pseudopc
  1094. >    lea.l    optabl(pc),a1        ;   a1 always points to optabl
  1095. >    lea.l    mloop(pc),a3        ;   a3 always points to mloop
  1096. >
  1097. >; Main loop (decode) starts here
  1098. >mloop:    moveq    #0,d0            ; 4 Execute appropriate subroutine.
  1099. >    move.b    (a2)+,d0        ; 8 Grab the next opcode and inc pc.
  1100. >    asl    #2,d0            ;10 D0 high word is still zero!
  1101. >    move.l    0(a1,d0.w),a0        ;18 Get address of routine from table
  1102. >    jmp    (a0)            ; 8 Do the subroutine.
  1103. >                    ;48 total cycles to decode
  1104. >    even
  1105. >optabl:    dc.l    nop00,lxib,staxb,inxb,inrb,dcrb,mvib,rlc
  1106. >    dc.l    ...
  1107. >
  1108. >Each z80 instruction emulation routine ends with:
  1109. >
  1110. >    jmp    (a3)
  1111. >
  1112. >-------------------------------------------------------------------------------
  1113. >Method 2: The "position-independent" method:
  1114. >
  1115. >This is slightly quicker, the executable is more than 1500 bytes smaller, and
  1116. >you get another register to play with in the emulator (a1 in this case).  I
  1117. >currently use this method (or close to it) in my Spectrum emulator.  The code
  1118. >is clean and re-entrant.
  1119. >
  1120. >    move.l    z80ram,a2        ;   load pseudopc
  1121. >    lea.l    mloop(pc),a3        ;   a3 always points to mloop
  1122. >mloop:    moveq    #0,d0            ; 4 clear opcode word
  1123. >    move.b    (a2)+,d0        ; 8 get opcode byte
  1124. >    add.w    d0,d0            ; 4 2 bytes per entry
  1125. >    move.w    optabl(pc,d0.w),d0    ;14 get offset of routine
  1126. >    jmp    optabl(pc,d0.w)        ;14 do instruction
  1127. >                    ;44 total to decode
  1128. >    even
  1129. >optabl:    dc.w    nop00-optabl,lxib-optabl,staxb-optabl,inxb-optabl
  1130. >    dc.w    inrb-optabl,dcrb-optabl,mvib-optabl,rlc-optabl
  1131. >    dc.w    ...
  1132. >
  1133. >Each instruction emulation routine ends with:
  1134. >
  1135. >    jmp    (a3)
  1136. >
  1137. >-------------------------------------------------------------------------------
  1138. >Method 3: The "decode-at-end-of-instruction" method:
  1139. >
  1140. >(There are really 2 methods described here.)  Take either method 1 or method 2.
  1141. >Instead of ending each emulation routine with "jmp (a3)", end each one with a
  1142. >complete copy of the code from mloop to the indirect jmp.  There is no longer
  1143. >a main loop, because each instruction jumps directly to the next one.
  1144. >
  1145. >This method is slightly faster, takes maybe twice as much code, is clean, and
  1146. >is re-entrant.  It also saves yet another reserved register, in this case a3.
  1147. >(Personally, I find that a z80 emulator needs as many free registers as you
  1148. >can get your fingers on.)
  1149. >
  1150. >-------------------------------------------------------------------------------
  1151. >Method 4: The "threaded jsr's" method:
  1152. >
  1153. >Warning: This method uses self-modifying, non-re-entrant code, and therefore
  1154. >is not recommended.  This code is hazardous to your cache!  (No flames please
  1155. >--- read on).
  1156. >
  1157. >Introduce a 390kb contiguous block of code (called thread) which looks like
  1158. >this:
  1159. >
  1160. >thread:        jsr    patch    ; 0
  1161. >        jsr    patch    ; 1
  1162. >        ...
  1163. >        jsr    patch    ; 65535
  1164. >        jmp    thread
  1165. >
  1166. >That is, there is a jsr instruction for each byte in the z80's address space.
  1167. >This is in addition to z80ram.
  1168. >
  1169. >To start the emulator, you transfer control to "thread".  What the "patch"
  1170. >routine does is to replace the current "jsr patch" with "jsr this_routine",
  1171. >where this_routine is the emulation routine for the corresponding opcode in
  1172. >z80ram.  Then patch jmps to the this_routine to execute the instruction and to
  1173. >return to the next jsr in the thread.  After a while, patch will no longer be
  1174. >called (except by z80 self modifying code), and every jsr made will be to
  1175. >emulate a z80 opcode directly.
  1176. >
  1177. >Whenever a z80 instruction writes to RAM, it patches the corresponding
  1178. >"jsr this_routine" with "jsr patch".  As a variation, it could patch
  1179. >"jsr this_routine" with "jsr new_routine", but that would probably be slower
  1180. >in general.
  1181. >
  1182. >Advantage:
  1183. >
  1184. >It would be faster than methods 1 to 3, --- I think, --- especially in the
  1185. >Spectrum emulator, which has to do a lot of work with every write to RAM to
  1186. >check for ROM and video RAM anyway.  The main reason for the extra speed is
  1187. >that it no longer has to decode the opcode on every instruction.  There are
  1188. >the extra overheads of call and return though, and extra work to do on every
  1189. >RAM write.
  1190. >
  1191. >Disadvantages:
  1192. >
  1193. >1: The code breaks C='s self-modifying code law.  To run on Amiga's with
  1194. >caches, it would have to either disable the caches or update them manually
  1195. >after every patch.  The code is extremely dirty, not re-entrant, and
  1196. >definitely not recommended;
  1197. >
  1198. >2: You need 390k contiguous memory (plus another 64k somewhere else, plus
  1199. >whatever else you need for video).
  1200. >
  1201. >Other characteristics:
  1202. >
  1203. >Code would run slowly the first time round the loop, then speed up.
  1204. >
  1205. >--------------------------------------------------------------------------
  1206. >Method 5: The "replicated code" method.
  1207. >
  1208. >Warning: This also uses self-modifying, non-re-entrant code and is therefore
  1209. >not recommended.
  1210. >
  1211. >Thread consists of 65536 blocks of code, each long enough to emulate the
  1212. >trickiest z80 instruction.  Initially it contains 65536 copies of patch.  (You
  1213. >will need A LOT of contiguous memory.)  What patch does is to actually copy
  1214. >the code for the opcode over itself, then transfer control to the beginning of
  1215. >itself.  (Tricky, but it can be done.)  Every emulation routine finishes with
  1216. >a "bra.s next_instr" so they are all really the same length.  That saves the
  1217. >call and return overhead.
  1218. >
  1219. >If an emulation routine is too long, then just use a jmp to somewhere.  In
  1220. >practice, you would probably start with:
  1221. >
  1222. >    jsr    patch
  1223. >    bra.s    next_instr
  1224. >
  1225. >in every slot, rather than a complete copy of patch.  Z80 RAM writes would
  1226. >copy the above code to the corresponding slot, if necessary, rather than
  1227. >copying the whole patch routine.
  1228. >
  1229. >Short of "compiling and optimising", this is the fastest method I can think of,
  1230. >but it is incredibly space-wasting, self-modifying, extremely dirty, and
  1231. >definitely not recommended.
  1232. >
  1233. >--------------------------------------------------------------------------
  1234. >Method 6: The "threaded vector table" method:
  1235. >
  1236. >Ok, now to fix the self-modifying code problem.  Take method 4 (threaded jsr's),
  1237. >but use a 262kb vector table in a private data segment, instead of a thread in
  1238. >the code segment.
  1239. >
  1240. >vectors:    dc.l    patch    ; 0
  1241. >        dc.l    patch    ; 1
  1242. >        ...
  1243. >        dc.l    patch    ; 65535
  1244. >        dc.l    jmp_thread
  1245. >
  1246. >The main instruction loop looks like:
  1247. >
  1248. >        lea.l    vectors,a0
  1249. >        lea.l    mloop(pc),a2
  1250. >mloop:        move.l    (a0)+,a1    ;12 cycles
  1251. >        jmp    (a1)        ; 8 cycles
  1252. >
  1253. >and every instruction finishes with "jmp (a2)".  A0 is acting as a "pseudo-pc"
  1254. >into the vector table.  Of course patch performs the same functions as before
  1255. >(except it is no longer self modifying, it just patches a vector).  The vector
  1256. >table still needs to be updated by every write to Z80 RAM.  The code is
  1257. >re-entrant provided each task has a separate copy of the vector table.
  1258. >
  1259. >--------------------------------------------------------------------------
  1260. >Method 7: The "position-independent threaded vector table" method:
  1261. >
  1262. >Same as method 6, except that now the private data segment is:
  1263. >
  1264. >thread:        dc.w    patch-base    ; 0
  1265. >        dc.w    patch-base    ; 1
  1266. >        ...
  1267. >        dc.w    patch-base    ; 65535
  1268. >        dc.w    jmp_thread-base
  1269. >
  1270. >and the main loop is:
  1271. >
  1272. >        lea.l    thread,a0
  1273. >        lea.l    mloop(pc),a1
  1274. >mloop:        move.w    (a0)+,d0    ; 8 cycles
  1275. >        jmp    base(pc,d0.w)    ;14 cycles
  1276. >base:
  1277. >patch:        ...
  1278. >op00:        ...
  1279. >op01:        ...
  1280. >jmp_thread:    ...
  1281. >
  1282. >Now it is position-independent, only 128kb contiguous memory, the executable
  1283. >is 1500 bytes smaller, and it is slightly slower (only by 2 cycles per z80
  1284. >instruction though).  The code is re-entrant provided each task has a separate
  1285. >copy of the vector table.
  1286. >
  1287. >--------------------------------------------------------------------------
  1288. >Method 8: The "decode-at-end-of-instruction threaded vector table" method:
  1289. >
  1290. >Same as method 6 except that every opcode emulation routine finishes with:
  1291. >
  1292. >        move.l    (a0)+,a1
  1293. >        jmp    (a1)
  1294. >
  1295. >instead of "jmp (a2)".  Now isn't that faster?  And it saves a2 for more
  1296. >important things.
  1297. >
  1298. >Unfortunately you can't do exactly the same thing to method 7 unless you can
  1299. >write a complete z80 emulator in 256 bytes  8-) .  But you could take method 7
  1300. >and end each emulation routine with:
  1301. >
  1302. >mloop:        move.w    (a0)+,d0
  1303. >        lea.l    base(pc),a1
  1304. >        jmp    0(a1,d0.w)
  1305. >
  1306. >instead.  The code is re-entrant provided each task has a separate copy of
  1307. >the vector table.
  1308. >
  1309. >--------------------------------------------------------------------------
  1310. >Personally I'm considering using one of the methods 6, 7 or 8 in the next
  1311. >version of the Spectrum emulator (probably method 8)  (That is, if I ever get
  1312. >enough spare time without more interesting things to do.)  I'll probably make
  1313. >the source public domain.  That will use more Amiga RAM, but should go faster
  1314. >(I hope).  Any guesses as to which method will be the fastest, and still fit
  1315. >comfortably in a 512k machine?
  1316. >
  1317. >Unfortunately I don't think any of the methods (except the first 3) are
  1318. >suitable for an 8088 emulator because of the huge memory requirements.
  1319. >
  1320. >I'm interested in any ideas anyone might have along these lines.  The
  1321. >discussion of "compiling and optimising" is very interesting, but I don't see
  1322. >how the details would work.  In particular, how do you cope with self-modifying
  1323. >code, code loaders, overlays etc?
  1324. >
  1325. >
  1326. >Peter McGavin.  (srwmpnm@wnv.dsir.govt.nz)
  1327. >
  1328. >Disclaimer:  I haven't tested any of the above ideas (except 1 and 2).  If you
  1329. >see any bugs, point them out.
  1330. >
  1331. >
  1332. >From: daveh@cbmvax.commodore.com (Dave Haynie)
  1333. >Newsgroups: comp.sys.amiga.emulations
  1334. >Subject: Re: Emulator Mechanics
  1335. >Message-ID: <19749@cbmvax.commodore.com>
  1336. >Date: 11 Mar 91 20:25:55 GMT
  1337. >References: <1991Mar7.093149.18707@mintaka.lcs.mit.edu>
  1338. >Reply-To: daveh@cbmvax.commodore.com (Dave Haynie)
  1339. >Organization: Commodore, West Chester, PA
  1340. >
  1341. >In article <1991Mar7.093149.18707@mintaka.lcs.mit.edu> rjc@geech.ai.mit.edu (Ray Cromwell) writes:
  1342. >
  1343. >> Why doesn't AMAX multitask? It should be EASY to run multiple copies
  1344. >>of the Mac ROM as a task (since its a rom, its mostly pure code, except
  1345. >>each task would need to alter where the ROM things the system memory is.)
  1346. >
  1347. >Without massive patching of the Mac ROM, I don't think so.  The same reason
  1348. >you don't have more sophisticated multitasking on the Mac itself.  You can't
  1349. >run multiple copies of the same Mac ROM code, since the code is not reentrant
  1350. >like Amiga code.  Without some clever MMU tricks, I don't think you could 
  1351. >easily relocate things such that several different copies of the ROM code
  1352. >could coexist.  At least, you would expect Apple to have considered any of
  1353. >the more mundane tricks available on any 680x0 system.  Apple does seem to have]
  1354. >decent technical folks, I doubt they missed any easy tricks...
  1355. >
  1356. >Via MMU, you could certainly get further.  Multifinder didn't do that, since
  1357. >it would have required an MMU based system for any multitasking.  I'm surprised
  1358. >Apple didn't work out anything like that for their MacOS-under-UNIX, though.
  1359. >
  1360. >>Does anyone know if IBM code uses self modifying code, or
  1361. >>jump tables? 
  1362. >
  1363. >Self modifying code and other uglies are very prevalent in MS-DOS code.  That's
  1364. >the main reason Intel went for a unified code/data cache for the 80486. 
  1365. >Separate I and D caches with internal Harvard architecture (like the '030 and
  1366. >'040) can yield a much faster system, all else being equal.  But that extra
  1367. >performance would not have been worth tossing out what was apparently a large
  1368. >portion of the MS-DOS programs out there.
  1369. >
  1370. >MS-DOS programs, more and more often, think they're the operating system.  
  1371. >Every program is responsible for managing differences between CPUs, managing
  1372. >memory (beyond the 8088 model), graphics, etc.  
  1373. >
  1374. >-- 
  1375. >Dave Haynie Commodore-Amiga (Amiga 3000) "The Crew That Never Rests"
  1376. >   {uunet|pyramid|rutgers}!cbmvax!daveh      PLINK: hazy     BIX: hazy
  1377. >    "What works for me might work for you"    -Jimmy Buffett
  1378. >
  1379. >
  1380. >From: srwmpnm@windy.dsir.govt.nz
  1381. >Newsgroups: comp.sys.amiga.emulations
  1382. >Subject: Re: 8 methods to emulate a Z80
  1383. >Summary: 4 more methods to emulate a Z80
  1384. >Keywords: Z80 68000 Amiga Emulator
  1385. >Message-ID: <18850.27dbf20e@windy.dsir.govt.nz>
  1386. >Date: 11 Mar 91 21:09:34 GMT
  1387. >References: <18847.27d80900@windy.dsir.govt.nz>
  1388. >Organization: DSIR, Wellington, New Zealand
  1389. >
  1390. >Here are 4 more methods for doing z80 instruction decoding on a 68000.  These
  1391. >methods are all based on fixed-size instruction emulation routines.
  1392. >
  1393. >There are some general comments, on handling multiple-byte opcodes, writing to
  1394. >hardware registers, and flag handling, at the end of this post.
  1395. >
  1396. >These 4 methods are faster than methods 1..3 (see previous post), they do not
  1397. >have the RAM-write overheads of methods 4..8, and they do not require tables of
  1398. >opcode routine address offsets.  Some of these methods might be superior to all
  1399. >previous methods.
  1400. >
  1401. >In all these methods, each z80 instruction emulation routine is fixed at 256
  1402. >bytes in length.  (See earlier post by Ian Farquhar.)  They are coded in the
  1403. >sequence op_80, op_81, ... op_ff, op_00, op_01, ... op_7f.  So there is exactly
  1404. >64k of code.  If a routine is shorter than 256 bytes, the extra space is
  1405. >wasted.  If a routine is longer than 256 bytes then it will need a jsr to
  1406. >somewhere.
  1407. >
  1408. >-------------------------------------------------------------------------------
  1409. >Method 9: The "self-modifying fixed-size routine" method:
  1410. >
  1411. >Warning: Self modifying code follows.
  1412. >
  1413. >This method is almost the same as in Ian Farquhar's earlier c.s.a.e post.
  1414. >
  1415. >setup:    lea.l    op_00(pc),a1        ; a1 always points to op_00
  1416. >    move.l    z80ram,a2        ; load pseudopc
  1417. >
  1418. >mloop:    move.b    (a2)+,1$-op_00+4(a1)    ;16 patch $ff in jmp instruction, inc pc
  1419. >1$:    jmp    $ff00(a1)        ;10 Jump to routine.
  1420. >                    ;26 total cycles
  1421. >
  1422. >Every instruction emulation routine ends with a copy of mloop, rather than a
  1423. >jump back to mloop.
  1424. >
  1425. >The move.b patches the high byte of the offset in the second instruction, so
  1426. >the jump goes to the right routine.  (1$-op_00+4 might be 1$-op_00+2 --- I
  1427. >haven't checked.)
  1428. >
  1429. >The code is extremely fast (maybe the fastest yet), but it does not work on
  1430. >Amigas with memory caches.
  1431. >
  1432. >I thought of making it even faster by permanently setting a4 to the address of
  1433. >the byte to patch, and then using:
  1434. >
  1435. >mloop:    move.b    (a2)+,(a4)        ;12 Patch $ff in jmp instruction, inc pc
  1436. >    jmp    $ff00(a1)        ;10 Jump to routine.
  1437. >                    ;22 total cycles
  1438. >
  1439. >but you run out of reserved registers if there are lots of copies of mloop.
  1440. >(I.e, you can't use the decode-at-end-of-instruction technique.)  Using
  1441. >"jmp (a3)" at the end of every routine makes it slower.
  1442. >
  1443. >-------------------------------------------------------------------------------
  1444. >Method 10: The "standard fixed-size routine" method:
  1445. >
  1446. >Now we eliminate self-modifying code.
  1447. >
  1448. >The main loop is coded into the wasted space at the end of op_ff (so that it is
  1449. >within 128 bytes of op_00 --- Remember that op_00 is in the middle of the 64k
  1450. >code block).
  1451. >
  1452. >setup:    subq.l    #2,sp            ; make room for scratch area on stack
  1453. >    clr.w    (sp)            ; low byte of scratch area is always 0
  1454. >    lea.l    mloop(pc),a3        ; a3 always points to mloop
  1455. >    move.l    z80ram,a2        ; load pseudopc
  1456. >
  1457. >mloop:    move.b    (a2)+,(sp)        ;12 Opcode to scratch high byte, inc pc
  1458. >    move.w    (sp),d0            ; 8 high byte is opcode, low byte is 0
  1459. >    jmp    op_00(pc,d0.w)        ;14 Jump to routine.
  1460. >
  1461. >Each z80 instruction emulation routine ends with:
  1462. >
  1463. >    jmp    (a3)            ;10
  1464. >                    ;44 total cycles
  1465. >
  1466. >Unfortunately it's quite a bit slower.  We can do better...
  1467. >
  1468. >-------------------------------------------------------------------------------
  1469. >Method 11: The "decode-at-end-of-instruction fixed-size routine" method:
  1470. >
  1471. >Register a3 always points to op_00 instead of to mloop, and we have:
  1472. >
  1473. >setup:    subq.l    #2,sp            ; make room for scratch area on stack
  1474. >    clr.w    (sp)            ; low byte of scratch area is always 0
  1475. >    lea.l    op_00(pc),a3        ; a3 always points to op_00
  1476. >    move.l    z80ram,a2        ; load pseudopc
  1477. >
  1478. >mloop:    move.b    (a2)+,(sp)        ;12 Opcode to scratch high byte, inc pc
  1479. >    move.w    (sp),d0            ; 8 high byte is opcode, low byte is 0
  1480. >    jmp    0(a3,d0.w)        ;14 Jump to routine.
  1481. >                    ;34 total cycles
  1482. >
  1483. >Each z80 instruction emulation routine ends with a copy of the decode routine.
  1484. >This is faster than method 10, and mloop can be coded anywhere.
  1485. >
  1486. >Can we avoid using scratch memory and still be as fast?  Think about how you
  1487. >might do this before you read on.  An 8-bit shift of register d0 avoids using
  1488. >scratch memory, but is slower (on a plain 68000).  The next method shows how to
  1489. >make the decode faster and avoid using scratch memory, but it (possibly)
  1490. >introduces overhead elsewhere.
  1491. >
  1492. >-------------------------------------------------------------------------------
  1493. >Method 12: The "stretched-address-space fixed-size-routine" method:
  1494. >
  1495. >This method assumes that the z80 address space (z80ram) is stretched to 128k,
  1496. >so that each byte in the z80's address space takes up a word in the Amiga.
  1497. >The low order byte of every word must always be 0.
  1498. >
  1499. >setup:    move.l    z80ram,a2        ; load pseudopc
  1500. >    lea.l    op_00(pc),a3        ; a3 always points to op_00
  1501. >
  1502. >mloop:    move.w    (a2)+,d0        ; 8 Opcode to d0 high byte, inc pc
  1503. >    jmp    0(a3,d0.w)        ;14 Jump to routine.
  1504. >                    ;22 total cycles
  1505. >
  1506. >For best results, every routine ends with the mloop code (decode at end of
  1507. >instruction).  The instruction decode is faster than method 11, but now many
  1508. >instructions will have extra work to do to convert byte z80 addresses to word
  1509. >amiga addresses.  Still, this code looks good enough to try.
  1510. >
  1511. >Miscellaneous hint #9: To convert a byte offset to a word offset, use
  1512. >"add.w d0,d0", not "lsl.w #1,d0".
  1513. >
  1514. >Another miscellaneous hint: Maybe there's a use for movep here.
  1515. >
  1516. >You could maintain 2 copies of the z80 address space --- one in 64k and the
  1517. >other in 128k.  Then it's just a simple matter of writing a byte to both places
  1518. >whenever the z80 does a write.  That gets rid of the overhead of converting
  1519. >between offset types on memory reads.
  1520. >
  1521. >But now our method is starting to look like threaded code (method 8) again.
  1522. >The threaded code method uses the 128k block to store the offset to the
  1523. >handling routine, rather than storing the opcode itself.  The overhead in doing
  1524. >a memory write is the same in both methods, and threaded code has other
  1525. >advantages (like not having to pad code to 256 bytes, multiple-byte opcodes
  1526. >handled better).  So we're back to threaded code again.
  1527. >
  1528. >-------------------------------------------------------------------------------
  1529. >A note on multiple-byte opcode instructions:
  1530. >
  1531. >The z80 uses these.  They are prefixed with $cb, $dd, $ed and $fd.  There are
  1532. >also $ddcb and $fdcb prefixed instructions.
  1533. >
  1534. >For fixed-size methods, (methods 9..12), the fastest way to cope with long
  1535. >instructions is to use more tables.  But that means multiplying the number of
  1536. >tables by 5 or 7.  64k of code has just jumped to 320k or 448k.  That's no good
  1537. >on a small machine.  Also, if you reserve a register to point to op_00 in each
  1538. >table, that's 5 or 7 registers gone.  Oops.
  1539. >
  1540. >-------------------------------------------------------------------------------
  1541. >Some notes on threaded code:
  1542. >
  1543. >I spent last evening trying threaded code (method 8) in the Spectrum emulator. 
  1544. >(See previous post.)  I got a 20..40% speed improvement over the position-
  1545. >independent standard method (256-way CASE statement).  It's still several times
  1546. >slower than a real Spectrum, unfortunately.  There is some slow code in places
  1547. >where there wasn't before, so there is scope for more improvement.
  1548. >Unfortunately I introduced some bugs during the systematic changes, and they
  1549. >are proving hard to track down.  Everything in the Spectrum ROM seems to work
  1550. >ok.  Some machine-code programs that worked before have stopped working.
  1551. >
  1552. >Threaded code is extremely fast for multiple-opcode instructions.  Control is
  1553. >vectored directly to the right routine first time, without having to decode
  1554. >multiple tables.  A problem with this is that if a Spectrum program overwrites
  1555. >the second opcode of a multiple-byte instruction ($cb, $dd, $ed, $fd) without
  1556. >writing to the first byte, then the emulator doesn't cope.
  1557. >
  1558. >-------------------------------------------------------------------------------
  1559. >Note on hardware registers:
  1560. >
  1561. >Handling writes to hardware registers in the Spectrum isn't really very hard.
  1562. >The z80 has a separate IO address space with a separate set of instructions for
  1563. >handling it.  (The same is true of the 8088.)  The only thing to watch for is
  1564. >writing to video RAM.  It is fixed size and at a fixed place, so it takes 2
  1565. >tests.  (There doesn't seem to be a faster way of doing a single bit test ---
  1566. >the video RAM doesn't end on a power-of-2 boundary.)  I have a separate task
  1567. >(sort of) which uses the blitter to keep the screen up-to-date with the video
  1568. >RAM.  So when there is a write to video RAM, the emulator task doesn't have to
  1569. >do much.  It just flags the blitter task "Hey, there's something to update in
  1570. >character row n, when you wake up".  The blitter task doesn't slow the emulator
  1571. >down much, because it's mostly running on another processor, and it sleeps when
  1572. >there's nothing to update.
  1573. >
  1574. >-------------------------------------------------------------------------------
  1575. >Some notes on flag handling:
  1576. >
  1577. >Both of the CP/M emulators I know about spend a lot of time handling z80 flags
  1578. >(condition codes).  After just about every instruction they do a "move sr,d0"
  1579. >or "move ccr,d0" or call GetCC() to get the 68000 flags, then they do a table
  1580. >lookup to translate them to z80 format.  After every logical instruction (not,
  1581. >or, xor etc), a second table lookup is done to set the z80 parity flag.  (The
  1582. >68000 does not have a parity flag.)  These table lookups are slow.  In fact,
  1583. >they often take several times as long as the guts of the instruction itself.
  1584. >
  1585. >Both these table lookups are totally unnecessary!  It's faster to save the
  1586. >flags in 68000 format (in a register).  Routines that test flags simply test
  1587. >the corresponding 68000 flag.  For logical instructions, simply save the parity
  1588. >byte away somewhere, and set another bit in the register to say to use the
  1589. >parity byte and not v flag.  The parity testing instructions (e.g, "jp po,nn")
  1590. >look at that bit, and then test either the v flag or the saved parity byte.
  1591. >The only times you need to translate flags between z80 and 68000 formats are in
  1592. >"push af" and "pop af" instructions.
  1593. >
  1594. >I got a 10..20% speedup in my Spectrum emulator this way.
  1595. >
  1596. >-------------------------------------------------------------------------------
  1597. >I said in my previous post that threaded code for 8088 emulation would use too
  1598. >much memory to be practical.  In fact it would be perfectly practical on an
  1599. >Amiga equipped with 3 Mbytes or more.
  1600. >
  1601. >Peter McGavin.   (srwmpnm@wnv.dsir.govt.nz)
  1602. >
  1603. >
  1604. >From: Chris_Johnsen@mindlink.UUCP (Chris Johnsen)
  1605. >Newsgroups: comp.sys.amiga.emulations
  1606. >Subject: Emulator Mechanics [Transpiler]
  1607. >Message-ID: <5097@mindlink.UUCP>
  1608. >Date: 10 Mar 91 17:54:43 GMT
  1609. >Organization: MIND LINK! - British Columbia, Canada
  1610. >
  1611. >
  1612. >        Thank you all for the valued input into this discussion to date,  both
  1613. >pro and con.  I must say I find it very stimulating.
  1614. >
  1615. >        When discussing this form platform porting translator/compiler  with
  1616. >anyone, I find the need for a short word to use.  In a humble attempt  to coin
  1617. >a descriptive phrase, may I suggest transpiler?
  1618. >Charlie Gibbs (Charlie_Gibbs@mindlink.UUCP) Jean-Noel Moyne (jnmoyne@lbl.gov)
  1619. >Dave Clemans (dclemans@mentorg.com) Dwight Hubbard (uunet.uu.net!easy!lron)
  1620. >Pete Ashdown (pashdown@javelin.es.com) Jyrki Kuoppala (jkp@cs.HUT.FI) confirm
  1621. >that, indeed some research and even program development has been done in this
  1622. >direction.
  1623. >
  1624. >Eddy Carroll (ecarroll@maths.tcd.ie) has written such a transpiler, as a last
  1625. >year project, with some success, some reservations.
  1626. >
  1627. >Chris Gray (cg@ami-cg.UUCP) suggests that the most practicable route to
  1628. >desinging a viable transpiler would be to make it interactive.  BTW Chris, I
  1629. >very much enjoyed your compiler articles in the Amiga Transactor.
  1630. >
  1631. >Ray Cromwell (rjc@pogo.ai.mit.edu) suggested an interesting thought, a sort of
  1632. >Usenetware combined effort for development, he also thinks reasonable execution
  1633. >speed can be achieved.
  1634. >
  1635. >        Those are what I percieve to be the ideas supporting the transpiler
  1636. >concept.  The statements of contrary considerations are more voluminous. These
  1637. >appear to fall into a number of categories.
  1638. >          o  Self-modifying code
  1639. >          o  Separating code from data
  1640. >          o  Determining video access
  1641. >          o  Stack handling
  1642. >          o  Jump table problems
  1643. >          o  Handling overlay segments
  1644. >Ian Farquhar (ifarqhar@sunb.mqcc.mq.oz.au) suggested that a compiled module  be
  1645. >run concurrent with an emulator type section so that in some parallel  way, any
  1646. >references within the source executable, which would also be loaded during
  1647. >runtime of the emulation, could be validated.  It could be argued that this
  1648. >should be placed on the pro side of the ledger, but the incurred overhead
  1649. >during execution would be large.  This function, I had imagined initially,
  1650. >would be carried out during the transpiler phase and not attached to, or
  1651. >burdening, the runtime execution.
  1652. >
  1653. >Brad Pepers (pepers@enme1.ucalgary.ca) Jyrki Kuoppala (jkp@cs.HUT.FI)  Jonathan
  1654. >David Abbey (jonabbey@cs.utexas.edu) were somewhat concerned with
  1655. >self-modifying code.  There were a significant number of voices that dismissed
  1656. >this concern.  Personally, I wouldn't worry about it.  If a particular program
  1657. >used this technique, for whatever reason, I would accept the fact that not all
  1658. >programs can be transpiled.
  1659. >
  1660. >Ian Farquhar (ifarqhar@sunb.mqcc.mq.oz.au) Dave Clemans (dclemans@mentorg.com)
  1661. >Sullivan (Radagast@cup.portal.com) Jean-Noel Moyne (jnmoyne@lbl.gov)  Chris
  1662. >Gray (cg@ami-cg.UUCP) raised concerns about determining code from bytes of the
  1663. >data persuation.  This is what I had thought would be the biggest stumbling
  1664. >block. I hadn't thought there would be so many others. :-)  It's great to hear
  1665. >how how long the road is before you begin the journey.
  1666. >
  1667. >Udi Finkel (finkel@TAURUS.BITNET) Eddy Carroll (ecarroll@maths.tcd.ie) are
  1668. >concerned about determining when the access to memory is video ram.  This, I
  1669. >believe, can be solved, if it is dealt with in the same manner as the code/data
  1670. >resolution.  Please read on.
  1671. >
  1672. >Eddy Carroll (ecarroll@maths.tcd.ie) points out problems with processor stack
  1673. >handling.  How did you resolve this with the transpiler you developed?  It
  1674. >occurs to me that every emulator whether it be a transpiler, or interpretive
  1675. >type must handle the stack properly.  To me, it would be more difficult to
  1676. >approach this problem if I were writting an emulator, as opposed to a
  1677. >transpiler, as less executable size constraints would inhibit producing an
  1678. >intelligent stack handler.  On the 68000 the stack would be handled as
  1679. >word/longword only.  Any bytewise stack functions within the  source executable
  1680. >would be transpiled into wordwise handling.  A problem, but surmountable.
  1681. >
  1682. >Chris Gray (cg@ami-cg.UUCP) Eddy Carroll (ecarroll@maths.tcd.ie) raised the
  1683. >problem of coping with jump or branch tables properly.  Weighty point. This I
  1684. >would describe as a grey area between code and data where it may be actual jump
  1685. >instructions, or a table of label locations, the code to be  emulated
  1686. >calculating an offset into it.  This must be handled in a similar fashion to
  1687. >the code/data recognition problem.
  1688. >
  1689. >Dave Clemans (dclemans@mentorg.com) Kurt Tappe (JKT100@psuvm.psu.edu) Jonathan
  1690. >David Abbey (jonabbey@cs.utexas.edu) were concerned with the  handling of
  1691. >overlay segments.  This bothers me too.  With the Amiga, it  would be possible
  1692. >to either use overlays, in the case of very large programs,  or convert to
  1693. >all-in-one programs by consolidating the overlay hunks.  The  main problem is
  1694. >determining that overlays are employed, loading them and  transpiling.  This is
  1695. >closely related to the code/data determination problem.
  1696. >
  1697. >Dwight Hubbard (uunet.uu.net!easy!lron) suggest that this would work, kind of
  1698. >like DSM.  I have DSM and must say it was a model for me when contemplating
  1699. >this idea.  Chris Gray (cg@ami-cg.UUCP) believes that an  interactive compiler
  1700. >holds some promise.  In one of my initial messages to  Charlie Gibbs I
  1701. >entertained the reality of having to fall back on human  intervention, at the
  1702. >transpiler output source code level.  This would be  determined at runtime,
  1703. >when the emulated program failed to operate correctly  or at all.  A programmer
  1704. >would be required to fix the problem.
  1705. >
  1706. >        What I interpert the huge human parallel processor the net represents,
  1707. >to be saying, is that human intervention must be employed.  My only reservation
  1708. >in this regard is that there are interperative emulators that run a wide
  1709. >variety of software on-the-fly.  To me, this feat is more difficult than a
  1710. >transpiler, which can ruminate over the executable for as long as say a
  1711. >raytracer.
  1712. >
  1713. >        To incorporate the above concepts, and arrive at a workable resolution,
  1714. >what is required is an expert system, probably requiring a resident expert (not
  1715. >software, though brain matter is soft), to resolve the code/data, video access,
  1716. >jump/branch table and overlay problems.  I don't suppose that the average
  1717. >emulator user is prepared to deal with or even understand the problems we have
  1718. >been discussing.  I don't think it would be worthwhile writing a transpiler
  1719. >unless it could be made to work in some standalone fashion.  Feed it the
  1720. >source executable and out pops the Amiga version.
  1721. >
  1722. >        Mind shifting stage left...
  1723. >
  1724. >        I have a suggestion, since it is not considered practicable to write a
  1725. >transpiler that will work unassisted, that may resolve the percieved problems
  1726. >using some of the concepts from this message thread.
  1727. >
  1728. >        First, an off-the-wall analogy.  Here he goes again. :-)
  1729. >
  1730. >        Consider a desirable program, running on another platform, useful to
  1731. >an Amiga user, but with a very large dongle attached.  This useful program must
  1732. >be run on the dongle in fact.  This is inconvient.  What do I do if I loose my
  1733. >dongle?  I propose you think of this as a form of copy protection  for a
  1734. >moment, obstructiing the Amiga owner, who also owns a program that runs  on
  1735. >another platform.  Follow me so far?  The legitimate program for the  other
  1736. >platform cannot be used on the favoured, Amiga machine, so it needs  to be
  1737. >decopy-protected.  What do you use if you wish to backup and/or deprotect
  1738. >software?  A copier program.  Most copiers can be employed by users who have
  1739. >little knowledge of copy protection, yet they succeed in making the copy.  The
  1740. >more difficult protection schemes require "brain files" which are written by
  1741. >experts to achieve this end.  End of transpiler/copy-protection analogy.
  1742. >
  1743. >        Take a basic transpiler, such as the one Eddy Carroll wrote, and add a
  1744. >toolkit.  The transpiler would do its best to translate and compile the source
  1745. >executable.  If this failed, an expert, not just the average user, would either
  1746. >run the transpiler in expert mode, or a debug tool from the toolkit, which
  1747. >would work through any problem areas.  The result of this would be an expert
  1748. >transpiler file.  The beauty of this approach is that any user could run the
  1749. >transpiler, given access to the expert file.  These expert files could be
  1750. >included with the release package and any new ones could be included in any
  1751. >updates or shared by conventional electronic means as PD.
  1752. >
  1753. >What do you think?
  1754. >
  1755. >csj
  1756. >
  1757. > No really officer, I wasn't speeding, just keeping a safe distance in front of
  1758. >the car behind me!
  1759. >
  1760. >Usenet: a542@mindlink.UUCP Phone: (604)853-5426 FAX: (604)854-8104
  1761. >
  1762. >
  1763. >From: Chris_Johnsen@mindlink.UUCP (Chris Johnsen)
  1764. >Newsgroups: comp.sys.amiga.emulations
  1765. >Subject: Emulator Mechanics [Transpiler]
  1766. >Message-ID: <5097@mindlink.UUCP>
  1767. >Date: 10 Mar 91 17:54:43 GMT
  1768. >Organization: MIND LINK! - British Columbia, Canada
  1769. >
  1770. >
  1771. >        Thank you all for the valued input into this discussion to date,  both
  1772. >pro and con.  I must say I find it very stimulating.
  1773. >
  1774. >        When discussing this form platform porting translator/compiler  with
  1775. >anyone, I find the need for a short word to use.  In a humble attempt  to coin
  1776. >a descriptive phrase, may I suggest transpiler?
  1777. >Charlie Gibbs (Charlie_Gibbs@mindlink.UUCP) Jean-Noel Moyne (jnmoyne@lbl.gov)
  1778. >Dave Clemans (dclemans@mentorg.com) Dwight Hubbard (uunet.uu.net!easy!lron)
  1779. >Pete Ashdown (pashdown@javelin.es.com) Jyrki Kuoppala (jkp@cs.HUT.FI) confirm
  1780. >that, indeed some research and even program development has been done in this
  1781. >direction.
  1782. >
  1783. >Eddy Carroll (ecarroll@maths.tcd.ie) has written such a transpiler, as a last
  1784. >year project, with some success, some reservations.
  1785. >
  1786. >Chris Gray (cg@ami-cg.UUCP) suggests that the most practicable route to
  1787. >desinging a viable transpiler would be to make it interactive.  BTW Chris, I
  1788. >very much enjoyed your compiler articles in the Amiga Transactor.
  1789. >
  1790. >Ray Cromwell (rjc@pogo.ai.mit.edu) suggested an interesting thought, a sort of
  1791. >Usenetware combined effort for development, he also thinks reasonable execution
  1792. >speed can be achieved.
  1793. >
  1794. >        Those are what I percieve to be the ideas supporting the transpiler
  1795. >concept.  The statements of contrary considerations are more voluminous. These
  1796. >appear to fall into a number of categories.
  1797. >          o  Self-modifying code
  1798. >          o  Separating code from data
  1799. >          o  Determining video access
  1800. >          o  Stack handling
  1801. >          o  Jump table problems
  1802. >          o  Handling overlay segments
  1803. >Ian Farquhar (ifarqhar@sunb.mqcc.mq.oz.au) suggested that a compiled module  be
  1804. >run concurrent with an emulator type section so that in some parallel  way, any
  1805. >references within the source executable, which would also be loaded during
  1806. >runtime of the emulation, could be validated.  It could be argued that this
  1807. >should be placed on the pro side of the ledger, but the incurred overhead
  1808. >during execution would be large.  This function, I had imagined initially,
  1809. >would be carried out during the transpiler phase and not attached to, or
  1810. >burdening, the runtime execution.
  1811. >
  1812. >Brad Pepers (pepers@enme1.ucalgary.ca) Jyrki Kuoppala (jkp@cs.HUT.FI)  Jonathan
  1813. >David Abbey (jonabbey@cs.utexas.edu) were somewhat concerned with
  1814. >self-modifying code.  There were a significant number of voices that dismissed
  1815. >this concern.  Personally, I wouldn't worry about it.  If a particular program
  1816. >used this technique, for whatever reason, I would accept the fact that not all
  1817. >programs can be transpiled.
  1818. >
  1819. >Ian Farquhar (ifarqhar@sunb.mqcc.mq.oz.au) Dave Clemans (dclemans@mentorg.com)
  1820. >Sullivan (Radagast@cup.portal.com) Jean-Noel Moyne (jnmoyne@lbl.gov)  Chris
  1821. >Gray (cg@ami-cg.UUCP) raised concerns about determining code from bytes of the
  1822. >data persuation.  This is what I had thought would be the biggest stumbling
  1823. >block. I hadn't thought there would be so many others. :-)  It's great to hear
  1824. >how how long the road is before you begin the journey.
  1825. >
  1826. >Udi Finkel (finkel@TAURUS.BITNET) Eddy Carroll (ecarroll@maths.tcd.ie) are
  1827. >concerned about determining when the access to memory is video ram.  This, I
  1828. >believe, can be solved, if it is dealt with in the same manner as the code/data
  1829. >resolution.  Please read on.
  1830. >
  1831. >Eddy Carroll (ecarroll@maths.tcd.ie) points out problems with processor stack
  1832. >handling.  How did you resolve this with the transpiler you developed?  It
  1833. >occurs to me that every emulator whether it be a transpiler, or interpretive
  1834. >type must handle the stack properly.  To me, it would be more difficult to
  1835. >approach this problem if I were writting an emulator, as opposed to a
  1836. >transpiler, as less executable size constraints would inhibit producing an
  1837. >intelligent stack handler.  On the 68000 the stack would be handled as
  1838. >word/longword only.  Any bytewise stack functions within the  source executable
  1839. >would be transpiled into wordwise handling.  A problem, but surmountable.
  1840. >
  1841. >Chris Gray (cg@ami-cg.UUCP) Eddy Carroll (ecarroll@maths.tcd.ie) raised the
  1842. >problem of coping with jump or branch tables properly.  Weighty point. This I
  1843. >would describe as a grey area between code and data where it may be actual jump
  1844. >instructions, or a table of label locations, the code to be  emulated
  1845. >calculating an offset into it.  This must be handled in a similar fashion to
  1846. >the code/data recognition problem.
  1847. >
  1848. >Dave Clemans (dclemans@mentorg.com) Kurt Tappe (JKT100@psuvm.psu.edu) Jonathan
  1849. >David Abbey (jonabbey@cs.utexas.edu) were concerned with the  handling of
  1850. >overlay segments.  This bothers me too.  With the Amiga, it  would be possible
  1851. >to either use overlays, in the case of very large programs,  or convert to
  1852. >all-in-one programs by consolidating the overlay hunks.  The  main problem is
  1853. >determining that overlays are employed, loading them and  transpiling.  This is
  1854. >closely related to the code/data determination problem.
  1855. >
  1856. >Dwight Hubbard (uunet.uu.net!easy!lron) suggest that this would work, kind of
  1857. >like DSM.  I have DSM and must say it was a model for me when contemplating
  1858. >this idea.  Chris Gray (cg@ami-cg.UUCP) believes that an  interactive compiler
  1859. >holds some promise.  In one of my initial messages to  Charlie Gibbs I
  1860. >entertained the reality of having to fall back on human  intervention, at the
  1861. >transpiler output source code level.  This would be  determined at runtime,
  1862. >when the emulated program failed to operate correctly  or at all.  A programmer
  1863. >would be required to fix the problem.
  1864. >
  1865. >        What I interpert the huge human parallel processor the net represents,
  1866. >to be saying, is that human intervention must be employed.  My only reservation
  1867. >in this regard is that there are interperative emulators that run a wide
  1868. >variety of software on-the-fly.  To me, this feat is more difficult than a
  1869. >transpiler, which can ruminate over the executable for as long as say a
  1870. >raytracer.
  1871. >
  1872. >        To incorporate the above concepts, and arrive at a workable resolution,
  1873. >what is required is an expert system, probably requiring a resident expert (not
  1874. >software, though brain matter is soft), to resolve the code/data, video access,
  1875. >jump/branch table and overlay problems.  I don't suppose that the average
  1876. >emulator user is prepared to deal with or even understand the problems we have
  1877. >been discussing.  I don't think it would be worthwhile writing a transpiler
  1878. >unless it could be made to work in some standalone fashion.  Feed it the
  1879. >source executable and out pops the Amiga version.
  1880. >
  1881. >        Mind shifting stage left...
  1882. >
  1883. >        I have a suggestion, since it is not considered practicable to write a
  1884. >transpiler that will work unassisted, that may resolve the percieved problems
  1885. >using some of the concepts from this message thread.
  1886. >
  1887. >        First, an off-the-wall analogy.  Here he goes again. :-)
  1888. >
  1889. >        Consider a desirable program, running on another platform, useful to
  1890. >an Amiga user, but with a very large dongle attached.  This useful program must
  1891. >be run on the dongle in fact.  This is inconvient.  What do I do if I loose my
  1892. >dongle?  I propose you think of this as a form of copy protection  for a
  1893. >moment, obstructiing the Amiga owner, who also owns a program that runs  on
  1894. >another platform.  Follow me so far?  The legitimate program for the  other
  1895. >platform cannot be used on the favoured, Amiga machine, so it needs  to be
  1896. >decopy-protected.  What do you use if you wish to backup and/or deprotect
  1897. >software?  A copier program.  Most copiers can be employed by users who have
  1898. >little knowledge of copy protection, yet they succeed in making the copy.  The
  1899. >more difficult protection schemes require "brain files" which are written by
  1900. >experts to achieve this end.  End of transpiler/copy-protection analogy.
  1901. >
  1902. >        Take a basic transpiler, such as the one Eddy Carroll wrote, and add a
  1903. >toolkit.  The transpiler would do its best to translate and compile the source
  1904. >executable.  If this failed, an expert, not just the average user, would either
  1905. >run the transpiler in expert mode, or a debug tool from the toolkit, which
  1906. >would work through any problem areas.  The result of this would be an expert
  1907. >transpiler file.  The beauty of this approach is that any user could run the
  1908. >transpiler, given access to the expert file.  These expert files could be
  1909. >included with the release package and any new ones could be included in any
  1910. >updates or shared by conventional electronic means as PD.
  1911. >
  1912. >What do you think?
  1913. >
  1914. >csj
  1915. >
  1916. > No really officer, I wasn't speeding, just keeping a safe distance in front of
  1917. >the car behind me!
  1918. >
  1919. >Usenet: a542@mindlink.UUCP Phone: (604)853-5426 FAX: (604)854-8104
  1920. >
  1921. >
  1922. >From: daveh@cbmvax.commodore.com (Dave Haynie)
  1923. >Newsgroups: comp.sys.amiga.emulations
  1924. >Subject: Re: Emulator Mechanics
  1925. >Message-ID: <19792@cbmvax.commodore.com>
  1926. >Date: 12 Mar 91 22:39:53 GMT
  1927. >References: <1991Mar7.093149.18707@mintaka.lcs.mit.edu> <19749@cbmvax.commodore.com> <1991Mar12.011418.24768@mintaka.lcs.mit.edu>
  1928. >Reply-To: daveh@cbmvax.commodore.com (Dave Haynie)
  1929. >Organization: Commodore, West Chester, PA
  1930. >
  1931. >In article <1991Mar12.011418.24768@mintaka.lcs.mit.edu> rjc@geech.ai.mit.edu (Ray Cromwell) writes:
  1932. >>In article <19749@cbmvax.commodore.com> daveh@cbmvax.commodore.com (Dave Haynie) writes:
  1933. >>>In article <1991Mar7.093149.18707@mintaka.lcs.mit.edu> rjc@geech.ai.mit.edu (Ray Cromwell) writes:
  1934. >
  1935. >>>> Why doesn't AMAX multitask? 
  1936. >
  1937. >>>Without massive patching of the Mac ROM, I don't think so.  
  1938. >
  1939. >>  Well  it's not a heavy loss if you can't make the Mac ROM resident, but
  1940. >>why does AMAX have to take over the Amiga's operating system? The only
  1941. >>thing that would make it really diffcult to run MacOS under
  1942. >>AmigaDOS is if Mac code fiddles with absolute memory locations or the
  1943. >>OS implements function calls as traps/interupts.
  1944. >
  1945. >Well, the Mac OS fiddles with absolute memory locations, and the OS implements
  1946. >function calls as line-A exceptions.  Apparently, all the absolute locations
  1947. >are in low memory and get swapped as part of the process context when you run
  1948. >Multifinder.
  1949. >
  1950. >>I also wonder why readysoft used a special disformat for Mac disks
  1951. >>instead of reading Amiga disks.
  1952. >
  1953. >No doubt this was to make Mac software work on Amiga disks.  The Mac isn't
  1954. >as nice about filesystem independence as the Amiga is, so you can't really
  1955. >provide a loadable Mac filesystem kind of thing that maps Mac filespace
  1956. >into Amiga filespace adequately.  So ReadySoft essentially just built a new
  1957. >device driver, which uses Amiga-readable formats, but Mac filesystem 
  1958. >organization.
  1959. >
  1960. >This is apparently why Mac's don't generally talk to MS-DOS disks via an
  1961. >alternate filesystem like CrossDOS or MSH, but instead use a user program for 
  1962. >the conversion, along the lines of the old PC Utilities.  
  1963. >-- 
  1964. >Dave Haynie Commodore-Amiga (Amiga 3000) "The Crew That Never Rests"
  1965. >   {uunet|pyramid|rutgers}!cbmvax!daveh      PLINK: hazy     BIX: hazy
  1966. >    "What works for me might work for you"    -Jimmy Buffett
  1967. >
  1968. >
  1969. >From: ifarqhar@sunb.mqcc.mq.oz.au (Ian Farquhar)
  1970. >Newsgroups: comp.sys.amiga.emulations
  1971. >Subject: Re: Emulator Mechanics [Transpiler]
  1972. >Message-ID: <1338@macuni.mqcc.mq.oz>
  1973. >Date: 13 Mar 91 03:24:04 GMT
  1974. >References: <5097@mindlink.UUCP>
  1975. >Organization: Macquarie University, Sydney, Australia.
  1976. >
  1977. >In article <5097@mindlink.UUCP> Chris_Johnsen@mindlink.UUCP (Chris Johnsen) writes:
  1978. >>Ian Farquhar (ifarqhar@sunb.mqcc.mq.oz.au) suggested that a compiled module  be
  1979. >>run concurrent with an emulator type section so that in some parallel  way, any
  1980. >>references within the source executable, which would also be loaded during
  1981. >>runtime of the emulation, could be validated.  It could be argued that this
  1982. >>should be placed on the pro side of the ledger, but the incurred overhead
  1983. >>during execution would be large.  This function, I had imagined initially,
  1984. >>would be carried out during the transpiler phase and not attached to, or
  1985. >>burdening, the runtime execution.
  1986. >
  1987. >No, I suggested that an image of the original code, without translation,
  1988. >be carried around with the compiled code, with indexes into the compiled
  1989. >code's equivalent sections that would allow jump tables and so forth to
  1990. >function correctly.  This would be a memory intensive but low-time-overhead
  1991. >time way of resolving problems.  It should also be noted that such a
  1992. >system would allow trapping of addresses that need to be handled by
  1993. >special code (hardware locations etc.), and that the image is also used
  1994. >for data storage, meaning that once loaded and allocated, the program
  1995. >would be unlikely to need any further allocation of heap.
  1996. >
  1997. >This system solves two problems: the data/code differentiation (if you
  1998. >accidentally compile some data, it is no great problem) though a much
  1999. >more minor problem remains if a couple of opcodes are missed because of
  2000. >data being accidentally compiled and the compiler assuming an opcode is
  2001. >the data for a false opcode, and also the problem of jump tables
  2002. >and jumping to locations stored in registers.
  2003. >
  2004. >--
  2005. >Ian Farquhar                      Phone : + 61 2 805-9400
  2006. >Office of Computing Services      Fax   : + 61 2 805-7433
  2007. >Macquarie University  NSW  2109   Also  : + 61 2 805-7420
  2008. >Australia                         EMail : ifarqhar@suna.mqcc.mq.oz.au
  2009. >
  2010. >
  2011. >From: srwmpnm@windy.dsir.govt.nz
  2012. >Newsgroups: comp.sys.amiga.emulations
  2013. >Subject: Re: CPU-emulators
  2014. >Message-ID: <18878.2801a4ad@windy.dsir.govt.nz>
  2015. >Date: 9 Apr 91 11:25:31 GMT
  2016. >Organization: DSIR, Wellington, New Zealand
  2017. >
  2018. >Ilja Heitlager (iheitla@cs.vu.nl) wrote:
  2019. >>I'm planning to write a 6502 (and maybe when I like it some others) emulator.
  2020. >
  2021. >Good on you!  I've played around with the Z80 emulators for the Amiga, by Ulf
  2022. >Nordquist and Charlie Gibbs, making them faster.  I have never touched 6502 but
  2023. >the same techniques should apply.
  2024. >
  2025. >>At this moment I think there are two ways of doing it:
  2026. >>    1- Compare every Opcode and jump to a routine which executes the
  2027. >>       instruction
  2028. >>    2- Do it more or less the way the microcode does it.
  2029. >>       Ok in software you can't do more operations at the same moment.
  2030. >
  2031. >I found at several more fundamentally different ways of doing it, and many
  2032. >variations on those.  So far the fastest practical method seems to be threaded
  2033. >code.  You can avoid decoding an opcode for every 6502 instruction altogether!
  2034. >The emulation routine for each 6502 opcode ends with:
  2035. >
  2036. >        move.l    (a3)+,a0
  2037. >        jmp    (a0)
  2038. >
  2039. >So each emulation routine jumps directly to the next emulation routine without
  2040. >any decoding at all.  Register a3 is acts like a "pseudo pc" into a 256 kbyte
  2041. >table in which there is a longword pointer to the emulation routine for each
  2042. >corresponding opcode in the 64 kbyte 6502 address space.
  2043. >
  2044. >Now, every time the 6502 writes to RAM, you need to update an entry in the
  2045. >256 kbyte table.  At first it looks as if you have to do an instruction decode
  2046. >to compute the new table value every time the 6502 writes to RAM.  But in fact
  2047. >that is not necessary either!
  2048. >
  2049. >What you do, when the 6502 writes to RAM, is to write a constant address into
  2050. >the table.  That constant address points to a special routine called "patch".
  2051. >When patch is called, you finally get to do an instruction decode.  Patch
  2052. >computes the address of the routine for the current instruction, stuffs it
  2053. >in the 256 kbyte table, then jumps to the routine for the current instruction.
  2054. >Next time this instruction is executed, control bypasses patch and goes
  2055. >directly to the right routine.
  2056. >
  2057. >A variation of this method which saves memory but is slightly slower, is to use
  2058. >word offsets in a 128 kbyte table, instead of longword addresses in a 256 kbyte
  2059. >table.  Each routine ends with:
  2060. >
  2061. >        move.w    (a3)+,d0
  2062. >        jmp    0(a2,d0.w)
  2063. >
  2064. >where a2 holds the base from which all the routine offsets are computed.
  2065. >
  2066. >This method has more advantages:
  2067. >
  2068. >1: To handle known ROM entry points, just point the vector for the entry point
  2069. >at an optimised 68000 routine to do what the ROM routine does.  There is no
  2070. >overhead at all in checking for ROM entry points.
  2071. >
  2072. >2: To handle multiple-byte opcodes (e.g, prefix instructions), patch can be made
  2073. >smart enough to point the vector for the prefix byte to the routine for the
  2074. >entire instruction.  There is no need to decode opcodes after the prefix every
  2075. >time the instruction is executed.
  2076. >
  2077. >3: Patch can be made smart enough to recognise common sequences of 6502
  2078. >instructions, and to point the vector at an optimised 68000 routine for the
  2079. >whole sequence.
  2080. >
  2081. >Note that 2 and 3 above (if implemented) won't correctly emulate certain types
  2082. >of self-modifying code.
  2083. >
  2084. >There was a good article on "Portable Fast Direct Threaded Code" by Eliot
  2085. >Miranda in comp.compilers recently.  He uses GCC to write "machine independent"
  2086. >threaded code that is just about as efficient as my 68000-specific code.
  2087. >
  2088. >Hope this helps.  Regards, Peter McGavin.   (srwmpnm@wnv.dsir.govt.nz)
  2089. >
  2090. >
  2091.