home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / CPM / 6502 / 6502SIM.LBR / ZXART.DQC / ZXART.DOC
Text File  |  2000-06-30  |  15KB  |  289 lines

  1. ZX65: Simulating a Micro                    Page 1
  2.  
  3.  
  4.     Having logged many hours of software development work on my Z80-
  5. based system, I recently decided that I was up to a particular challenge:
  6. using my system as a development tool for a totally different microprocessor;
  7. specifically, the 6502.  This decision, I must confess, was not altogether
  8. based on a burning lust for knowledge, but rather on a more practical re-
  9. quirement:  I had become involved in the development of a 6502-based device,
  10. and had no ready access to a 6502 development system.  I decided that a sim-
  11. ple cross-assembler wasn't good enough, and set out to write a 6502-to-Z80
  12. object code translator/simulator/interpreter.
  13.  
  14.     The program described here, which I have dubbed ZX65, is the result
  15. of my efforts to generate a software package to perform this task.  It is 
  16. useful not only for the intended purpose of software development, but also
  17. for the rewarding experience of exploring a whole new field of good soft-
  18. ware written for a processor other than one's own.
  19.  
  20.     ZX65 operates as an interpretive simulator; that is, a simulated
  21. 6502 CPU is maintained in system memory, and each encountered 6502 instru-
  22. ction is decoded and processed interpreter-fashion to properly act upon the
  23. simulated CPU registers.  The entire package resides in just over 4K Bytes
  24. of high system memory, leaving the remaining portion, including the all-im-
  25. portant (for 6502) base page, free for program and data storage.
  26.  
  27.  
  28.  
  29. ZX65: Simulating a Micro                    Page 2
  30.  
  31.  
  32. Note that CP/M* serves only as a loader and is not used thereafter.  In fact,
  33. while the program loads normally into CP/M's Transient Program Area, it imm-
  34. ediately relocates itself into high memory, overlaying both the CCP and the
  35. BDOS portions of CP/M.  The user I/O linkages (CBIOS), however, remain intact
  36. and are used by the package to perform disk and console functions.  The func-
  37. tions which must be provided by the system are a subset of those required by
  38. CP/M and should thus be readily available.  These functions are all accessed
  39. via a jump table within the interpreter and thus can be readily changed to
  40. accomodate different system requirements (see table 1).  Parameters must be
  41. passed to the drivers in register "C" (register pair "BC" for item six) and
  42. data, if any, is returned in the accumulator.
  43.  
  44.     ZX65 is referred to as a package since it includes three distinct
  45. function groups.  The first and largest is the interpreter/simulator itself.
  46. The second group is an elementary monitor providing the functions of memory
  47. examine and modify.  The third group consists of a self contained, elementary
  48. disk operating system (DOS). (Note: ZX65 files are not CP/M compatible.) The
  49. normal configuration assumes a two-drive system, with the system diskette on
  50. drive A, and all internal disk access routed to drive B.
  51.  
  52.  
  53.  
  54. ZX65: Simulating a Micro                    Page 3
  55.  
  56.  
  57. (Modification for a single drive system could easily be done, requiring the
  58. user to switch diskettes after the package is loaded.)  The package is writ-
  59. ten in standard Zilog/Mostek source code and is quite heavily commented.  Much
  60. use is made of the Z80's special instructions and index registers, and oper-
  61. ation on an 8080 is definitely not possible without extensive modification.
  62. Although the commented listing should guide you through the program's opera-
  63. tion without too much trouble, a brief description of the interpreter portion
  64. is in order. A study of the small but powerful instruction set of the 6502
  65. will reveal certain patterns in the op codes, as also occurs with most other
  66. processors. The eight "BRANCH ON CONDITION" codes, for example, all have zero
  67. as a low-order nybble, while the high-order portion is always an odd value.
  68. Another pattern, not so obvious, is that every instruction having an odd-
  69. valued op code uses multiple addressing modes.  These and other such patterns
  70. are exploited by the program.  Even so, much of the code is devoted solely to
  71. decoding the op code and transferring control to the proper instruction proc-
  72. essor.  The code beginning at "STEP" (line 241) performs the initial decoding,
  73. and fully decodes certain instructions such as "RTI", "JMP", and a few others.
  74. In most cases, decoding proceeds to the point where the instruction is known
  75. to be part of a group, such as the branch group described above.  In these
  76. cases, look-up tables are used, but not always in the same way.
  77.  
  78.  
  79.  
  80. ZX65: Simulating a Micro                    Page 4
  81.  
  82.  
  83. The branch group and the flag set/reset group, for example, derive mask
  84. patterns from the tables which are used for testing, setting, or resetting
  85. the condition flags.  In the case of multiple addressing mode instructions,
  86. however, the tables serve an entirely different purpose.  The table contents
  87. are, in these instances, decoded into addresses for indirect jumps; first to
  88. the addressing mode processors, and then to the actual instruction processors.
  89. Each processor performs its instruction as required.  The "ADC" instruction,
  90. as an example, causes a byte of data to be fetched from memory or from the
  91. object code, depending on the addressing mode.  After setting the Z80 carry
  92. flag to match the simulated 6502 carry flag, the data byte is added to the
  93. value currently in the simulated accumulator, and the result is stored there.
  94. The simulated carry flag is then updated to again match the Z80 carry, and the
  95. simulated sign, overflow, and zero flags are set or reset according to the re-
  96. sults in the accumulator.  (In this case, the overflow flag is set or reset
  97. according to a logical "OR" of bits six and seven.)  Considerable care has
  98. been given to treatment of the flags, since Z80 flags and their 6502 name-
  99. sakes do not always have the same meaning.  (Case in point:  the 6502 carry
  100. flag assumes a logical "NOT" borrow meaning during subtract and compare op-
  101. erations, while the Z80 and most other common processors invert the carry  
  102. flag internally during these operations so that it represents a "TRUE" bor-
  103. row. Other flag differences, for the most part, are more subtle.)
  104.  
  105.  
  106.  
  107. ZX65: Simulating a Micro                    Page 5
  108.  
  109.  
  110. Finally each individual processor is responsible for exiting with codes
  111. for the mnemonic, the addressing mode, and the number of bytes occupied by
  112. the instruction available for use by the "DISPLAY" routine (line 173) to in-
  113. dex into yet another set of tables and retrieve the ASCII messages for mnem-
  114. onic and addressing modes.  Upon exiting the instruction processors, the val-
  115. ue stored in "NEXT PROGRAM COUNTER" is updated, and control is passed back to
  116. the COMMAND section where the operating mode in effect determines whether to
  117. display the updated CPU contents, and whether or not to continue execution
  118. with the next instruction.  
  119.  
  120.  
  121.  
  122.     Several distinct operating modes are available, defined briefly as
  123. follows:
  124.  
  125. SINGLE-STEP MODE:  One instruction is decoded and executed. The console
  126.     then displays the address of the instruction just executed (Current
  127.     Program Counter), the decoded instruction (Mnemonic and Operand),  
  128.     the state of all CPU registers, and the address of the next instr-
  129.     uction to be executed (Next Program Counter).  Control is then
  130.     returned to the user.
  131.  
  132. MULTI-STEP TRACE MODE:  The user specifies up to 255 instructions to be
  133.     executed with the results displayed as above following each step.
  134.     This occurs at the rate of approximately two instructions per sec-
  135.     ond.  Control is returned to the user after the required number
  136.     of steps have been executed and displayed.
  137.  
  138. RUN-TO-BREAKPOINT MODE:  Instructions are executed without display up to
  139.     and including the user-specified breakpoint.  CPU registers are
  140.     then displayed as above, and control is returned to the user.
  141.  
  142.  
  143.  
  144. ZX65: Simulating a Micro                    Page 7
  145.  
  146.  
  147. FREE-RUN MODE:  Instructions are executed with no display and no break-
  148.     points.  The console keyboard, however, is monitored, and CPU con-
  149.     tents are displayed as above and control is returned to the user
  150.     at any point upon console input.
  151.  
  152.     The user may examine any or all CPU registers, including the Pro-
  153. gram Counter, at any time that he has control, without otherwise disturbing
  154. any operation on progress.
  155.  
  156.     The 6502 "BRK" instruction (software interrupt) is handled through
  157. a vector location, just as in a normal system.  Optionally, however, the BRK
  158. instruction may be used (and is initialized) to return control to the user
  159. in the same manner as the breakpoint mode described above.  The BRK instru-
  160. ction and breakpoint mode operate independently, and both may be used in the
  161. same program.  Additionally, up to five different "user" subroutines may be
  162. called for transfer of control to Z80 routines (such as console I/O).  These
  163. are invoked by executing either a "JMP" or "JSR" instruction to addresses
  164. 0000, 0001, 0002, 0003, and 0004 (HEX).  Control is passed via a jump table
  165. located within the interpreter, which the user may load with Z80 "JP" ins-
  166. tructions to the desired subroutines.  The subroutines must be terminated
  167. in the normal Z80 fashion by one of the "RET" instructions.  Note that the
  168. first two entries in the table (JMP or JSR 0000, 0001 HEX) are initialized
  169. as console input and output via the 6502 accumulator.  Three parameters may
  170. be passed to and from all user subroutines, as outlined in table 2.  Table
  171. 3 lists the locations of the BRK vector and the user subroutine jump table.
  172.  
  173.  
  174.  
  175. ZX65: Simulating a Micro                    Page 8
  176.  
  177.  
  178.     Although the interpreter is quite versatile and all 6502 instruc-
  179. tions are handled properly, there are some limitations to its use.  Perhaps
  180. the most important of these is that, even in the "free-run" mode, execution
  181. is quite slow compared to an actual 6502 running at normal clock speeds.  No
  182. attempt has been made to correlate execution times with normal 6502 clock
  183. periods.  (This may, in fact, be impossible due to the way in which the sys-
  184. tem handles multiple addressing modes.)  Therefore, programs incorporating 
  185. software timing loops will not execute properly.  Also, since the package was
  186. intended for software development, hardware interrupts are not supported, and
  187. only a limited amount of hardware interfacing can be performed through the
  188. "user" subroutine calls.
  189.  
  190.     Two program listings are included here.  The first, called "ZXLD",
  191. is a short program whose only function is to boot the main program from the
  192. CP/M Transient Program Area to high memory.  The second listing is the in-
  193. terpreter itself, assembled to reside from 6F00 to 7DFF (HEX) in a 32K sys-
  194. tem.  Note that this leaves 512 bytes, normally containing CBIOS, unaffected.
  195.  
  196.  
  197.  
  198. ZX65: Simulating a Micro                    Page 9
  199.  
  200.  
  201. The program is not relecatable, so that in order to run in a different mem-
  202. ory configuration, it will be necessary to reassemble the source with a new
  203. value in the "SIZE EQU XX" statement.  All other addresses are derived from 
  204. this value.  If the object code is to be manually loaded for the 32K system
  205. then "ZXLD" must be loaded first, starting at 0100 (HEX).  Then start loading
  206. ZX65 at 0110 (HEX), continuing until the end.  Finally, use the CP/M "SAVE"
  207. command (SAVE 15 ZX65.COM) to write the object code to disk.  The package can
  208. now be invoked by answering "ZX65" to the CP/M prompt.  If you are reassem-
  209. bling the package, then you should also reassemble the ZXLD routine (modified
  210. for your system) so that you have a .HEX file for each module.  Then load
  211. the package using DDT as follows:
  212.  
  213.     DDT ZXLD.HEX
  214.     IZX65.HEX
  215.     Rdisp
  216.  
  217. where "disp" is the load offset value required to load the object code from
  218. the .HEX file at a different address than that specified.  (DDT will calcu-
  219. late "disp" for you if you type "H110,ssss", where "ssss" is the actual
  220. starting address of your program.  DDT will respond with the sum and diff-
  221. erence of the two values, and the difference will be the required value
  222. "disp".)  Finally, save the object code as directed above for manual loading.
  223.  
  224.  
  225.  
  226. ZX65: Simulating a Micro                    Page 10
  227.  
  228.  
  229.     To run the program, place the disk with ZX65.COM on drive A (this
  230. disk should also have a copy of CP/M so that the system can be restarted, if
  231. necessary, without changing disks.)  Place the ZX65 data disk on drive B.
  232. Start CP/M and invoke ZX65 by answering "ZX65" to the CP/M input prompt.  
  233. The system will prompt you with a header and a ">" symbol.  You are now in
  234. the command mode, and typing one of the command codes (see table 4) will
  235. cause the appropriate action to occur.  Note that if you are using a disk
  236. not previously used for ZX65 on drive B, you will be unable to use the ZX65
  237. DOS functions until you have initialized the disk.  This is done simply by
  238. typing "I", at which point the system will ask you to verify this request
  239. with a "Y" or "N".  The reason for this (note this well) is that the "I"
  240. command clears and initializes the directory on disk B.  This is the equiv-
  241. alent of the CP/M command "ERA *.*" and must be used with caution.
  242.  
  243.     For those unwilling to type in a 4K byte program by hand, I will
  244. make the package available on a standard 8 inch IBM format, single side,
  245. single density diskette (Sorry, I can't handle any other format!) at a nom-
  246. inal charge of $15 to cover time and material.  Write to me at the address
  247. given at the head of this article, and specify your system memory size.  I
  248. will send a diskette containing both source and object files assembled to
  249. reside in high memory as described above, as well as a copy of ZXLD.
  250.  
  251.  
  252.  
  253. ZX65: Simulating a Micro                    Page 11
  254.  
  255.  
  256. A command summary, operating hints, and other general information will be in-
  257. cluded on the disk.
  258.  
  259.     Finally, although this program has been rather exhaustively tested
  260. with a variety of 6502 software (both simple and sophisticated) I cannot 
  261. guarantee it bug-free.  I have included no copyright notices of any kind,
  262. and I hope users will feel free to copy, add, delete, and improve at will.
  263. (I would like to hear of any major modifications...I may want to include them
  264. myself!)  As stated at the outset, the package was developed as a side benefit
  265. of another project, not as a money maker, and if anyone else finds it useful,
  266. so much the better.                -RMK
  267.  
  268.  
  269.  
  270. Acknowledgements:
  271.  
  272.     *CP/M is a registered trademark of Digital Research, Pacific
  273.     Grove, CA.
  274.  
  275.     While not specifically mentioned in the text, I have relied
  276.     heavily on the information contained in the book "6502 Assembly
  277.     Language Programming" by Lance A. Leventhal (Osborne/McGraw Hill
  278.     Inc., 1979)
  279.  
  280.  
  281. 
  282. orne/McGraw Hill
  283.     Inc., 1979)
  284.  
  285.  
  286. 
  287. 
  288.                         
  289. σ═É╤═?δßs#r!    4├╙7!    n&├í:δ!9∙δ┴╔┼!  9∙DM!    ^#Vr+sz│╩≈:═Énδ`is!    ^#Vr+s╒═É    nδßs!        ^#Vr+s╒`inδßs├▓:δ!9∙δ┴╔