?login_element?

Subversion Repositories NedoOS

Rev

Rev 1469 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1.         DEVICE ZXSPECTRUM1024
  2.           include "in_zxzvm.asm"
  3.  
  4.         macro SYSERR
  5.         jp      syserr
  6.         ;call syserr
  7.         endm
  8.  
  9.         macro NC_SYSERR
  10.         jp      nc,syserr
  11.         ;call nc,syserr
  12.         endm
  13.  
  14.         macro NZ_SYSERR
  15.         jp      nz,syserr
  16.         ;call nz,syserr
  17.         endm
  18.  
  19. peek64  equ     ZXPK64
  20.  
  21.  
  22.         org     7000h   ;Program Segment Prefix (aka Zero Page). Those
  23. begin
  24.         jp      main    ;things are so useful!
  25. zinum:  defb    7       ;<< v0.02 >> Pretend to be a C128
  26.  
  27. param   equ     705Ch   ;Where the FCB parameter would be.
  28.  
  29.         org     7100h
  30. main:   ld      (isp),sp        ;Save BASIC's SP
  31.         ld      hl,0
  32.         ld      (cycles),hl
  33.         ld      de,param
  34.         call    ZXINIT  ;Initialise the I/O code
  35.         NC_SYSERR;jp    nc,syserr
  36.         call    ZXVER
  37.         ld      hl,vererr
  38.         cp      VMVER
  39.         NZ_SYSERR;jp    nz,syserr
  40.         call    ZXIHDR
  41.         NC_SYSERR;jp    nc,syserr
  42.         call    init_hdr
  43.         NC_SYSERR;jp    nc,syserr
  44.         call    init_scr
  45.         NC_SYSERR;jp    nc,syserr
  46.         call    init_rnd
  47.         NC_SYSERR;jp    nc,syserr
  48.         call    init_stack
  49.         NC_SYSERR;jp    nc,syserr
  50.         call    test_mem        ;<< v0.02 Check we can write Z-memory
  51.         NC_SYSERR;jp    nc,syserr       ;>> v0.02
  52.         ld      a,1
  53.         ld      (running),a
  54.         call    showpc
  55. zloop:  call    zinst
  56.         push    hl              ;<< v0.02
  57.         push    af
  58.         ld      hl,(cycles)
  59.         inc     hl
  60.         ld      (cycles),hl     ;CALL ZXRCPU once every 2048 z-cycles.
  61.         ld      a,h
  62.         cp      8
  63.         jr      c,zlp0
  64.         ld      hl,0
  65.         ld      (cycles),hl
  66.         call    ZXRCPU
  67. zlp0:                           ; v0.02 >>
  68. zlp1:   pop     af
  69.         pop     hl
  70.  
  71.         call    showpc
  72.         jp      nc,zmstop
  73.         ld      a,(running)     ;Running = 1 to continue
  74.         dec     a               ;        = 0 to quit
  75.         jr      z,zloop         ;        = 2 to restart
  76.         inc     a
  77.         jr      z,zmexit
  78.         cp      2
  79.         jr      z,zmreset
  80.         jp      stub
  81. ;
  82. zmstop: push    hl
  83.         call    flush_buf
  84.         call    showstk
  85.         ld      de,anykey
  86.         ld      c,9
  87.         call    ZXFDOS
  88.         ld      c,6
  89.         ld      e,0FDh
  90.         call    ZXFDOS
  91.         pop     hl
  92.         SYSERR;jp       syserr
  93. ;
  94. zmexit: call    flush_buf
  95.         ld      de,anykey
  96.         ld      c,9
  97.         call    ZXFDOS
  98.         ld      c,6
  99.         ld      e,0FDh
  100.         call    ZXFDOS
  101.         scf
  102.         call    ZXEXIT
  103.         ld      sp,(isp)
  104.         ret                     ;<< v0.02 >> Spectrum-specific code removed
  105. ;                               ;from ZXZVM
  106. zmreset:
  107.         call    ZXRST           ;Restart
  108.         NC_SYSERR;jp    nc,syserr
  109.         call    ZXIHDR
  110.         NC_SYSERR;jp    nc,syserr
  111.         call    init_hdr
  112.         NC_SYSERR;jp    nc,syserr
  113.         call    init_scr
  114.         NC_SYSERR;jp    nc,syserr
  115.         call    init_rnd
  116.         NC_SYSERR;jp    nc,syserr
  117.         call    init_stack
  118.         NC_SYSERR;jp    nc,syserr
  119.         ld      a,1
  120.         ld      (running),a
  121.         jp      zloop
  122.  
  123. stub:   ld      a,8
  124.         ld      (5C3Ah),a
  125.         ld      hl,msg1
  126. syserr: xor     a
  127.         call    ZXEXIT
  128.        ;pop hl
  129.        ;rst 0 ;QUIT
  130.        ;nop
  131.        
  132. ;
  133. ;ZXEXIT should not return if called with Carry reset. But in case it does,
  134. ;      reboot!
  135. ;
  136.         rst     0
  137.         di
  138.         halt
  139. ;
  140. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  141. ;
  142. ;Numerical data
  143. ;
  144. cycles: defw    0       ;No. of Z-machine cycles executed modulo 2048
  145. isp:    defw    0       ;BASIC's SP
  146. zsp:    defw    0       ;Z-machine stack pointer
  147. zstop:  defw    0       ;Top of Z-machine stack
  148. rsp:    defw    0       ;Routine stack pointer
  149. rstop:  defw    0       ;Top of routine stack
  150. zpc:    defw    0,0     ;Z-machine program counter
  151. zipc:   defw    0,0     ;Address of last opcode (as opposed to data)
  152. zver:   defb    0       ;Version of the Z-Machine
  153. inst:   defw    0       ;Current instruction
  154. running:
  155.         defb    0       ;SET to 1 while the Z-machine is running
  156. ;
  157. ;Some strings
  158. ;
  159. msg1:   defb    '9 Stub encountere'
  160.         defb    0E4h            ;'d'+80h
  161. ;
  162. vererr: defb    'A Version mismatc'
  163.         defb    0E8h            ;'h'+80h
  164. ;
  165. memerr: defb    '4 Memory test faile'   ;<< v0.02
  166.         defb    0E4h            ;'d'+80h;>> v0.02
  167. ;
  168. anykey: defb    13,10,'Press SPACE to finish',13,10,'$'
  169. ;
  170. zvbad:  defb    'A Story type '
  171. zvbuf:  defb    '000'
  172. ;
  173. ;;;;;;;;;;;;;;;;;;; SET up the header ;;;;;;;;;;;;;;;;;;;;;
  174. ;
  175. init_hdr:
  176.         ld      e,0
  177.         ld      hl,0
  178.         call    ZXPEEK          ;Get Z-file version
  179.         ld      (zver),a
  180.         ld      de,zvbuf
  181.         ld      l,a
  182.         ld      h,0             ;CREATE the "invalid version" error
  183.         call    spdec3
  184.         ex      de,hl
  185.         dec     hl
  186.         set     7,(hl)          ;Fatal error, so set bit 7 of last char
  187.         ld      hl,zvbad
  188.         ld      a,(zver)        ;v5 only?
  189.         cp      8
  190.         jr      z,verok
  191.         cp      3       ;<< v0.04 v3 support
  192.         jr      z,verok ;>> v0.04
  193.         cp      4       ;<< v1.10 v4 support
  194.         jr      z,verok ;>> v1.10
  195.         cp      5
  196.         jp      nz,ihdr8
  197. verok:  ld      hl,001Eh        ;Interpreter no.
  198.         ld      a,(zinum)       ;The interpreter number we pretend to be
  199.         call    ZXPOKE
  200.         ld      hl,001Fh
  201.         ld      a,'I'           ;<< v1.03 >> Our release number
  202.         call    ZXPOKE
  203. ;
  204. ;Get g_addr, address of globals table; also strings & routines
  205. ;
  206.         ld      e,0
  207.         ld      hl,08h
  208.         call    ZXPKWI
  209.         ld      (d_addr),bc     ;Dictionary
  210.         call    ZXPKWI
  211.         ld      (obj_addr),bc
  212.         call    ZXPKWI
  213.         ld      (g_addr),bc
  214. ;
  215.         ld      hl,28h
  216.         call    ZXPKWI
  217.         ld      (r_offs),bc
  218.         call    ZXPKWI
  219.         ld      (s_offs),bc
  220. ;
  221. ;Work out which upack_addr to use
  222. ;
  223.         ld      a,(zver)
  224.         ld      l,a
  225.         ld      h,0
  226.         add     hl,hl
  227.         ld      de,upack_table
  228.         add     hl,de
  229.         ld      e,(hl)
  230.         inc     hl
  231.         ld      d,(hl)
  232.         ld      (upack_addr+1),de
  233. ;
  234. ;Compute property sizes
  235. ;
  236.         ld      a,(zver)
  237.         cp      4
  238.         jr      c,psv3
  239.         ld      a,03Fh
  240.         ld      (psmask),a
  241.         ld      (pnmask),a
  242.         ld      hl,14
  243.         ld      (objlen),hl
  244.         ld      hl,126
  245.         ld      (ptlen),hl
  246.         jr      psv4
  247.  
  248. psv3:   ld      a,0E0h
  249.         ld      (psmask),a
  250.         ld      a,01Fh
  251.         ld      (pnmask),a
  252.         ld      hl,9
  253.         ld      (objlen),hl
  254.         ld      hl,62
  255.         ld      (ptlen),hl
  256. psv4:   scf
  257.         ret
  258. ;
  259. ihdr8:  and     a
  260.         ret
  261. ;
  262. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  263. ;
  264. init_scr:
  265.         ld      a,-1    ;erase_window(-1)
  266.         call    ZXERAW
  267.         xor     a
  268.         ld      (abbrev),a
  269.         ld      (alpha),a
  270.         ld      (dalpha),a
  271.         ld      (shift),a
  272.         ld      (multi),a
  273.         ld      (timer),a
  274.         call    inibuf  ;Initialise I/O buffering
  275.         scf
  276.         ret
  277. ;
  278. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  279. ;
  280. ;Initialise the random number generator
  281. ;
  282. init_rnd:
  283.         xor     a
  284.         ld      (rmode),a
  285.         ld      hl,0
  286.         call    random  ;Seed the generator
  287.         scf
  288.         ret
  289. ;
  290. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  291. ;
  292. ;Stack frame format (little-endian):
  293. ;
  294. ;+0:  DD pc             ;Previous ZPC
  295. ;+4:  DS 30             ;Local variables
  296. ;+34: DB call type      ;0=function, 1=procedure, 2=interrupt
  297. ;+35: DB pcount         ;Count of parameters
  298. ;+36: DW rsp            ;Routine stack pointer
  299. ;
  300. init_stack:
  301.         call    ZXTMEM
  302.         inc     hl              ;1st unusable byte
  303.         ld      (zsp),hl
  304.         ld      (zstop),hl
  305.         ld      de,-2048        ;2k call stack
  306.         add     hl,de
  307.         ld      (rsp),hl        ;Routine stack
  308.         ld      (rstop),hl
  309.         call    mkframe         ;Returns HL->frame
  310.         ret     nc
  311.         push    hl
  312.         pop     ix              ;IX -> frame
  313.         ld      de,(rsp)
  314.         ld      (ix+36),e
  315.         ld      (ix+37),d       ;Routine stack pointer
  316.         ld      (ix+34),1       ;Procedure
  317.         ld      (ix+35),0       ;No local variables
  318.         ld      hl,6
  319.         ld      e,0             ;00000006
  320.         call    ZXPKWD          ;PC high
  321.         ld      (zpc),bc
  322.         ld      bc,0
  323.         ld      (zpc+2),bc
  324.         scf
  325.         ret
  326. ;
  327. mkframe:
  328.         push    de
  329.         ld      hl,(zsp)
  330.         ld      de,-38          ;Frame size
  331.         add     hl,de
  332.         ld      (zsp),hl
  333.         push    hl
  334.         ld      de,(rstop)      ;CALL stack hits routine stack?
  335.         and     a
  336.         sbc     hl,de
  337.         pop     hl
  338.         ccf
  339.         jp      nc,spfail1              ;Stack overflow!
  340.         push    hl
  341.         ld      e,38
  342. mframe1:
  343.         ld      (hl),0          ;Initialise the frame to zeroes.
  344.         inc     hl
  345.         dec     e
  346.         jr      nz,mframe1
  347.         pop     hl
  348.         pop     de
  349.         ret
  350. ;
  351. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  352. ;
  353. zinst:  ld      hl,(zpc)
  354.         ld      (zipc),hl       ;Last opcode location
  355.         ld      hl,(zpc+2)
  356.         ld      (zipc+2),hl
  357.         call    zpcipeek                ;Get instruction byte
  358.         ret     nc
  359.         ld      (inst),a
  360.         cp      0FAh
  361.         jp      z,v2_inst               ;8-operand VAR
  362.         cp      0ECh
  363.         jp      z,v2_inst
  364.         cp      0E0h
  365.         jp      nc,var_inst
  366.         cp      0C0h
  367.         jp      nc,op2_vainst
  368.         cp      0BEh
  369.         jp      z,ext_inst
  370.         cp      0B0h
  371.         jp      nc,op0_inst
  372.         cp      080h
  373.         jp      nc,op1_inst
  374. ;
  375. ;It's a 2OP.
  376. ;
  377.         call    parse_2op       ;This decodes the parameters.
  378.         ld      a,2
  379.         ld      (v_argc),a
  380. op2_main:
  381.         ld      a,(inst)
  382.         and     1Fh
  383.         ld      de,two_ops
  384.         jr      dispatch
  385. ;
  386. op2_vainst:
  387.         call    zpcipeek        ;Operand types
  388.         ret     nc
  389.         call    parse_var       ;2OP with VAR parameters
  390.         jr      op2_main
  391. ;
  392. op1_inst:
  393.         ld      a,1             ;<< v1.00  SET argument count correctly
  394.         ld      (v_argc),a      ;>> v1.00
  395.         call    parse_1op
  396.         ld      a,(inst)
  397.         and     0Fh
  398.         ld      de,one_ops
  399.         jr      dispatch
  400. ;
  401. op0_inst:
  402.         xor     a               ;<< v1.00 SET argument count correctly
  403.         ld      (v_argc),a      ;>> v1.00
  404.         ld      a,(inst)
  405.         and     0Fh
  406.         ld      de,zero_ops
  407.         jr      dispatch
  408. ;
  409. ext_inst:
  410.         ld      a,(zver)        ;Aren't allowed extended opcodes in v1-v4
  411.         cp      5
  412.         jp      c,fail
  413.         call    zpcipeek        ;Get real opcode
  414.         ret     nc
  415.         ld      (inst+1),a
  416.         call    zpcipeek        ;Operand types
  417.         ret     nc
  418.         call    parse_var
  419.         ld      a,(inst+1)
  420.         bit     7,a
  421.         jp      nz,ext_high
  422.         ld      de,ext_ops
  423.         cp      MAXEXT          ;IN range?
  424.         jr      c,dispatch
  425.         jr      z,dispatch
  426.         jp      fail
  427.  
  428. v2_inst:
  429.         call    zpcipeek
  430.         ret     nc
  431.         ld      b,a
  432.         call    zpcipeek
  433.         ret     nc
  434.         ld      c,a
  435.         call    parse_v2
  436.         jr      var_main
  437. ;
  438. var_inst:
  439.         call    zpcipeek        ;Operand types
  440.         ret     nc
  441.         call    parse_var       ;Count in argc, args in arg1-arg4
  442. var_main:
  443.         ld      a,(inst)
  444.         and     1Fh
  445.         ld      de,var_ops
  446. dispatch:
  447.         ld      l,a
  448.         ld      h,0
  449.         add     hl,hl
  450.         add     hl,de
  451.         ld      e,(hl)
  452.         inc     hl
  453.         ld      d,(hl)
  454.         ex      de,hl
  455.         jp      (hl)
  456. ;
  457. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  458. ;
  459. ;Utility code to read the Z-machine's memory
  460. ;
  461. ipeek:  call    ZXPEEK
  462.         push    af
  463.         inc     hl
  464.         ld      a,h
  465.         or      l
  466.         jr      nz,ipeek1
  467.         inc     e
  468. ipeek1: pop     af
  469.         ret
  470. ;
  471. zpcipeek:
  472.         push    hl
  473.         push    de
  474.         ld      hl,(zpc)
  475.         ld      de,(zpc+2)
  476.         call    ipeek
  477.         ld      (zpc),hl
  478.         ld      (zpc+2),de
  479.         pop     de
  480.         pop     hl
  481.         ret
  482. ;
  483. zpcpeek:
  484.         push    hl
  485.         push    de
  486.         ld      hl,(zpc)
  487.         ld      de,(zpc+2)
  488.         call    ZXPEEK
  489.         pop     de
  490.         pop     hl
  491.         ret
  492. ;
  493. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  494. ;
  495. ; << v0.02 Check that Z-machine memory is writable. IF not, then
  496. ;          we can't run.
  497. ;
  498. test_mem:
  499.         ld      hl,40h
  500.         call    ZXPK64
  501.         ld      d,a     ;Correct value
  502.         cpl
  503.         ld      e,a     ;Different value
  504.         call    ZXPOKE
  505.         call    ZXPK64
  506.         cp      e       ;Has the change registered?
  507.         jr      nz,tm_fail
  508.         ld      a,d     ;Change back to the correct value.
  509.         call    ZXPOKE
  510.         call    ZXPK64
  511.         cp      d       ;Has the change back registered?
  512.         jr      nz,tm_fail
  513.         scf
  514.         ret
  515. ;
  516. tm_fail:
  517.         ld      hl,memerr
  518.         xor     a
  519.         ret
  520.  
  521. ; >> v0.02
  522. ;
  523. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  524. ;
  525. ;Other source files
  526. ;
  527.         include "vmdebug.asm"     ;Debugging ops
  528.         include "vmvar.asm"       ;Access to variables & operand decoding
  529.         include "vm0ops.asm"      ;0OP: operations
  530.         include "vm1ops.asm"      ;1OP: operations
  531.         include "vm2ops.asm"      ;2OP: operations
  532.         include "vmvops.asm"      ;VAR: operations
  533.         include "vmeops.asm"      ;EXT: operations
  534.         include "vmarith.asm"     ;Arithmetic operations
  535.         include "vmobj.asm"       ;Object operations
  536.         include "vmprop.asm"      ;Property operations
  537.         include "vmzchar.asm"     ;I/O and buffering
  538.         include "vmdict.asm"      ;Dictionary oprations
  539.         include "in_wrhex.asm"    ;Output hex and decimal numbers
  540. end
  541.  
  542.         savebin "vm.bin",begin,end-begin
  543.  
  544.         ;LABELSLIST "../../us/user.l"
  545.