home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / emulate / systems / apple / cartrige.asm < prev    next >
Assembly Source File  |  1990-04-17  |  14KB  |  415 lines

  1.     Page    58,132
  2.     Title    CARTRIGE.ASM    Cartridge Routines
  3. ;******************************************************************************
  4. ;
  5. ;   Name:    CARTRIGE.ASM    Cartridge Routines
  6. ;
  7. ;   Group:    Emulator
  8. ;
  9. ;   Revision:    1.00
  10. ;
  11. ;   Date:    January 30, 1988
  12. ;
  13. ;   Author:    Randy W. Spurlock
  14. ;
  15. ;******************************************************************************
  16. ;
  17. ;  Module Functional Description:
  18. ;
  19. ;        This module contains all the code for the Apple
  20. ;    cartridge.
  21. ;
  22. ;******************************************************************************
  23. ;
  24. ;  Changes:
  25. ;
  26. ;    DATE     REVISION                DESCRIPTION
  27. ;  --------   --------    -------------------------------------------------------
  28. ;   1/30/88    1.00    Original
  29. ;
  30. ;******************************************************************************
  31.     Page
  32. ;
  33. ;  Public Declarations
  34. ;
  35.     Public    Cart_ID         ; Cartridge ID string
  36.     Public    Cart_Init        ; Cartridge initialization routine
  37.     Public    Cart_Ctrl        ; Cartridge control routine
  38.     Public    Cart_Rd         ; Cartridge read routine
  39.     Public    Cart_Wrt        ; Cartridge write routine
  40.     Public    Cart_Mem_Rd        ; Cartridge memory read routine
  41.     Public    Cart_Mem_Wrt        ; Cartridge memory write routine
  42.     Public    Cart_Exp_Rd        ; Cartridge expansion read routine
  43.     Public    Cart_Exp_Wrt        ; Cartridge expansion write routine
  44.     Public    Cart_Data        ; Cartridge data segment pointers
  45. ;
  46. ;  External Declarations
  47. ;
  48.     Extrn    Slot_Address:Near    ; Get expansion slot address   (DEVICE)
  49.     Extrn    Error:Near        ; Apple emulator error routine    (APPLE)
  50.     Extrn    Exit:Near        ; Apple emulator exit routine    (APPLE)
  51.     Extrn    ERR_NO_MEMORY:Abs    ; Not enough memory error code     (DATA)
  52. ;
  53. ;  LOCAL Equates
  54. ;
  55. CART_SIZE    Equ    0805h        ; Cartridge memory size (32K)
  56. CART_MAX    Equ    8000h        ; Maximum cartridge size (32k Bytes)
  57. AREA_MAX    Equ    0400h        ; Maximum area size (2k/1k Bytes/Words)
  58. AREA_START    Equ    0C800h        ; Area starting address
  59. AREA_SHIFT    Equ    03h        ; Area shift count value
  60. CTRL_INIT    equ    0FFh        ; Cartridge control initialization
  61. CONTROL_MASK    Equ    0Fh        ; Cartridge control bits mask
  62. ASCII_CONVERT    Equ    30h        ; ASCII conversion value (Slot)
  63. ;
  64. ;  Define any include files needed
  65. ;
  66.     Include     Macros.inc    ; Include the macro definitions
  67.     Include     Equates.inc    ; Include the equate definitions
  68.     Include     StrucS.inc    ; Include the structure definitions
  69.     .286c                ; Include 80286 instructions
  70.     Page
  71. ;
  72. ;  Define the emulator code segment
  73. ;
  74. Emulate Segment Word Public 'EMULATE'   ; Emulator code segment
  75.     Assume    cs:Emulate, ds:Nothing, es:Nothing
  76.     Subttl    Cart_Init    Cartridge Initialization
  77.     Page    +
  78. ;******************************************************************************
  79. ;
  80. ;    Cart_Init(RAM_Space, Slot_Number)
  81. ;
  82. ;        Try to allocate memory for the cartridge area
  83. ;        If errors allocating memory
  84. ;            Set error code to not enough memory
  85. ;            Call the error routine
  86. ;            Call routine to exit the emulator
  87. ;        Endif
  88. ;        Save address of cartridge data area
  89. ;        Initialize the cartridge control byte
  90. ;        Try to open the cartridge file
  91. ;        If no errors opening cartridge file
  92. ;            Try to read the cartridge image
  93. ;        Endif for opening cartridge file
  94. ;        Return to the caller
  95. ;
  96. ;    Registers on Entry:
  97. ;
  98. ;        AX    - Slot number (0 - 7)
  99. ;        DS    - 65C02 RAM space
  100. ;
  101. ;    Registers on Exit:
  102. ;
  103. ;        AX-DX - Destroyed
  104. ;        SI-DI - Destroyed
  105. ;
  106. ;******************************************************************************
  107.         Even            ; Force procedure to even address
  108. Cart_Init    Proc    Near        ; Cartridge initialization procedure
  109.     mov    di,ax            ; Get the language card slot number
  110.     shl    di,1            ; Convert slot number to table index
  111. Cart_Allocate:
  112.     mov    ah,ALLOCATE_MEMORY    ; Get the allocate memory function code
  113.     mov    bx,CART_SIZE        ; Get number of paragraphs to allocate
  114.     int    DOS            ; Try to allocate cartridge space
  115.     jnc    Cart_Setup        ; Jump if no errors allocating space
  116. Memory_Error:
  117.     mov    al,ERR_NO_MEMORY    ; Get not enough memory error code
  118.     call    Error            ; Call routine to print the error
  119.     call    Exit            ; Call routine to exit the emulator
  120. Cart_Setup:
  121.     Save    ds,es            ; Save the DS and ES register values
  122.     mov    ds,ax            ; Setup cartridge segment address
  123.     mov    cs:[di + Cart_Data],ax    ; Save cartridge segment address
  124.     mov    ds:[Cart_Byte],CTRL_INIT; Initialize the cartridge control byte
  125.     mov    ax,ds            ; Get the cartridge segment
  126.     mov    es,ax            ; Set ES to cartridge segment
  127.     mov    ax,cs            ; Get current CS register value
  128.     mov    ds,ax            ; Set DS to current CS register value
  129.     mov    dx,di            ; Get the cartridge slot index
  130.     shr    dx,1            ; Convert slot index to slot number
  131.     add    dl,ASCII_CONVERT    ; Convert slot to ASCII
  132.     lea    si,cs:[Base_File]    ; Get pointer to base file name
  133.     mov    di,Cart_File        ; Get pointer to cartridge file name
  134.     mov    cx,Size Cart_Name    ; Get length of the base file name
  135.     rep    movsb            ; Setup base file name for drive A
  136.     mov    es:[Cart_File.Cart_Slot],dl
  137.     mov    ax,es            ; Get cartridge segment value
  138.     mov    ds,ax            ; Set DS to cartridge segment
  139. Open_Cart_File:
  140.     mov    ah,OPEN_FILE        ; Get the open file function code
  141.     mov    al,READ_ONLY        ; Get read only file access code
  142.     mov    dx,Cart_File        ; Get pointer to file name
  143.     int    DOS            ; Try to open cartridge file
  144.     jc    Cart_Exit        ; Jump if errors opening the file
  145. Read_Cart_File:
  146.     mov    bx,ax            ; Move file handle to BX register
  147.     mov    ah,READ_FILE        ; Get read file function code
  148.     mov    dx,Cart_Image        ; Setup the buffer address
  149.     mov    cx,CART_MAX        ; Get maximum cartridge size (32k)
  150.     int    DOS            ; Try to read the cartridge image
  151.     mov    ah,CLOSE_FILE        ; Get close file function code
  152.     int    DOS            ; Close the disk ROM file
  153. Cart_Exit:
  154.     Restore ds,es            ; Restore the DS and ES register values
  155.     ret                ; Return to the caller
  156. Cart_Init    Endp            ; End of the Cart_Init procedure
  157.     Subttl    Cart_Ctrl    Cartridge Control
  158.     Page    +
  159. ;******************************************************************************
  160. ;
  161. ;    Cart_Ctrl(RAM_Space, Slot_Number)
  162. ;
  163. ;
  164. ;        Return to the caller
  165. ;
  166. ;    Registers on Entry:
  167. ;
  168. ;        AX    - Slot number (0 - 7)
  169. ;        DS    - 65C02 RAM space
  170. ;
  171. ;    Registers on Exit:
  172. ;
  173. ;        None
  174. ;
  175. ;******************************************************************************
  176.         Even            ; Force procedure to even address
  177. Cart_Ctrl    Proc    Near        ; Cartridge card control procedure
  178.  
  179.     ret                ; Return to the caller
  180. Cart_Ctrl    Endp            ; End of the Cart_Ctrl procedure
  181.     Subttl    Cart_Rd     Cartridge Read
  182.     Page    +
  183. ;******************************************************************************
  184. ;
  185. ;    Cart_Rd(Effective_Address, Slot_Index)
  186. ;
  187. ;        Save the required registers
  188. ;        Setup the cartridge data segment
  189. ;        Get the cartridge control bits (From effective address)
  190. ;        Get the current control bits value
  191. ;        Update the control bits value
  192. ;        Call the cartridge update routine
  193. ;        Restore the required registers
  194. ;        Return to the caller
  195. ;
  196. ;    Registers on Entry:
  197. ;
  198. ;        BP    - Slot index (Slot number * 2)
  199. ;        DS:DI - 65C02 Effective address
  200. ;
  201. ;    Registers on Exit:
  202. ;
  203. ;        AL    - Memory value
  204. ;        AH    - Destroyed
  205. ;        BP    - Destroyed
  206. ;
  207. ;******************************************************************************
  208.         Even            ; Force procedure to even address
  209. Cart_Rd     Proc    Near        ; Cartridge read procedure
  210.     Save    es            ; Save the required registers
  211.     mov    es,cs:[bp + Cart_Data]    ; Setup the cartridge data segment
  212.     mov    ax,di            ; Get the effective address
  213.     and    ax,CONTROL_MASK     ; Mask off all but the control bits
  214.     mov    ah,es:[Cart_Byte]    ; Get the current control bit values
  215.     mov    es:[Cart_Byte],al    ; Update the control bits value
  216.     call    Cart_Update        ; Call the cartridge update routine
  217. Cart_Rd_Exit:
  218.     Restore es            ; Restore the required registers
  219.     ret                ; Return to the caller
  220. Cart_Rd     Endp            ; End of the Cart_Rd procedure
  221.     Subttl    Cart_Wrt    Cartridge Write
  222.     Page    +
  223. ;******************************************************************************
  224. ;
  225. ;    Cart_Wrt(Effective_Address, Slot_Index, Memory_Value)
  226. ;
  227. ;        Save the required registers
  228. ;        Setup the cartridge data segment
  229. ;        Get the cartridge control bits (From effective address)
  230. ;        Get the current control bits value
  231. ;        Update the control bits value
  232. ;        Call the cartridge update routine
  233. ;        Restore the required registers
  234. ;        Return to the caller
  235. ;
  236. ;    Registers on Entry:
  237. ;
  238. ;        AL    - Memory value
  239. ;        BP    - Slot index (Slot number * 2)
  240. ;        DS:DI - 65C02 Effective address
  241. ;
  242. ;    Registers on Exit:
  243. ;
  244. ;        BP    - Destroyed
  245. ;
  246. ;******************************************************************************
  247.         Even            ; Force procedure to even address
  248. Cart_Wrt    Proc    Near        ; Cartridge write procedure
  249.     Save    ax,es            ; Save the required registers
  250.     mov    es,cs:[bp + Cart_Data]    ; Setup the cartridge data segment
  251.     mov    ax,di            ; Get the effective address
  252.     and    ax,CONTROL_MASK     ; Mask off all but the control bits
  253.     mov    ah,es:[Cart_Byte]    ; Get the current control bit values
  254.     mov    es:[Cart_Byte],al    ; Update the control bits value
  255.     call    Cart_Update        ; Call the cartridge update routine
  256. Cart_Wrt_Exit:
  257.     Restore ax,es            ; Restore the required registers
  258.     ret                ; Return to the caller
  259. Cart_Wrt    Endp            ; End of the Cart_Wrt procedure
  260.     Subttl    Cart_Mem_Rd    Cartridge Memory Read
  261.     Page    +
  262. ;******************************************************************************
  263. ;
  264. ;    Cart_Mem_Rd(Effective_Address)
  265. ;
  266. ;        Return to the caller
  267. ;
  268. ;    Registers on Entry:
  269. ;
  270. ;        DS:DI - 65C02 Effective address
  271. ;
  272. ;    Registers on Exit:
  273. ;
  274. ;        AL    - Memory value
  275. ;
  276. ;******************************************************************************
  277.         Even            ; Force procedure to even address
  278. Cart_Mem_Rd    Proc    Near        ; Cartridge memory read procedure
  279.     ret                ; Return to the caller
  280. Cart_Mem_Rd    Endp            ; End of the Cart_Mem_Rd procedure
  281.     Subttl    Cart_Mem_Wrt    Cartridge Memory Write
  282.     Page    +
  283. ;******************************************************************************
  284. ;
  285. ;    Cart_Mem_Wrt(Effective_Address)
  286. ;
  287. ;        Return to the caller
  288. ;
  289. ;    Registers on Entry:
  290. ;
  291. ;        AL    - Memory value
  292. ;        DS:DI - 65C02 Effective address
  293. ;
  294. ;    Registers on Exit:
  295. ;
  296. ;        None
  297. ;
  298. ;******************************************************************************
  299.         Even            ; Force procedure to even address
  300. Cart_Mem_Wrt    Proc    Near        ; Cartridge memory write procedure
  301.     ret                ; Return to the caller
  302. Cart_Mem_Wrt    Endp            ; End of the Cart_Mem_Wrt procedure
  303.     Subttl    Cart_Exp_Rd    Cartridge Expansion Read
  304.     Page    +
  305. ;******************************************************************************
  306. ;
  307. ;    Cart_Exp_Rd(Effective_Address)
  308. ;
  309. ;        Read the memory location value (Byte)
  310. ;        Return to the caller
  311. ;
  312. ;    Registers on Entry:
  313. ;
  314. ;        DS:DI - 65C02 Effective address
  315. ;
  316. ;    Registers on Exit:
  317. ;
  318. ;        AL    - Memory value
  319. ;
  320. ;******************************************************************************
  321.         Even            ; Force procedure to even address
  322. Cart_Exp_Rd    Proc    Near        ; Cartridge expansion read procedure
  323.     mov    al,ds:[di]        ; Read the memory location
  324.     ret                ; Return to the caller
  325. Cart_Exp_Rd    Endp            ; End of the Cart_Exp_Rd procedure
  326.     Subttl    Cart_Exp_Wrt    Cartridge Expansion Write
  327.     Page    +
  328. ;******************************************************************************
  329. ;
  330. ;    Cart_Exp_Wrt(Effective_Address)
  331. ;
  332. ;        Return to the caller (Cartridge is NOT writable)
  333. ;
  334. ;    Registers on Entry:
  335. ;
  336. ;        AL    - Memory value
  337. ;        DS:DI - 65C02 Effective address
  338. ;
  339. ;    Registers on Exit:
  340. ;
  341. ;        None
  342. ;
  343. ;******************************************************************************
  344.         Even            ; Force procedure to even address
  345. Cart_Exp_Wrt    Proc    Near        ; Cartridge expansion write procedure
  346.     ret                ; Return to the caller
  347. Cart_Exp_Wrt    Endp            ; End of the Cart_Exp_Wrt procedure
  348.     Subttl    Cart_Update    Cartridge Update Routine
  349.     Page    +
  350. ;******************************************************************************
  351. ;
  352. ;    Cart_Update(Old_Control, New_Control, RAM_Space, Cart_Segment)
  353. ;
  354. ;        Save the required registers
  355. ;        If control bits have changed
  356. ;            Get the size of area to update
  357. ;            Compute the source address
  358. ;            Setup destination to area
  359. ;            Update the memory area from cartridge
  360. ;        Endif for control bits
  361. ;        Restore the required registers
  362. ;        Return to the caller
  363. ;
  364. ;    Registers on Entry:
  365. ;
  366. ;        AH    - Old control bits
  367. ;        AL    - New control bits
  368. ;        DS    - 65C02 RAM space
  369. ;        ES    - Cartridge segment
  370. ;
  371. ;    Registers on Exit:
  372. ;
  373. ;        AX-BX - Destroyed
  374. ;
  375. ;******************************************************************************
  376.         Even            ; Force procedure to even address
  377. Cart_Update    Proc    Near        ; Cartridge update procedure
  378.     Save    cx,si,di,ds,es        ; Save the required registers
  379.     cmp    al,ah            ; Check for control bits change
  380.     je    Update_Done        ; Jump if control bits identical
  381.     mov    cx,AREA_MAX        ; Get area maximum size (Words)
  382.     mov    ah,al            ; Get the new control bit settings
  383.     xor    al,al            ; Convert control bits to full word
  384.     shl    ax,AREA_SHIFT        ; Shift control bits into position
  385.     mov    si,ax            ; Setup the source index value
  386.     add    si,Cart_Image        ; Compute the actual source index
  387.     mov    ax,ds            ; Get the 65C02 RAM space segment
  388.     mov    di,es            ; Get the cartridge segment value
  389.     mov    ds,di            ; Setup DS to the cartridge segment
  390.     mov    es,ax            ; Setup ES to the 65C02 RAM space
  391.     mov    di,AREA_START        ; Setup destination to the area
  392.     rep    movsw            ; Move correct section into the area
  393. Update_Done:
  394.     Restore cx,si,di,ds,es        ; Restore the required registers
  395. Update_Exit:
  396.     ret                ; Return to the caller
  397. Cart_Update    Endp            ; End of the Cart_Update procedure
  398. ;******************************************************************************
  399. ;
  400. ;    Define the cartridge data areas
  401. ;
  402. ;******************************************************************************
  403. Cart_Data    Equ    This Word    ; Define the cartridge pointers
  404.         Slot_Data    <>    ; Pointers to the cartridge data areas
  405. Base_File    Cart_Name    <>    ; Define base cartridge file name
  406. Cart_ID     Equ    This Byte    ; Cartridge ID string
  407.         Db    "Cartridge",0
  408. ;******************************************************************************
  409. ;
  410. ;    Define the end of the Emulator Code Segment
  411. ;
  412. ;******************************************************************************
  413. Emulate Ends
  414.     End                ; End of the Cartrige module
  415.