cebix / macemu

Basilisk II and SheepShaver Macintosh emulators
1.4k stars 291 forks source link

[BasiliskII] Patch Mac SE ROM to use emulated hard disk #134

Closed rickyzhang82 closed 7 years ago

rickyzhang82 commented 7 years ago

This thread is intended for discussing patching disk driver in Mac SE ROM. In #131 PR, we found that emulated hard drive doesn't work in 24bit ROM. That mean the ROM patching doesn't work.

rickyzhang82 commented 7 years ago

I don't have too much doc regarding to Compact Macintosh Driver. The following is based on my observation and code reading from Mac SE ROM.

  1. Hardware driver is written in ROM.

  2. It is in resource.

  3. Find the offset of resource.

                      ; Resource in ROM
    1AF1C  001C                      DC.W      $001C           ; ?
    1AF1E  01FA                      DC.W      $01FA           ; resource map size
                      ; Resource map
    1AF20  0000 0100                 DC.L      $00000100       ; reserved for Offset to resource data
    1AF24  0002 1B1D                 DC.L      $00021B1D       ; reserved for Offset to resource map
    1AF28  0002 1A1D                 DC.L      $00021A1D       ; reserved for Length of resource data
    1AF2C  0000 01FA                 DC.L      $000001FA       ; reserved for Length of resource map
    1AF30  0000 0000                 DC.L      $00000000       ; reserved for handle to next resource map
    1AF34  0002                      DC.W      $0002           ; reserved for file reference number
    1AF36  8000                      DC.W      $8000           ; Resource file attributes
    1AF38  001C                      DC.W      $001C           ; Offset to type list
    1AF3A  01D6                      DC.W      $01D6           ; Offset to resource name list
  4. Find the offset of DRVR type list.

                      ; Type list
    1AF3C  000C                      DC.W      $000C           ; Number of resource types - 1
    1AF3E  5041 434B                 DC.L      $5041434B       ; Resource type PACK
    1AF42  0002                      DC.W      $0002           ; Number of resources of this type - 1
    1AF44  006A                      DC.W      $006A           ; Offset to reference list
    1AF46  7463 736C                 DC.L      $7463736C       ; Resource type tcsl
    1AF4A  0000                      DC.W      $0000           ; Number of resources of this type - 1
    1AF4C  008E                      DC.W      $008E           ; Offset to reference list
    1AF4E  6262 6D63                 DC.L      $62626D63       ; Resource type bbmc
    1AF52  0000                      DC.W      $0000           ; Number of resources of this type - 1
    1AF54  009A                      DC.W      $009A           ; Offset to reference list
    1AF56  5345 5244                 DC.L      $53455244       ; Resource type SERD
    1AF5A  0000                      DC.W      $0000           ; Number of resources of this type - 1
    1AF5C  00A6                      DC.W      $00A6           ; Offset to reference list
    1AF5E  4452 5652                 DC.L      $44525652       ; Resource type DRVR
    1AF62  0004                      DC.W      $0004           ; Number of resources of this type - 1
    1AF64  00B2                      DC.W      $00B2           ; Offset to reference list
    1AF66  4344 4546                 DC.L      $43444546       ; Resource type CDEF
    1AF6A  0001                      DC.W      $0001           ; Number of resources of this type - 1
    1AF6C  00EE                      DC.W      $00EE           ; Offset to reference list
    1AF6E  4B43 4852                 DC.L      $4B434852       ; Resource type KCHR
    1AF72  0000                      DC.W      $0000           ; Number of resources of this type - 1
    1AF74  0106                      DC.W      $0106           ; Offset to reference list
    1AF76  4B4D 4150                 DC.L      $4B4D4150       ; Resource type KMAP
    1AF7A  0000                      DC.W      $0000           ; Number of resources of this type - 1
    1AF7C  0112                      DC.W      $0112           ; Offset to reference list
    1AF7E  4D42 4446                 DC.L      $4D424446       ; Resource type MBDF
    1AF82  0000                      DC.W      $0000           ; Number of resources of this type - 1
    1AF84  011E                      DC.W      $011E           ; Offset to reference list
    1AF86  4D44 4546                 DC.L      $4D444546       ; Resource type MDEF
    1AF8A  0000                      DC.W      $0000           ; Number of resources of this type - 1
    1AF8C  012A                      DC.W      $012A           ; Offset to reference list
    1AF8E  5744 4546                 DC.L      $57444546       ; Resource type WDEF
    1AF92  0001                      DC.W      $0001           ; Number of resources of this type - 1
    1AF94  0136                      DC.W      $0136           ; Offset to reference list
    1AF96  4355 5253                 DC.L      $43555253       ; Resource type CURS
    1AF9A  0003                      DC.W      $0003           ; Number of resources of this type - 1
    1AF9C  014E                      DC.W      $014E           ; Offset to reference list
    1AF9E  464F 4E54                 DC.L      $464F4E54       ; Resource type FONT
    1AFA2  0004                      DC.W      $0004           ; Number of resources of this type - 1
    1AFA4  017E                      DC.W      $017E           ; Offset to reference list
  5. Find .SONY DRVR

                      ; Resource type 'DRVR'
    1AFEE  000A                      DC.W      $000A           ; Resource ID
    1AFF0  0000                      DC.W      $0000           ; Offset to name
    1AFF2  5803 24AA                 DC.L      $580324AA       ; Attributes + Offset to data block; Apple Talk ATP
    1AFF6  0000 0000                 DC.L      $00000000       ; reserved for handle
    1AFFA  0009                      DC.W      $0009           ; Resource ID
    1AFFC  0005                      DC.W      $0005           ; Offset to name
    1AFFE  5803 312A                 DC.L      $5803312A       ; Attributes + Offset to data block; Apple Talk MPP
    1B002  0000 0000                 DC.L      $00000000       ; reserved for handle
    1B006  0004                      DC.W      $0004           ; Resource ID
    1B008  0012                      DC.W      $0012           ; Offset to name
    1B00A  5803 4680                 DC.L      $58034680       ; Attributes + Offset to data block; 0x34680 is offset to sony driver.
    1B00E  0000 0000                 DC.L      $00000000       ; reserved for handle
    1B012  0003                      DC.W      $0003           ; Resource ID
    1B014  0018                      DC.W      $0018           ; Offset to name
    1B016  5803 6C90                 DC.L      $58036C90       ; Attributes + Offset to data block; 0x36C90 is offset to sound driver.
    1B01A  0000 0000                 DC.L      $00000000       ; reserved for handle
    1B01E  0028                      DC.W      $0028           ; Resource ID
    1B020  001F                      DC.W      $001F           ; Offset to name
    1B022  5803 7028                 DC.L      $58037028       ; Attributes + Offset to data block; Apple Talk
    1B026  0000 0000                 DC.L      $00000000       ; reserved for handle
  6. Go to the offset of SONY driver

                      ; resource header
    34678  C000 2610                 DC.L      $C0002610       ; Length
    3467C  0000 0074                 DC.L      $00000074       ; ?
                      ; resource : 'DRVR' $4 '.Sony'
                      ; Disk Driver
                      Driver_Sony:
    34680  4F00                      DC.W      $4F00           ; flags
    34682  0000                      DC.W      $0000           ; number of ticks between periodic actions
    34684  0000                      DC.W      $0000           ; desk accessory event mask
    34686  0000                      DC.W      $0000           ; menu ID
    34688  006E                      DC.W      $006E           ; offset to open routine
    3468A  038C                      DC.W      $038C           ; offset to prime routine
    3468C  01F6                      DC.W      $01F6           ; offset to control routine
    3468E  033E                      DC.W      $033E           ; offset to status routine
    34690  0018                      DC.W      $0018           ; offset to close routine
    34692  05                        DC.B      $05             ; length of driver name
                      ; Name
    34693  2E53 6F6E 79              DC.B      '.Sony'
                      ; end of Name
    
                      E_Sony_Close:
    34698  70E8                      MoveQ.L   $-18, D0
    3469A  4E75                      Rts
    ...
rickyzhang82 commented 7 years ago

BII floppy driver patch overwrite routine address from offset 34688 to 34690. I confirmed that emulated floppy works in BII. But I haven't figure it out why BII just place hard disk driver with delta offset 0x100 in line 901 of src/rom_patches.cpp.

0895     // Replace .Sony driver
0896     sony_offset = 0x34680;
0897     D(bug("sony %08lx\n", sony_offset));
0898     memcpy(ROMBaseHost + sony_offset, sony_driver, sizeof(sony_driver));
0899 
0900     // Install .Disk and .AppleCD drivers
0901     memcpy(ROMBaseHost + sony_offset + 0x100, disk_driver, sizeof(disk_driver));
0902     memcpy(ROMBaseHost + sony_offset + 0x200, cdrom_driver, sizeof(cdrom_driver));
rickyzhang82 commented 7 years ago

I figured it out that driver installation in classic rom patch doesn't work:

0882     // Install our own drivers
0883     wp = (uint16 *)(ROMBaseHost + 0x3f82a);
0884     *wp++ = htons(M68K_EMUL_OP_INSTALL_DRIVERS);
0885     *wp++ = htons(M68K_NOP);
0886     *wp++ = htons(M68K_NOP);
0887     *wp = htons(M68K_NOP);

My patch in my pr branch https://github.com/rickyzhang82/macemu/tree/pr-fix-emulated-hard-drive-in-mac-se-rom fix the issue.

I have tested that the patch works for Mac SE ROM and Mac Classic ROM.

rickyzhang82 commented 4 years ago

For record purpose, I replaced the sound driver with disk driver in PR https://github.com/cebix/macemu/commit/ca11c2588239721d9dee7a7d1186a9aa3542feac .

This enables System 6 to boot from emulated hard disk.