home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / emulate / z80emula.lha / Z80Intro < prev    next >
Text File  |  1992-03-08  |  38KB  |  718 lines

  1. Introduction to Z80 Emulator / Cross Developer V1.00ß
  2.  
  3. Written by Phil Brown, Copyright 1992
  4.  
  5. This  program  forms  the  basis of my final year individual project for my
  6. Masters  degree  in Software Engineering at Imperial College, UK.  The work
  7. contained within it is entirely my own so I take full responsibility for it
  8. and  any  credit,  if  it  is due.  Currently it is the result of about two
  9. weeks  work, and is now in a state where it provides moderate functionality
  10. to those with an interest in the Z80.  After my finals are complete (May) I
  11. have  six weeks to complete the project, in which time I shall expand it in
  12. the  course  of my research and probably incorporate any feedback I receive
  13. on this initial release.
  14.  
  15. The  program  is not being written for financial reward and I ask for none.
  16. This program may be copied, transferred, posted and distributed freely.  If
  17. you  use  this  program, then it would be nice if you could let me know and
  18. tell  me what you are using it for, as this may affect future developments,
  19. and I will derive pleasure from this.  Please don't send me money.  I don't
  20. expect  this  program to make me a millionaire, and if there is someone out
  21. there  who  finds it useful then that is just reward for all the neat stuff
  22. with  which others have provided me.  At some later date (very much later),
  23. this program may evolve into a shareware product so if you feel the urge to
  24. shower  me  with  gifts  then  hold  on until I feel justified in accepting
  25. remuneration  for  the  product, or spend it on your girlfriend/partner.  I
  26. know which I would choose.
  27.  
  28. Please  note that this is a product under development.  It is supplied with
  29. no  guarantees.  Use it at your own discretion.  I will however gladly give
  30. support  to this product (my finals are coming up so my response may not be
  31. all  I  would like), and I appreciate any feedback (especially bug reports)
  32. on the use of this program.
  33.  
  34. What you will need:
  35.  
  36. - A Commodore Amiga with about 50K CHIP and 200K FAST memory available.
  37.  
  38. - Preferably some experience or understanding of the Zilog Z80 CPU.
  39.  
  40. I  don't intend to describe the intricacies or workings of the Z80 here, so
  41. in  the  following  documentation  I'll  use standard Z80 mnemonics without
  42. further  explanation.   Those who don't understand these will probably find
  43. this program fairly useless anyway.
  44.  
  45. The philosophy behind this program:
  46.  
  47. This  program  was  written to investigate the levels at which a CPU can be
  48. emulated  in  software.   More  on  this  later.   As  such it comprises an
  49. emulation  of  all  the  internal registers of the Z80, a 64K memory map, a
  50. complete   cross-assembler  and  disassembler  and  a  simple  interpretive
  51. emulation  routine which should simulate all the effects of instructions on
  52. a  Z80.   Furthermore  an "abstract machine" is provided which looks like a
  53. console  screen  and  keyboard to the Z80, responding via IN and OUT ports,
  54. but  which  provides  a "bridge" to the peripherals and devices of the host
  55. computer (the Amiga).
  56.  
  57. It  was  not  written  to  emulate  any one particular system, although the
  58. highly modular nature of the code means that new abstract machines, methods
  59. of  software  emulation and even CPUs may be added very easily.  The code I
  60. have  written  provides  a  core system around which I may add other things
  61. very  easily  for  my research.  However, as it stands it provides a fairly
  62. complete  cross-development  system  for  Z80-based  targets.  As there has
  63. (somewhat  surprisingly)  proved to be moderate demand for such a system, I
  64. am releasing this software.  Comments and suggestions are welcomed, however
  65. I  already  have  a huge list of things I wish to investigate as part of my
  66. project and these must take priority.
  67.  
  68. For  those  interested parties the program is written entirely in Modula-2.
  69. Untrendy  it may be but my Benchmark compiler gets the job done in half the
  70. time  of  a  C compiler.  The program was developed on an A500 with a 25MHz
  71. MegaMidgetRacer  68030  accelerator.  It has been tested on this system and
  72. no  others  (in particular 2.0).  I see no reason why it should not work on
  73. all  systems  as I don't do anything even faintly naughty.  At the moment I
  74. am  debating  lashing  out on a nice new A1500 with GVP Combo, but I suffer
  75. from the traditional student woes of lack of moolah.
  76.  
  77. What you currently get:
  78.  
  79. - A representation of the internal registers of the Zilog Z80 CPU. These
  80.   include all the nasty things like the Half Carry register, IFF1 and
  81.   IFF2 and the Parity/Overflow flag.
  82.  
  83. - A simulated 64K address space.
  84.  
  85. - A single pass assembler.
  86.  
  87. - A symbolic disassembler.
  88.  
  89. - Some debugging tools.
  90.  
  91. - An emulated console.
  92.  
  93. - An interpretive emulator.
  94.  
  95. Please note that this emulator was not designed for speed.  It does however
  96. give  a reasonable throughput, and future developments of this program will
  97. give you VERY fast emulation.  Incidentally, the emulator is designed to be
  98. reasonably  portable  and  all the Amiga-specific stuff is contained in one
  99. module.   There's no reason why porting to another system shouldn't be very
  100. trivial.  Sun incarnations will probably be forthcoming.
  101.  
  102. Central  to  the  design  of the system is the Abstract Instruction Format.
  103. When  I  started  designing  the program it became obvious that significant
  104. benefits  in  design and modularity could be released if I introduced a new
  105. representation   of  a  Z80  instruction.   Let  me  explain  this  a  bit.
  106. Traditionally  there  are  two  representations  for  an  instruction;  the
  107. mnemonic  string  eg LD A,(IX+23) and the byte-code eg 253,126,23.  Now the
  108. first  stage  of  disassembling and emulation is the same, to work out what
  109. the  heck  these  little  strings  of  1's  and  0's  really mean, the only
  110. difference  being that when disassembling you display the mnemonic and when
  111. emulating you simulate the effects.
  112.  
  113. Normally  the  code to display the instruction or emulate it is threaded in
  114. amongst  the  disassembly of the byte codes.  This would result in two very
  115. similar  routines,  which is unsatisfactory from a design elegance point of
  116. view,  and also makes the code less legible and more difficult to debug and
  117. maintain.  Similar problems exist when considering assembly and interactive
  118. emulation.   Say we want the user to be able to type in an instruction from
  119. a  prompt  and  see the effects; normally it would be necessary to assemble
  120. this string into some place in memory and emulate it from there.
  121.  
  122. What  I  decided  to  do  was to introduce a structure which stored all the
  123. information  held within an instruction in an easily accessible form.  This
  124. was  used  to move between the different representations.  For example, the
  125. first  phase  of  disassembly  and emulation is now the same; to decode the
  126. bytes  into an abstract instruction, then this abstract structure is either
  127. displayed  (via  a  simpler  decoding  routine)  or emulated.  The abstract
  128. instruction thus lies between the two traditional representations:
  129.  
  130.                         Instruction Mnemonic
  131.                             LD A,(IX+23)
  132.  
  133.                               |    ^
  134.                               |   /|\
  135.                    Parser     |    |     Decoder
  136.                              \|/   |
  137.                               V    |
  138.                                                       \
  139.                         Abstract Instruction ----------> Emulator
  140.                                LOAD,                  /
  141.                           Direct Register A,
  142.                Indirect Register pair IX displaced 23
  143.  
  144.                               |    ^
  145.                               |   /|\
  146.                 Assembler     |    |     Disassembler
  147.                              \|/   |
  148.                               V    |
  149.  
  150.                              Byte Code
  151.                             253,126,23
  152.  
  153. This  structure  gives the code great modularity and is also quite elegant.
  154. Thus  disassembly  to  mnemonic  form  now  disassembles  from byte code to
  155. abstract format, and is decoded from that to the mnemonic string.  This has
  156. a performance hit but is not too bad.
  157.  
  158. Now the most useful bit: how to use the program.
  159.  
  160. First  the  good news:  most functions can be accessed from a typed command
  161. interface, or from a menu.
  162.  
  163. Now  the  bad  news:   menus  are not completely finished.  Any menu option
  164. which  is  interactive (such as a defining a filename to load or save) will
  165. give a strange error message (strange to you, not to me!), normally telling
  166. you that you have not defined all required parameters.  Why is this?  Well,
  167. I had limited time and did not want to write a whole load of requestor code
  168. which  would  be  scrapped  when (hopefully fairly imminently) I upgrade to
  169. 2.04  and  use the requestor libraries from there.  So, for the time being,
  170. if  a  menu  command  doesn't  work  then use the command line.  Menu items
  171. currently affected are:  Load, Save, Disassemble, Dump, Initialise and Copy
  172. memory.  All others should work OK.
  173.  
  174. When  the  program  is fired up (from the CLI, Workbench, Runbacked or just
  175. about any other way you fancy) you will be presented with a medium-res four
  176. colour  screen  with  two  sections.   The first section, occupying the top
  177. seven  lines  of the screen show the current register contents.  The second
  178. section  is  your  interactive command section, and where most responses to
  179. your commands will happen.
  180.  
  181. The program knows about a "current number base" which is the number base in
  182. which  values  will  be  displayed.   This can be Binary, Octal, Decimal or
  183. Hexadecimal.   On  startup the system is in Decimal mode (sorry all you hex
  184. fans,  it's  what I grew up with on the Z80.  It's strange; for some CPUs I
  185. think  in  hex,  for others in decimal...maybe I'm just wierd).  You can of
  186. course change this by simply selecting an item from the Settings menu or by
  187. typing a command, or by using a startup script.
  188.  
  189. Before  I start banging on about the individual functions, there's a couple
  190. of  things  I should mention.  The command parser is pretty flexible.  It's
  191. handled  by  a  database  which  details for each command the parameters it
  192. expects,  their  type  and  whether  they  are  optional  or required.  The
  193. commands  may be accessed by typing as much of the command as required eg D
  194. is  adequate  for  DISASSEMBLE, but at least DU is required for DUMP.  Each
  195. parameter  has  a keyword associated with it (unless it is a flag) and this
  196. may  be used to identify the following parameter.  If the parameter keyword
  197. is  missing  then  positional  placement  dictates which parameter is being
  198. defined.   Hmm.   I  have  not  defined  this too well methinks.  Here's an
  199. example.  The template for DISASSEMBLE is:
  200.  
  201. DISASSEMBLE <START Address> [END Address] [FILE FileName] [NOLABELS]
  202.  
  203. What  this  means  is that the first parameter is required and is the start
  204. address.  The other three are optional and the last is a flag which acts as
  205. a  switch.   Of  course  any or all of the command may be in upper or lower
  206. case.  If the user types:
  207.  
  208. D 0 100 Prog.ASM nolabels
  209.  
  210. Then the absence of keywords means that position dictates which is which so
  211. disassembly  starts from 0, ends at 100, output is to the file Prog.ASM and
  212. symbolic disassembly is switched off.
  213.  
  214. If, however, the user types:
  215.  
  216. DISASSEMBLE FILE Prog.ASM NOLABELS END 100 START 0
  217.  
  218. then  the parser will locate the parameters according to their keywords and
  219. the  result will be the same as previously.  The template may also indicate
  220. a  switch  list,  such  as  [  ON  |  OFF ] which specifies that one of the
  221. switches may be defined.
  222.  
  223. Furthermore, when a numeric parameter is required (such as an Address), the
  224. expression  handler  utilised  in  the  assembler is used.  This means that
  225. expressions  containing  symbols  may  be  used.   Standard  precedence  is
  226. observed.   Therefore, assuming the labels are defined in the symbol table,
  227. the user may type the following:
  228.  
  229. DISASSEMBLE MY_LABEL*10-20 END_CODE
  230.  
  231. and  the  expressions  will  be  evaluated  and  passed  to  the routine as
  232. expected.   If  the user wants to define a filename which contains a space,
  233. double quotes may be placed around the name.
  234.  
  235. Also,  any  numbers  specified  as  parameters will be assumed to be in the
  236. current  default number base.  If the user wishes to override this then the
  237. following prefixes may be used:
  238.  
  239. % - Binary
  240. & - Octal
  241. # - Decimal
  242. $ - Hexadecimal
  243.  
  244. eg %01111111, &177, #127, $7F all represent the same decimal value of 127.
  245.  
  246. I'll now describe in detail what those commands mean and what they do.
  247.  
  248. ASSEMBLE [FILE FileName] [START Address]
  249.  
  250. Initiates  assembly of the specified file to the specified address.  If the
  251. file  is  not  specified, or if the special token 'INLINE' is used then the
  252. input will be interactively from the keyboard, rather than from a file.  If
  253. the  start  address  is  not  specified then the current value of the PC is
  254. used.   Of  course, most programs will have an ORG directive as their first
  255. line anyway.  When assembling interactively, a colon ":" prompt rather than
  256. the  standard  arrow  ">"  will  be used.  The user should use the standard
  257. directive  END  to  terminate  assembly.  Each incarnation of the assembler
  258. clears  the  symbol  table  of  all  non-global  labels  (eg  assembler and
  259. disassembler-generated labels).
  260.  
  261. A  word  about  the assembler.  It follows the Zilog assembler specs pretty
  262. closely,  but with the following exceptions.  Zilog use appended letters to
  263. distinguish  their  numeric  bases,  and  I  use  prepended  characters  as
  264. previously  noted.   The  DEFW and DEFB directives should allow a series of
  265. comma-separated  values.  This doesn't fit into the parser nicely and until
  266. I  have  time  to  change  it,  DEFW  and  DEFB  directives accept a single
  267. parameter  only  eg  DEFW  100,13563 should be changed to DEFW 100 and DEFW
  268. 13563 on separate lines.  Sorry about that.  Also labels may only appear in
  269. the  first  column,  although  the  trailing colon is optional.  Who places
  270. labels  in  between  operands  anyway?  Macros are currently not supported.
  271. The  DEFL  directive  is not supported (use the EQU directive instead), the
  272. rather  pointless  DEFT directive is not supported (use DEFS instead).  The
  273. only   operators  allowed  in  expressions  are  -,+,/,*.   Most  of  these
  274. limitations will be lifted when I have more time.
  275.  
  276. The  assembler  is  single pass.  When it comes across a forward reference,
  277. space  is  left  in  the assembled code and the reference is entered into a
  278. table.   When  assembly  is  complete this table is resolved to fill in the
  279. bytes   with  the  proper  values.   Any  references  remaining  unresolved
  280. indicates  an  error.   These may be defined by the user and resolved using
  281. interactive  commands  (see later).  The assembler is not emulated but runs
  282. as  compiled  68000  code.   Therefore  the address space of the Z80 is not
  283. adulterated by having lots of utilities scattered around.
  284.  
  285. A  note  on  relative  jumps.   There  appear  to  be two standards used in
  286. relative jumps; that defined by Zilog and that used by other people.  Zilog
  287. state  that  a  relative jump is offset from the first byte of the relative
  288. jump instruction.  Others say that because the operand is modified by -2 to
  289. allow  for  the  twice  incremented  PC  after fetching the instruction and
  290. operand,  the  jump  is  offset  from the byte of the immediately following
  291. instruction.   In  other words, in Zilog terms JR 0 is an infinite loop but
  292. others  say  JR  0  does  nothing.  I have adopted the Zilog usage as it is
  293. likely to be more widespread.
  294.  
  295. DISASSEMBLE <START Address> [END Address] [FILE FileName] [NOLABELS]
  296.  
  297. Initiates diassembly of byte code to the equivalent mnemonics.  The address
  298. from  which  to start disassembling is required.  If the END address is not
  299. defined  then  the  whole 64K map will be disassembled.  The user may press
  300. Escape  or select the Abort menu item to stop disassembly at any point.  If
  301. the  FILE  parameter  is defined then output will be directed to this file,
  302. otherwise  output  will  go  to  the screen.  By default the disassembly is
  303. symbolic;  so  any symbols present in the symbol table will be used to make
  304. the  disassembly  more  meaningful.   This  can  be  disabled  by using the
  305. NOLABELS switch.
  306.  
  307. If the disassembler is run in symbolic mode, then any jump instruction will
  308. have  its  destination address calculated and a label for that address will
  309. be  generated.  If the address is before the current disassembly point then
  310. obviously the label will not previously have been displayed, so the address
  311. is  displayed.   If  the destination address is forward, then the generated
  312. label is used.  Subsequent runs of the disassembler will use the previously
  313. generated  labels to produce more symbolic output.  Disassembler labels are
  314. cleared  each  time  the  assembler  is  run,  however  the user may retain
  315. important labels by declaring them as GLOBAL.
  316.  
  317. Because it is very difficult to distinguish data from code in a Z80 system,
  318. disassembling  data  will  normally produce fairly sensible-looking results
  319. (sensible  that  is, until the code is examined).  However, occasionally an
  320. illegal  byte  code sequence will be produced (this will only happen in the
  321. case  of instructions extended by the hex CB, DD, ED and FD bytes.  When an
  322. illegal  sequence is detected, a DEFB directive of the first offending byte
  323. in  the  sequence  is output, and disassembly continues from the next byte.
  324. However  in  certain  cases  if required to disassemble an illegal extended
  325. instruction  for which there is an nonextended equivalent, the disassembler
  326. will  not  produce  an  error  but  will  output  the  nonextended  (legal)
  327. instruction.    For   example:   bytes  DD,60  has  no  legal  (documented)
  328. instruction,  but  the  disassembler will produce the mnemonic LD H,B which
  329. has   byte  code  60.   When  checking  my  assembler  and  disassembler  I
  330. reassembled  the  disassembly  of a 16K ROM and then disassembled it again.
  331. The  disassembled  output  was  identical  but the assembled code was three
  332. bytes  shorter  than  the  original!   This had me scratching my head for a
  333. while.   To  have the disassembler detecting these cases is an untidy piece
  334. of code on which I can't really justify spending time at the moment when it
  335. only   crops  up  when  you  disassemble  data  (or  possibly  undocumented
  336. instructions - see later).
  337.  
  338. CLEAR [ASSEMBLER | DISASSEMBLER | SYMBOLS | REFERENCES]
  339.  
  340. The  entries  in the symbol table have a source associated with them.  This
  341. command  allows  the  user to clear selected parts of the symbol table.  If
  342. the  parameter defined is SYMBOLS then the entire table is cleared.  If the
  343. user  defines  the  REFERENCES  parameter  then  the  table  of  unresolved
  344. references  is  cleared.   If no parameter is supplied then both the symbol
  345. table   and  unresolved  reference  table  are  cleared.   Defining  either
  346. ASSEMBLER or DISASSEMBLER causes the appropriate symbols to be cleared.
  347.  
  348. BASE [BINARY | OCTAL | DECIMAL | HEXADECIMAL]
  349.  
  350. This  command  defines  the  default  number  base  for  the  system.  If a
  351. parameter  is  defined  then  this  becomes  the  new  number  base and one
  352. immediately  noticeable  effect  of  this  is  that  the register window is
  353. updated  to  reflect  their  values  in  the  new base.  If no parameter is
  354. supplied  then a message is displayed telling you what the current base is.
  355. Alternatively  the  Settings  menu  contains  an item to control the system
  356. number base.
  357.  
  358. EVALUATE <VALUE Value>
  359.  
  360. This  provides a primitive calculator service.  The user enters a number or
  361. expression  as the parameter and the result is displayed in the four number
  362. bases.
  363.  
  364. LOAD <FILE FileName> <START Address>
  365.  
  366. This  command  loads  a  byte  image of the specified file to the specified
  367. emulated  address  space.  If the file is longer then memory then it simply
  368. rolls-over and overwrites earlier portions of the file.
  369.  
  370. SAVE <FILE FileName> <START Address> <END Address | LENGTH Length>
  371.  
  372. Well  surprise  of  surprise  this  command  saves  a chunk of the emulated
  373. address space as a byte image.  The last parameter specifies either the end
  374. address  or  the  number  of bytes to write.  If the keyword is not present
  375. then LENGTH is assumed.
  376.  
  377. COPY <SOURCE Address> <DESTINATION Address> <LENGTH Length>
  378.  
  379. This  copies a section of the emulated address space from the source to the
  380. destination addresses.
  381.  
  382. SHOW [ASSEMBLER | DISASSEMBLER | SYMBOLS | REFERENCES]
  383.  
  384. This  command  displays  the symbol table or unresolved references.  If the
  385. user specifies ASSEMBLER or DISASSEMBLER as the parameter then labels which
  386. were  generated  by  the  appropriate  module  are displayed, if SYMBOLS is
  387. defined   then  the  current  GLOBAL/EXTERNAL  symbols  are  displayed,  if
  388. REFERENCES is defined then the unresolved reference table is displayed.  If
  389. none  is  defined  then the symbol table is displayed in its entirety.  The
  390. number of entries in the symbol table is limited only by available memory.
  391.  
  392. INTERRUPTS [VALUE Value] [ON | OFF]
  393.  
  394. This  allows  the  user  to  control  the  system interrupts.  If the VALUE
  395. parameter  is  supplied  then  this  defines  the  number  of  Amiga  clock
  396. interrupts  to  receive  before  emulating  a Z80 interrupt.  If the ON/OFF
  397. parameter  is  defined then this controls overall system interrupts.  If it
  398. is  OFF  then the Z80 will never receive a clock interrupt.  Note that this
  399. has  nothing  to do with the status of interrupts on the Z80 (the IFF1 flag
  400. set  through  EI  and DI).  This is useful if you want to ignore interrupts
  401. completely  or are debugging a piece of code which involves interrupts, but
  402. you want to forget about them temporarily.  If no parameter is defined then
  403. the current settings are displayed.  Alternatively the interrupt status can
  404. be  changed  through  one  of  the Settings menu items.  Currently the only
  405. interrupts  supported  are  clock interrupts, as other devices have not yet
  406. been  implemented.   The  interrupt  system  should be identical to the Z80
  407. system;   allowing   for   all  three  interrupt  modes  and  maskable  and
  408. non-maskable  interrupts,  and priority interrupts.  I don't take advantage
  409. of this flexibility and completeness, but it will come into its own later.
  410.  
  411. EMULATE [8080 | Z80 | Zaphod]
  412.  
  413. This  command  defines  which system to emulate.  The Intel 8080 is not yet
  414. supported  (apart  from  the  downwards  compatability  of the Z80).  It is
  415. intended  to  add  assembly  and disassembly using the different opcodes of
  416. this  processor at some later stage.  Emulating a Z80 gives the user access
  417. to  the  CPU  and memory, but no I/O facilities.  Interrupts in this system
  418. respond by placing a value on the data bus (ie typically Interrupt Mode 0),
  419. which  is normally an RST instruction which defines the restart address for
  420. interrupt handling.
  421.  
  422. RUN [START Address] [TRACE] [SINGLESTEP]
  423.  
  424. Initiates  emulation at the specified address, or the current PC if none is
  425. defined.   Note  that  no commands affect the register contents, so that if
  426. the  user  breaks  out  of  emulation  and changes some contents of memory,
  427. simply  typing  RUN  will resume from where the emulation left off.  If the
  428. TRACE  parameter  is  defined  then the register contents are updated after
  429. every  instruction  (and dramatically reducing performance in the process).
  430. The  SINGLESTEP  parameter  defines that only a single instruction is to be
  431. emulated.  Useful in combination with the hot key for examining programs at
  432. a  pretty  low  level.  The user may press Escape at any stage to abort the
  433. emulation.
  434.  
  435. DEBUG [ON | OFF]
  436.  
  437. This  command  is a switch which causes some other commands to produce more
  438. output.   If  debugging is on the the assembler displays each line as it is
  439. processed,  together  with  the  bytes  to  which  it  was  assembled,  the
  440. disassembler  displays similar information (eg bytewise as well as mnemonic
  441. output),  and  the emulator will display the mnemonic of the instruction it
  442. is   currently   emulating  in  the  Ex:   box  in  the  register  display.
  443. Singlestepping   through   code   with   debugging  enabled  can  be  quite
  444. enlightening.   If  no  parameter  is defined then this command reports the
  445. current status, which may also be affected via one of the Settings menus.
  446.  
  447. DUMP <START Address> [END Address] [BINARY | OCTAL | DECIMAL | HEXADECIMAL]
  448.  
  449. Back  to  something a bit more simple.  A simple memory dump from the start
  450. address to the end address (if defined), or until the user presses Escape.
  451.  
  452. INITIALISE <START Address> <END Address | LENGTH Address> [VALUE Value]
  453.  
  454. Sets  every byte between the lower and upper bounds to the specified value.
  455. If the VALUE is not defined then 0 is assumed.
  456.  
  457. RESOLVE [REFERENCES]
  458.  
  459. This  explicitly tells the system to attempt to resolve any references that
  460. remain  unresolved after assembly.  Normally the user would define a couple
  461. labels or would assemble some other file with GLOBAL/EXTERNAL references to
  462. give  values  to these symbols.  The parameter may be defined for syntactic
  463. sugar but is not required.  The number of refernences that may be stored is
  464. limited only by available memory.
  465.  
  466. ROM [START Address] [END Address] [ON | OFF]
  467.  
  468. Declares the section of memory as Read Only.  Any updates inside this range
  469. will  have  no  effect.  The ROM may be enabled or disabled by the optional
  470. third  parameter.   If  the  START or END parameters are undefined then the
  471. current  values  are used.  Only one memory range can be defined as ROM for
  472. simplicity.
  473.  
  474. SCRIPT <FILE FileName>
  475.  
  476. Executes  the  script  with the specified name.  The script may contain any
  477. commands  or  instructions  that can be used in the system.  Therefore, you
  478. can  have  a  script  which  does  a whole bunch of things to customise the
  479. environment to your personal preferences.  On startup, the system looks for
  480. scripts  with the name Startup-Z80 in the current directory and in S:.  The
  481. scripts may even call other scripts if you want, get them to assemble files
  482. or  anything else you can normally do.  There is currently a built-in limit
  483. of 8 nested scripts.
  484.  
  485. ABOUT
  486.  
  487. Not particularly useful, just declares the author and version number.
  488.  
  489. HELP [MNEMONIC Mnemonic]
  490.  
  491. If  used  without  a  parameter  then the database of command structures is
  492. analysed  and  the  result displayed on the screen.  The assembler needs to
  493. store  a  database of valid commands for use when syntax checking, and this
  494. may  be interrogated to display valid operands for an instruction mnemonic.
  495. If you are unsure of the operand format for an instruction, or need to know
  496. which  instructions  are supported (eg undocumented instructions) then this
  497. database  will tell you as it is the same as used by the syntax checker and
  498. assembler.   Typing  HELP LD will give you all supported formats for the LD
  499. instruction.  In the list a 'r' denotes a single register, an 'n' denotes a
  500. single byte operand, 'nn' a word and 'd' a displacement byte.
  501.  
  502. QUIT
  503.  
  504. Exits the program.
  505.  
  506. In  addition  the  user  may type any valid instruction at the prompt which
  507. will  be  immediately emulated.  The instructions will have all the correct
  508. effects,  but  be  aware  that doing something like JP 1000 will do nothing
  509. more  than  set the PC to 1000 (ie the routine at 1000 will not continue to
  510. be  processed).  Of course following this up by singlestepping will achieve
  511. this.
  512.  
  513.  
  514. More about that Zaphod thing:
  515.  
  516. Having  a  Z80  is  all  very  well  but it's usually pretty nice to have a
  517. machine  to  do  something  with.  The modular approach to the system means
  518. that  new  machine  emulations  can  be added very quickly and easily, once
  519. their  specs  are  known.  If you have a pet system which you'd like to see
  520. incorporated  into the program then you need to do two things:  1.  Provide
  521. me with the specifications of the system, including such things as to which
  522. INput/OUTput  ports  it  responds  (which  port,  which  value),  interrupt
  523. behaviour,  memory mapping and anything else relevant, and 2.  wait a while
  524. (not  too  long  I  hope  but  I  can't guarantee anything).  I will try to
  525. incorporate requirements as best I can.
  526.  
  527. The Zaphod-1 emulation is a pretty simple emulation as it currently stands,
  528. mainly because I have done very little work on it.  When I devote more time
  529. to  it,  it  will gradually migrate towards responding to the complete VT52
  530. escape  sequences,  and  will  allow a greater range of device interaction,
  531. such as disk I/O.  If you request the Zaphod-1 mode, then a new medium-res,
  532. two-colour,  24x80 screen will be opened in the background.  When emulation
  533. is running, this console screen will be brought to the foreground, and when
  534. emulation is complete it will retire quietly into the background once more.
  535. This  screen  has  invisible gadgets in the top right corner.  The user may
  536. interact  with  the  console using standard IN and OUT instructions.  An IN
  537. from  port 0 will respond with the ASCII code of the last key pressed, or 0
  538. if none available.  An OUT to port 1 will display the appropriate character
  539. on  the  console  screen.   The  following  escape  sequences are currently
  540. supported:
  541.  
  542. ESC 0     - clears the console screen
  543. ESC 1 x y - moves the cursor position to x,y
  544. ESC 2     - clears to end of line from the current position
  545.  
  546. A quick code stub illustrates how to talk to Zaphod:
  547.  
  548.         ld c,1      ; Port 1 for output to console screen
  549.         ld a,27     ; ESC 0 clears the screen
  550.         out (C),a
  551.         ld a,0
  552.         out (c),a
  553.         ld c,0      ; Port 0 for input from the keyboard
  554. loop:   in a,(c)    ; IN with the C Indirect sets the flags
  555.         jr z,loop   ; make sure we have some input
  556.         out (1),a   ; output the result
  557.         jr loop     ; carry on
  558.  
  559. which  provides  a  simple teletype; whatever is typed at the keyboard will
  560. appear on the screen.  At some later stage I may write an OS around Zaphod;
  561. more likely he will develop into an emulation of some other system.
  562.  
  563. Undocumented Instructions:
  564.  
  565. The  basis  for the emulator has been the Z80 Assembly Language Programming
  566. Manual  written  by  Zilog.   I have therefore not implemented undocumented
  567. instructions  as, not surprisingly, I have no details on them.  There is no
  568. reason  why they shouldn't be implemented, however I will need the complete
  569. details first ie which flags are affected, any side effects.  I am aware of
  570. the  following  undocumented instructions, but would appreciate any further
  571. details on them or other instructions which are out there:
  572.  
  573. 1 - The byte Index register LD instructions.
  574.  
  575. Any  LD instruction which refers to the H or L registers may be prefixed by
  576. the  standard  index  extension  bytes DD and FD in which case instructions
  577. referring  to  H  will have be replaced by the high byte of the appropriate
  578. index  pair,  and  those  for  L by the low byte.  Other instructions which
  579. follow  this  pattern are:  ADC, ADD, AND, CP, DEC, INC, OR, SBC, SUB, XOR.
  580. If   anyone   can   give  me  suitably  acceptable  terminology  for  these
  581. instructions  (eg  LD  HX,B  for  load  High  byte  of  index into B) I may
  582. incorporate them.
  583.  
  584. 2 - The badly implemented Shifts and Rotates.
  585.  
  586. Analysis of the operand codes after the standard extend byte CB reveals the
  587. following pattern:
  588.  
  589. 00-07 RLC r     08-0F RRC r
  590. 10-17 RL r      18-1F RR r
  591. 20-27 SLA r     28-2F SRA r
  592. 30-37 ?????     38-3F SRL r
  593.  
  594. It  would  be  logical  to  assume  that  the  codes  30-37 should be SLL r
  595. instructions.   I  seem  to  remember  from a very long time ago that Zilog
  596. didn't  get  this quite right and got one of the shifted bits wrong, or the
  597. Carry  or something, and so didn't document the (quite predictable) effects
  598. of  the instructions as they didn't fall into the pattern.  Again, if I get
  599. some more details on these they may find their way into the emulator.
  600.  
  601. If there are other undocumented instructions out there, please let me know.
  602.  
  603. Future Developments for the program:
  604.  
  605. Well I have a list as long as my arm. I shall try to tackle these in turn.
  606.  
  607. - More complete machine emulations. Not very hard provided I get complete
  608.   specs. The Sinclair Spectrum may well be incorporated pretty soon, for
  609.   nostalgia's sake. I'd love to get hold of a ZX81 (or even better ZX80)
  610.   for the ultimate timewarp as this would take me back to my early days
  611.   in computing. I can still remember 3D Monster Maze, utter classic!
  612.  
  613. - Device handling, providing access to Amiga devices as a bridge through
  614.   the Z80 emulated systems. This may depend on the other emulations
  615.   above, but Zaphod may grow into a killer Z80 machine.
  616.  
  617. - Undocumented instructions. Again, I need the specs.
  618.  
  619. - Support for 8080. Not too hard, just some alternative mnemonics.
  620.  
  621. - Support for other processors. Surprisingly, quite simple. The modularity
  622.   of the code means that the Z80 stuff is encapsulated in the disassembler,
  623.   assembler and emulator modules. I could write similar modules for other
  624.   processors (and after the Z80 would probably be simpler!) and the rest of
  625.   the system would remain untouched. Let me know if such is required. Such
  626.   modules as the expression handler, symbol table, reference table and
  627.   others could be reused with no change.
  628.  
  629. - CP/M support. Obviously this might be an integral part of a more complete
  630.   machine emulation. I don't have much experience of CP/M and so this may
  631.   take some investigation. I don't even know where I could get a copy.
  632.  
  633. The  above  are  really  additions to the emulation menu; I can see it now:
  634. select  your  system  from  8080,6502,6809,Z80,Zaphod,ZX81,Spectrum,CP/M...
  635. These  are  not however the driving point behind the emulator.  My research
  636. will follow the following lines:
  637.  
  638. - Currently the emulator reads some bytes from store, disassembles them and
  639.   emulates the result (a simple/traditional interpretive cycle). A clear
  640.   optimisation is to exhaustively run through the possible traces of a
  641.   program execution, creating a list of pre-parsed abstract instructions in
  642.   memory. These can then be emulated at far greater speed. Given that I
  643.   already have the code to generate abstract instructions, and code to
  644.   emulate them, all that is required is a routine to build the list. Not
  645.   too hard and won't take more than a day when I find the time. Then, for
  646.   the outlay of memory (each abstract instruction takes 23 bytes) there
  647.   will be very much increased program speed. Of course, there may be a
  648.   problem with self-modifying programs.
  649.  
  650. - Having built the abstract instruction list I shall translate these into
  651.   equivalent high-level language statements (similar to the ones used in
  652.   my emulator code). These can then be compiled and linked to a runtime
  653.   module containing the emulation of the Z80 registers and an abstract
  654.   machine to produce a standalone program which should execute at pretty
  655.   high speed on the host computer.
  656.  
  657. - The final level will be to produce actual assembler source from the
  658.   instruction list. In this case 68000 registers rather than high level
  659.   structures will be used, and the final code when assembled and linked
  660.   should really cook.
  661.  
  662. Of  course I cannot guarantee that any or all of these will be implemented.
  663. At  this stage my ambition is to get a decent grade for my project.  I have
  664. spent  about  two  weeks  coding  so far, and will have a further six weeks
  665. after  my  finals  with no other distractions (well, maybe one or two!).  I
  666. hope  to  go  a long way then with the project, and after that...who knows?
  667.  
  668. Please don't hesitate to inform me of any bugs which you locate or suspect.
  669.  
  670. I  have  always  erred on the side of trust in my programs when it comes to
  671. testing.   Doing exhaustive tests is not my idea of fun, although of course
  672. I  do  check  changes  as  they are made but perfect QA...I don't think so.
  673. Therefore,  I  urge  you  not to be hesitant in suspecting invertebrates if
  674. things  don't  happen  as  expected.   I'll go as far as to say I have fair
  675. confidence  in  the  assembler  and disassembler; they managed to produce a
  676. verbatim  copy  of  a  16K  ROM  after  reassembling  the  output  from the
  677. disassembler.   I'm  afraid  the jury is still out on the emulator; in fact
  678. the  counsel  for  the  defence is still preparing his case.  I tested each
  679. instruction  as  it  was  implemented  to  check I was getting the expected
  680. results,  but  there  may  be  some obscure ones which don't behave as they
  681. oughta.
  682.  
  683. I  can  be  reached  as  cpb@doc.ic.ac.uk and I tend to monitor most of the
  684. Amiga-related  Usenet groups, but I shall shortly be going into hibernation
  685. to  study  for  my finals.  Please bear with me if my response is less than
  686. immediate.  I will do my best!
  687.  
  688. In particular this program owes itself to:
  689.  
  690. - Benchmark Modula 2 (Thanks Leon!), without which I would have had made
  691.   very many type incompatabilities.
  692.  
  693. - CygnusEd Professional, without which the development would have taken a
  694.   lot longer.
  695.  
  696. - Tangerine Dream, REM, Gianna Nannini and various others without whom my
  697.   head would have probably exploded during those late night sessions.
  698.  
  699. - Valeria and Pappagalla for keeping me amused from a distance.
  700.  
  701. - Bonnington for keeping me company late at night (this is my cat).
  702.  
  703. - Grolsch lager for lubricating my thought processes.
  704.  
  705. - Apple and cream pastry slices for maintaining my blood sugar levels.
  706.  
  707. - and of course the Commodore Amiga for being the best damn personal
  708.   computer available, without which I would have had to develop at
  709.   University, something not particularly high up on my list of enjoyable
  710.   evening pastimes!
  711.  
  712. Release history:
  713.  
  714. V1.00ß - 6th March 1992.
  715. Initial release.
  716.  
  717. Phil Brown.
  718.