Compare commits

...
Author SHA1 Message Date
Maxim Poliakovski 3a5c61797c Revert "PCI fixes" 2022-09-02 23:24:06 +00:00
dingusdev 467e4df7dc Merge pull request #32 from joevt/master
PCI fixes
2022-09-02 07:42:01 -07:00
joevt 24ecdbe75b Allow non-HFS/MFS raw floppies
Such as FAT formatted floppies which should be readable in Open Firmware (when floppy support is updated to work in Open Firmware).
2022-09-02 03:39:50 -07:00
joevt b654424465 Fix PCI config r/w of byte and word and unaligned
dingusppc could not read bytes from offset 1,2,3 or words from offset 2.
dingusppc did not read words from offset 1,3 and longs from offset 1,2,3 in the same way as a real Power Mac 8600 or B&W G3.
This commit fixes those issues.

- Added pci_cfg_rev_read. It takes a 32 bit value from offset 0 and returns a value of the specified size using bytes starting from the specified offset. Offsets 4,5, & 6 wrap around to 0,1, & 2 respectively. The result bytes are in flipped order as required by the pci_cfg_read method (so a value of 0x12345678 is returned as 0x78563412)
A real Power Mac 8600 might return a random byte for offset 4, 5, 6 for vci0 but usually not for pci1. A B&W G3 seems to always wrap around correctly. We won't read random bytes, and we won't read a default such as 00 or FF. We'll do the wrap around which makes the most sense because writing 0x12345678 to any offset and reading from the same offset should produce the value that was written.

- Added pci_cfg_rev_write. It takes a 32 bit value from offset 0, and modifies a specified number of bytes starting at a specified offset with the offset wrapping around to 0 if it exceeds 3. The modified bytes take their new values from the flipped bytes passed to pci_cfg_write. When size is 4, the original value is not used since all bytes will be modified.

Basically, those two functions handle all the sizes and all the offsets and replace calls to BYTESWAP_32, read_mem or read_mem_rev, and write_mem or write_mem_rev.
read_mem_rev, as it was used by pcidevice and some other places, could read beyond offset 3 if it were ever passed a reg_offs value that did not have offset as 0. Since the offset was always zero, it would always read the wrong byte or word if they were not at offset 0. Same for read_mem as used by mpc106.
write_mem_rev, as it was used by pcidevice and some other places, could write beyond offset 3 if it were ever passed a reg_offs value that did not have offset as 0. Since the offset was always zero, it would always write the wrong byte or word if they were not at offset 0. Same for write_mem as used by mpc106.

The PCI controllers (bandit, chaos, mpc106) need to encode the offset (0,1,2,3) into the reg_offs parameter passed to pci_cfg_read and pci_cfg_write so they can return or modify the correct bytes of the dword at reg_offs & 3.

The pci_cfg_read and pci_cfg_write methods extract the offset from reg_offs and report unaligned accesses.

pci_cfg_read uses pci_cfg_rev_read to read from the reg using the size and offset to determine which bytes to read.

pci_cfg_write uses pci_cfg_rev_write to write to the reg using the size and offset to determine which bytes to modify.

Other changes:
- for unimplemented config register reads and writes, bandit and ATIRage now includes offset and size (and value in the case of writes) in log warnings.
- for unimplemented config register reads and writes, pcidevice now includes offset in log warnings.
- pci_read and pci_write of mpc106 require an offset parameter since config_addr does not contain the offset (it is always a multiple of 4). The offset is included in the log warninings for non-existent PCI devices.
- ATIRage uses pci_cfg_rev_read and pci_cfg_rev_write which correctly places user_cfg at byte 0x40 instead of 0x43 and writes the correct byte depending on size and offset.

Notes:
- pci_cfg_read calls READ_DWORD_LE_A and pci_cfg_write calls WRITE_DWORD_LE_A. When reading or writing memory that is organized as little endian dwords, such as my_pci_cfg_hdr of mpc106, the function should explicitly state that it's little endian so that the emulator may be ported one day to a CPU architecture that is not little endian.
2022-09-02 03:39:50 -07:00
joevt d91e14abc6 Log PCI config write values MSB first
Writes to config registers of invalid or non-existent PCI devices are logged. They should be logged with most significant byte first.
The values enter the methods in reverse byte order so they need to be byte swapped (except when size is 1) for logging.
The result is that this command in Open Firmware:
`12345678 16800 config-l!`
will log this:
`VCI0 err: write attempt to non-existing VCI device ??:0d.0 @00.l = 12345678`
2022-09-02 03:39:50 -07:00
joevt a08e70781a Add BAR 0 to control
BAR 0 exists on a real Power Mac 8600 and the dingusppc 7500.

On a Power Mac 8600, the initial value is 0x84000003. In Open Firmware, you can write to all bits of the BAR and read the value back except the 2 least significant bits are always %11. Bit 0 indicates I/O space. Bit 1 is reserved and should be zero so maybe this is not a real I/O space BAR. 0x8400000 is written to the BAR by Open Firmware. It doesn't look like a normal I/O address which are usually 16 bits.

On the emulated 7500, 0x02000000 is written to the BAR by Open Firmware sometime during probe-all. The BAR did not behave as it does in the Power Mac 8600. This commit fixes that.

Two questions remain:
1) Which fcode writes to the BAR? Is it the probe fcode or is it the control fcode? There's no config-_! in the control fcode.
2) What is the purpose of the BAR? Writing to it can cause a hang. The testbits code below seems to succeed - it restores the original value after reading the result of testing each bit and before displaying the result. The values shown for the MSB (0x84 on the 8600 and 0x02 on the 7500) could be three flag bits.

```
dev vci0
: testbits { adr ; org }
	cr
	adr config-l@ dup -> org ." original : " 8 u.r cr
	20 0 do
		1 1f i - << dup 8 u.r ."  : "
		adr config-l!
		adr config-l@
		org adr config-l!
		8 u.r cr
	loop
	;

15810 testbits \ 15810 is the address of the BAR on the emulated 7500.
```
2022-09-02 03:39:50 -07:00
joevt a24840803c Allow bit 1 of I/O BARs to be set
Usually bit 1 of I/O BARs is 0 since it is reserved, but control has an I/O BAR where this bit is set.
bar_cfg is used to determine the default values of the least significant bits (2 bits for I/O BARs and 4 bits for Memory BARs).
The upper bits of bar_cfg determine which bits can be set and also determines the size of the BAR.
2022-09-02 03:39:50 -07:00
joevt 7e7522d4b7 Fix Expansion ROM BAR writes
The bits that can be set are the enable bit (bit 0) plus the bits represented by exp_bar_cfg which is determined by the size of the ROM which is calculated to be a power of 2 and a minimum of 2K.
2022-09-02 03:39:50 -07:00
Maxim Poliakovski 5b08f283e5 Hacks for debugging HW interrupts. 2022-08-27 17:38:53 +02:00
Maxim Poliakovski 50053a52dc debugger: disable nvedit command on Windows hosts. 2022-08-26 11:13:33 +02:00
dingusdev 4a8a712868 Merge pull request #31 from joevt/master
Fix return value for bad pci config address
2022-08-24 20:31:11 -07:00
joevt e41b196977 Fix return value for bad pci config address
PCI config read fails should return all 1 bits.
All unused registers in an existing PCI device should return 0.

Because that's what my Power Mac 8600 returns when I run my Open Firmware lspci command.
Any bus/device/function that doesn't exist returns FF and won't be listed by lspci.
Any registers that are unused will show as 00 in the lspci output.

Make grackle log bus:device.function @register.size in all cases.
2022-08-24 07:58:12 -07:00
Maxim Poliakovski 6c59bf4203 heathrow: fix interrupt processing. 2022-08-24 14:58:07 +02:00
Maxim Poliakovski bb77b2d525 grandcentral: fix interrupt processing. 2022-08-24 14:58:07 +02:00
Maxim Poliakovski 293c5a40f3 amic: fix PDM interrupts. 2022-08-24 14:58:07 +02:00
Maxim Poliakovski 994f8d7155 viacuda: fix interrupts. 2022-08-24 14:15:48 +02:00
Maxim Poliakovski b38d00ce2d cpu/ppc: improve support for external interrupts.
Support generating of external interrupt exception
in MTMSR and RFI when MSR[EE] is re-enabled and
external interrupt still pending.
2022-08-24 14:15:47 +02:00
Maxim Poliakovski 141a276c5b Merge pull request #30 from joevt/master
more fixes
2022-08-23 02:56:26 +02:00
joevt 1c1300ce5a Add nvedit
setenv doesn't allow entering strings that include multiple lines which is useful for nvramrc.

nvedit uses CTRL-C to end editing, like Open Firmware. It does not support deleting characters from a line or editing previous lines or any other special keys. It doesn't have nvstore or nvquit or nvrecover commands. Basically, you should edit your nvramrc in a text editor, then copy and paste into the debugger.
2022-08-22 17:21:58 -07:00
joevt 1ccc8d1a25 Fix setenv and printenv
- Allow changing integer variables. Previously they could not be changed.
- Allow changing string variables. Previously they could only be changed if their length increased.
- printenv had a bug with strings longer than 32 characters.
- printenv now translates carriage return (used in nvramrc) as endl (which should perform carriage return and linefeed)
- For integers, setenv assumes decimal number input but if that fails it will try hex conversion. 0x prefix for hex numbers is optional unless the number only has digits 0 - 9.
- setenv converts linefeed to carriage return for string variables. nvramrc requires this for proper editing in Open Firmware.
- Added Open Firmware 2.4 nvram field names to OfNvramHdr comments.
2022-08-22 17:20:04 -07:00
joevt 67b7c86755 serial backend socket fixes
- increase delay to 800 consecutive characters (reduces times when Open Firmware throws away pasted text)
- don't log the first sock read error (when accept hasn't been called yet) but do log sock read error if accept was called successfully already.
2022-08-22 17:16:22 -07:00
joevt 3ee2ea1871 Fix read/write argument names
base class uses reg_start so derived classes should do the same.
Some derived class already uses reg_start for read method.
2022-08-22 17:16:22 -07:00
joevt 3b4f40635a Register offsets should be logged as hex
- decimal values are confusing (can't tell if 12 means 12 or 18)
- most specs show hex values for register offsets.
2022-08-22 17:16:22 -07:00
joevt c644d1609f PCI Expansion ROM size should be power of 2
The expansion rom base register indicates the size of the expansion rom by the number of bits that remain zero after code attempts to set them all to 1. For example, a result of fffe0000 means 128K. The 11 least significant bits are ignored in the size calculation, which means the minimum rom size is indicated by fffff800 = 2K.

Handle the case where an expansion rom file might not have a size that is a multiple of 2 or is not greater than 2K.
Bytes between the end of the file and the end of the calculated rom size are set to 0xff.
2022-08-22 17:16:22 -07:00
joevt d843091a4d Allow setting PCI capabilities pointer
The capabilities pointer is usually a constant, but whatever the capabilities pointer points to has to be handled by the derived class.
2022-08-22 17:16:22 -07:00
joevt 6fad095730 Allow setting PCI status register
Don't always return 0 when reading it. The status register contains some bits that Open Firmware uses to set some properties.
A PCI device can set a default status register value to set those bits.
	this->status = 0x02B0; // 0000  0 01 0  1 0 1 1  0000 Capabilities List, 66 MHz, Fast Back-to-Back, DevSel Speed: Medium
2022-08-22 17:16:22 -07:00
joevt 6af8b52376 changes to pci logging
For invalid or unsupported PCI accesses, do the following:
- log a device's full pci address using pciutils setpci command format bb:dd.f @rr.s (bus:device:function @register+offset.size).
- report as read or write access.
- log value for writes.
- bus, device, function, and register values cannot be determined from Invalid IDSEL values so they will output as ??.
- for invalid IDSEL values, report the entire value of the config_addr.
- for valid IDSEL values, the bus number cannot be determined since IDSEL only specifies device number. It's probably bus 00 but we'll show ?? to indicate an IDSEL type access.

Add missing config type read access logging for chaos.
2022-08-22 17:16:17 -07:00
joevt fca6cb11b6 Add info for possible grackle PCI devices
Devices that are not probed by Open Firmware might still be usable by Mac OS X or Linux if they can enumerate PCI devices without using Open Firmware's device tree.
A patch in nvramrc can make Open Firmware probe all the devices.
The point is that the emulation does not need to be limited to 5 slots.
2022-08-22 17:07:50 -07:00
joevt 753427f262 Add known Open Firmware 1.0.5 bugs
Additional notes about get-inherited-property and map-in.
2022-08-22 17:07:50 -07:00
joevt 6fbe03adb3 Add some Open Firmware bugs 2022-08-22 17:07:50 -07:00
joevt f78db220bc Add @startvec notes
because it's not the same as StartVec
2022-08-22 17:07:50 -07:00
joevt 63248b0fa2 Set Bandit's HWComponent name
Bandit has two names:
- PCIDevice->pci_name
- PCIDevice:MMIODevice:HWComponent->name
The latter was not being set.
2022-08-22 17:07:50 -07:00
joevt cd122ce263 Open Firmware is two words
Not OpenFirmware.
Also fixed a spelling mistake and removed some extra spaces.
2022-08-22 16:47:38 -07:00
Maxim Poliakovski e097b7a0a1 bandit: fix return value for empty slots. 2022-08-22 15:05:35 +02:00
Maxim Poliakovski 32b8c8ed43 pcidevice: fix expansion ROM mapping. 2022-08-20 12:51:08 +02:00
Maxim Poliakovski 5220b03f0f MPC106: insert PCI devices using the CLI. 2022-08-19 20:07:22 +02:00
Maxim Poliakovski 336ea14422 machinefactory: don't crash when property help is missing. 2022-08-19 19:44:10 +02:00
Maxim Poliakovski 56c54e4c8c pcihost: add attach_pci_device method. 2022-08-19 18:55:33 +02:00
Maxim Poliakovski f4c0499078 deviceregistry: add device_registered method. 2022-08-19 18:53:05 +02:00
Maxim Poliakovski 4964008511 machineid: improve Gossamer ID description. 2022-08-15 20:49:06 +02:00
Maxim Poliakovski c5ecdb840d machinegossamer: proper system & clocks configuration. 2022-08-15 14:51:37 +02:00
Maxim Poliakovski 0dbcda5710 machinegossamer: system register definitions. 2022-08-15 14:50:49 +02:00
Maxim Poliakovski 8c0f391548 ppcemu: add enums for HID0 and HID1. 2022-08-15 14:45:55 +02:00
dingusdev 7f57e96335 Merge pull request #29 from joevt/master
Add socket option for serial backend
2022-08-14 16:59:08 -07:00
joevt 93fae1ee68 Merge remote-tracking branch 'upstream/master' 2022-08-14 16:38:51 -07:00
joevt 650c2c88dd Add socket type for serial_backend
With the option --serial_backend=socket, input and output to a serial port will use a SOCK_STREAM type UNIX domain socket. This allows you to do Open Firmware in one window, while the first window can be used for dingusppc debugger.

Other fixes:

- Added SIGTERM handler so that if the user force quits dingusppc, the terminal settings are properly restored. A user needs to force quit when --serial_backend=stdio and Open Firmware is taking input from ttya. If terminal settings are not restored, then running dingusppc after a force quit will cause Control-C to not work when --serial_backend is not stdio.

- Added a couple numbers to rcv_char_available - 15 is the number of consecutive characters that can processed. 400 is the total number of calls to rcv_char_available after 15 consecutive characters have been read before additional characters can be read. This delay in processing additional characters allows pasting arbitrarily large amounts of text into Open Firmware. A real serial port terminal app might have a text pacing option to limit the number of output characters per second but that is not an option since the emulator is not limiting character data to a baud rate.

Related Notes:

The socket file is created when dingusppc starts.
The socket file is named dingusppcsocket and is created in the current working directory (usually where the executable is located and where the dingusppc.log, nvram.bin, and pram.bin files are created).
The socket file is not visible in the Finder. You can see it in the terminal using the ls command.
The socket file can be used with the following command in a new terminal window:
socat UNIX-CLIENT:dingussocket -,cs8,parenb=0,echo=0,icanon=0,isig=0,icrnl=0
When dingusppc quits, the socat command ends.

Other notes:

The dingusppc --debugger option causes dingusppc to enter the debugger before Open Firmware outputs anything. You can connect to the socket while dingusppc is in the debugger. Then enter the go command to leave the debugger and start Open Firmware. However, since the startup sound takes a long time, you can probably connect to the socket before Open Firmware starts even without the --debugger option. It's like with a real Power Mac - you have a few seconds to hold Command-Option-O-F except in this case you have a few seconds to press the up arrow and press enter (for executing the last command from the terminal command history) and if you do it too late you'll still get into Open Firmware if auto-boot? was previously set to false using the dingusppc debugger which is actually the only way to get into Open Firmware since a keyboard is currently not emulated?).

To set ttya as the input and output device in Open Firmware, you can use the setenv command in the dingusppc debugger. The device path needs to be longer than the current device path (because code for handling shortening of the paths is currently not implemented). For example, ttya can replace kbd for the input-device, but to replace screen for the output-device you need to add some extra characters like this: ttya,11 (I think the number is for baud but we're not using a real serial port so baud doesn't matter).

Future ideas:

- Have dingusppc execute the socat command for you so that it opens a terminal window before Open Firmware starts.
- Add another --serial_backend for the printer port (ttyb) since now we have more than one type of serial backend. If both serial ports use socket backend, then a different name for the second socket is required.
- Have an option to make dingusppc block until something connects to the socket (this means calling accept after listen instead of after select).
- Test compatibility with serial port socket created by Parallels Desktop virtual machines in macOS.
- Find a solution that works with Windows.
- Test with Linux.
- Create a serial_backend type for tty devices. I suppose maybe socat can pipe the file socket to tty but a direct connection might be easier to setup.
- Allow using a socket created by some other app (for example, socat UNIX-LISTEN). This means dingusppc will assume the client role and will call connect instead of accept.
2022-08-14 16:36:52 -07:00
Maxim Poliakovski 3f3af68582 machinegossamer: add Whisper ID PROM. 2022-08-14 23:01:55 +02:00
Maxim Poliakovski 8cdbd9f81f Generic I2C PROM emulation. 2022-08-14 23:01:55 +02:00
joevt b76bfedf4b Remove unnecessary linefeeds from log
To remove blank lines in the dingusppc.log file or in the console output when -d is used.
2022-08-14 05:26:56 -07:00
Maxim Poliakovski eecb5a0f42 machinegossamer: add Athens I2C device. 2022-08-11 01:45:32 +02:00
Maxim Poliakovski b67644ba35 athens: support ID reading & dot clock disabling. 2022-08-11 01:43:29 +02:00
Maxim Poliakovski 56db0426a4 heathrow: fix EMMO bit. 2022-08-08 21:06:57 +02:00
Maxim Poliakovski 5463c8e6cb athens: fix compilation with GCC. 2022-08-07 15:32:05 +02:00
Maxim Poliakovski fd41cafc4b New machine: Power Macintosh 7500. 2022-08-07 15:25:58 +02:00
Maxim Poliakovski 8dfdf55dff Initial emulation for the Control video. 2022-08-07 15:25:58 +02:00
Maxim Poliakovski a0e660f6b4 videoctrl: remove unnecessary delay. 2022-08-07 15:25:58 +02:00
Maxim Poliakovski cd1d0031e6 GC: implement attachable IOBus devices. 2022-08-07 15:25:58 +02:00
Maxim Poliakovski 4216c412b3 Initial Hammerhead ASIC emulation. 2022-08-07 15:25:58 +02:00
Maxim Poliakovski bd19914132 Initial emulation of the Athens clock ASIC. 2022-08-07 15:25:58 +02:00
Maxim Poliakovski 9a6336adb9 Move DRAM capacity constants to memctrlbase.h 2022-08-07 15:25:58 +02:00
Maxim Poliakovski e3900b9062 bandit: add Chaos support. 2022-08-06 19:29:45 +02:00
Maxim Poliakovski ea0fb3b410 bandit: initialize address mask register. 2022-07-25 12:51:55 +02:00
Maxim Poliakovski c3939e3b25 Gossamer: factory test control. 2022-07-20 20:08:37 +02:00
Maxim Poliakovski f0553720d6 Catalyst: factory test control. 2022-07-20 01:36:45 +02:00
Maxim Poliakovski 3c062443f6 PDM: factory test control. 2022-07-20 01:36:05 +02:00
Maxim Poliakovski b9fb0b9c5f Switch fdd_wr_prot to BinProperty. 2022-07-19 23:48:17 +02:00
Maxim Poliakovski 65512dc434 New property type: BinProperty. 2022-07-19 23:45:41 +02:00
Maxim Poliakovski b8915f11a2 debugger: fix ofnvram commands for Nubus machines. 2022-07-18 20:27:34 +02:00
Maxim Poliakovski c0078ce97d Refactor MachineBase and MachineFactory classes.
Adding new machines is much easier now.
A significant amount of duplicated code has been reduced.
2022-07-18 20:27:34 +02:00
Maxim Poliakovski 9971052a78 mesh: self-registration with the device registry. 2022-07-18 20:27:34 +02:00
Maxim Poliakovski 7db0a31cc5 nvram: self-registration with the device registry. 2022-07-18 20:27:34 +02:00
Maxim Poliakovski 5e6f3a51b5 soundserver: shut-down safely. 2022-07-18 20:27:34 +02:00
Maxim Poliakovski c37893847a atimach64gx: self-registration with the device registry. 2022-07-18 20:27:34 +02:00
Maxim Poliakovski 1d37982d02 mace: self-registration with the device registry. 2022-07-18 20:27:34 +02:00
Maxim Poliakovski 41a314d6d6 bandit: self-registration with the device registry. 2022-07-18 20:27:34 +02:00
Maxim Poliakovski 2dfc160e30 sc53c94: self-registration with the device registry. 2022-07-18 20:27:34 +02:00
Maxim Poliakovski 5f8a927846 platinum: self-registration with the device registry. 2022-07-18 20:27:34 +02:00
Maxim Poliakovski 97b3b9a6f8 MPC106: self-registration with the device registry. 2022-07-18 20:27:34 +02:00
Maxim Poliakovski cb68b70d52 hmc: self-registration with the device registry. 2022-07-18 20:27:34 +02:00
Maxim Poliakovski 66debbc730 viacuda: self-registration with the device registry. 2022-07-18 20:27:34 +02:00
Maxim Poliakovski 9056d53474 macio: self-registration with the device registry. 2022-07-18 20:27:34 +02:00
Maxim Poliakovski ca51c34157 amic: self-registration with the device registry. 2022-07-18 20:27:34 +02:00
Maxim Poliakovski 7fc28baf96 escc: self-registration with the device registry. 2022-07-18 20:27:34 +02:00
Maxim Poliakovski 439029cafe swim3: self-registration with the device registry. 2022-07-18 20:27:34 +02:00
Maxim Poliakovski dc5373ae27 Implement basic device registry. 2022-07-18 20:24:40 +02:00
Maxim Poliakovski f9ec73cd05 Merge branch 'tnt' 2022-06-18 18:47:31 +02:00
Maxim Poliakovski 5668fc161f macio: fix SWIM3 register space accesses. 2022-06-13 23:15:48 +02:00
Maxim Poliakovski 1d6f296d10 chario: working Windows CharIo backend. 2022-06-12 17:55:34 +02:00
Maxim Poliakovski 913944c607 platinum: implement video controller registers. 2022-06-09 23:16:03 +02:00
dingusdev fd3ff7b703 Initial attempt at terminal support for Windows 2022-05-22 17:46:40 -07:00
Maxim Poliakovski cdc670268a Merge pull request #27 from dingusdev/gh-actions
Improve Github actions.
2022-05-21 15:17:55 +02:00
Maxim Poliakovski 4fed6630ec Improve Github actions. 2022-05-21 15:14:30 +02:00
Maxim Poliakovski 06001a778f GCC compilation fixes. 2022-05-21 15:10:40 +02:00
Maxim Poliakovski f93df3b5c3 GrandCentral: improve error logging. 2022-05-21 14:51:28 +02:00
Maxim Poliakovski fcd2ddcc14 machinecatalyst: disable ATI Mach64 card hack. 2022-05-21 14:51:28 +02:00
Maxim Poliakovski 51e6ffb24f GrandCentral: properly connect Curio SCSI. 2022-05-21 14:51:28 +02:00
Maxim Poliakovski 0c77cccb9e ppcopcodes: fix mcrf (again). 2022-05-21 14:51:28 +02:00
Maxim Poliakovski d928d3ff55 CharIoStdin: disable SIGINT generation with Ctrl-C. 2022-05-21 14:51:28 +02:00
Maxim Poliakovski 4c9001901e ESCC: connect NULL and STDIO backends. 2022-05-21 14:51:27 +02:00
Maxim Poliakovski da25e72668 Fix ESCC register addressing. 2022-05-21 14:51:27 +02:00
Maxim Poliakovski 16bf46506a New machine: Catalyst (PM7200). 2022-05-21 14:51:27 +02:00
Maxim Poliakovski 0df1b2c408 ATI Mach64 GX controller emulation. 2022-05-21 14:51:27 +02:00
Maxim Poliakovski 226b51d8db displayid: constructor with parameters. 2022-05-21 14:51:27 +02:00
Maxim Poliakovski cd7624feb5 videoctrl: add pixel_depth member. 2022-05-21 14:51:27 +02:00
Maxim Poliakovski bd24b644c9 Move ATI Mach64 definitions to separate header. 2022-05-21 14:51:27 +02:00
Maxim Poliakovski ed02a06bb8 pcidevice: loading of expansion ROMs from files. 2022-05-21 14:51:27 +02:00
Maxim Poliakovski 5b7e79b979 Bandit: implement I/O space transactions. 2022-05-21 14:51:27 +02:00
Maxim Poliakovski bfd48eeeb2 Bandit: implement address mask register. 2022-05-21 14:51:27 +02:00
Maxim Poliakovski 4225f0aec2 ofnvram: implement changing of string variables. 2022-05-21 14:51:27 +02:00
Maxim Poliakovski 6b686e402e videoctrl: fix surface size. 2022-05-21 14:51:27 +02:00
Maxim Poliakovski dd79ec38e4 ATIRage: hack to support OF output. 2022-05-21 14:51:27 +02:00
Maxim Poliakovski 898c677ffb debugger: add printenv and setenv commands. 2022-05-21 14:51:27 +02:00
Maxim Poliakovski a7e06f4e4b Utility class for viewing/changing OF NVRAM variables. 2022-05-21 14:51:27 +02:00
Maxim Poliakovski 4b2f3cedc7 Make NVRAM a full-fledged HW component. 2022-05-21 14:51:27 +02:00
Maxim Poliakovski 32891867f2 GrandCentral: basic device interrupt handling. 2022-05-21 14:51:27 +02:00
Maxim Poliakovski 0ab4f4a7f9 GrandCentral: external SCSI (Curio style). 2022-05-21 14:51:27 +02:00
Maxim Poliakovski b47de8b042 Implement MPC601 style RTC. 2022-05-21 14:51:27 +02:00
Maxim Poliakovski 934f794d20 GrandCentral: fix access to NVRAM subdevice. 2022-05-21 14:51:27 +02:00
Maxim Poliakovski 9317719814 GrandCentral: access to ESCC and board reg 1. 2022-05-21 14:51:27 +02:00
Maxim Poliakovski 4676cfeee2 Platinum: implement memory controller registers. 2022-05-21 14:51:27 +02:00
Maxim Poliakovski 8be44dad82 Heathrow: interrupt registers & mode 1 interrupts. 2022-05-21 14:51:27 +02:00
Maxim Poliakovski 617f75851b Initial emulation of the GrandCentral I/O controller. 2022-05-21 14:51:27 +02:00
Maxim Poliakovski fc3901676b Heathrow: use common PCI configuration code. 2022-05-21 14:51:27 +02:00
Maxim Poliakovski fcfb2372b9 MPC106: use common PCI configuration code. 2022-05-21 14:51:27 +02:00
Maxim Poliakovski 36098af5eb ATIRage: use common PCI configuration code. 2022-05-21 14:51:27 +02:00
Maxim Poliakovski d863fa0c80 Bandit: use common PCI configuration code. 2022-05-21 14:51:27 +02:00
Maxim Poliakovski 1f67a57d7b Common code for handling PCI config space. 2022-05-21 14:51:26 +02:00
Maxim Poliakovski 9d7d9f3103 MACE: add MaceEnet namespace. 2022-05-21 14:51:26 +02:00
Maxim Poliakovski 8d9691cc6f MESH: add MeshScsi namespace. 2022-05-21 14:51:26 +02:00
Maxim Poliakovski 6c564cb720 amic: fix MACE register range. 2022-05-21 14:51:26 +02:00
Maxim Poliakovski 4b32e9bd30 Initial emulation of the Platinum Memory controller. 2022-05-21 14:51:26 +02:00
Maxim Poliakovski 23a046d889 Refine Bandit config address handling. 2022-05-21 14:51:26 +02:00
Maxim Poliakovski cdb34032bc Initial emulation of the Bandit ARBus-to-PCI bridge. 2022-05-21 14:51:26 +02:00
Maxim Poliakovski 74f4dbd7e6 pcihost: pull common code from Grackle. 2022-05-21 14:51:26 +02:00
Maxim Poliakovski 8889759f33 GrandCentral: improve error logging. 2022-05-21 14:45:33 +02:00
dingusdev 372a5dfe1a Merge pull request #26 from dingusdev/gh-actions
Create cmake.yml
2022-05-20 10:47:01 -07:00
Maxim Poliakovski 22988f9e28 machinecatalyst: disable ATI Mach64 card hack. 2022-05-20 09:26:04 +02:00
Maxim Poliakovski de2b149056 Create cmake.yml 2022-05-19 23:57:30 +02:00
Maxim Poliakovski 533edf6ef5 GrandCentral: properly connect Curio SCSI. 2022-05-17 13:27:58 +02:00
Maxim Poliakovski 9d89c5f64e ppcopcodes: fix mcrf (again). 2022-05-17 08:44:50 +02:00
Maxim Poliakovski b26baaaeff CharIoStdin: disable SIGINT generation with Ctrl-C. 2022-05-07 23:42:33 +02:00
Maxim Poliakovski e0b94e0b47 ESCC: connect NULL and STDIO backends. 2022-05-07 21:47:22 +02:00
Maxim Poliakovski d4c08bbe31 Fix ESCC register addressing. 2022-05-07 21:47:22 +02:00
Maxim Poliakovski be4f835e6c New machine: Catalyst (PM7200). 2022-05-07 21:47:14 +02:00
Maxim Poliakovski 57b102dae2 ATI Mach64 GX controller emulation. 2022-04-13 23:31:44 +02:00
Maxim Poliakovski 0e09ecdfa4 displayid: constructor with parameters. 2022-04-13 23:31:44 +02:00
Maxim Poliakovski 8fcdc5298a videoctrl: add pixel_depth member. 2022-04-13 23:31:44 +02:00
Maxim Poliakovski 3a4f22f459 Move ATI Mach64 definitions to separate header. 2022-04-13 23:31:44 +02:00
Maxim Poliakovski b78f17c161 pcidevice: loading of expansion ROMs from files. 2022-04-13 23:31:44 +02:00
Maxim Poliakovski 4bba61a920 Bandit: implement I/O space transactions. 2022-04-13 23:31:44 +02:00
Maxim Poliakovski d64f901f85 Bandit: implement address mask register. 2022-04-13 23:31:44 +02:00
Maxim Poliakovski 883aac2d05 ofnvram: implement changing of string variables. 2022-04-13 23:27:53 +02:00
Maxim Poliakovski f6e5d72e3c videoctrl: fix surface size. 2022-04-13 23:27:53 +02:00
Maxim Poliakovski c967eb3c65 ATIRage: hack to support OF output. 2022-04-13 23:27:53 +02:00
Maxim Poliakovski 89d453b4be debugger: add printenv and setenv commands. 2022-04-13 23:27:53 +02:00
Maxim Poliakovski ba8e3b657c Utility class for viewing/changing OF NVRAM variables. 2022-03-29 01:55:11 +02:00
Maxim Poliakovski 4d87ed9b38 Make NVRAM a full-fledged HW component. 2022-03-29 01:55:11 +02:00
Maxim Poliakovski d71a7b8694 GrandCentral: basic device interrupt handling. 2022-03-28 18:33:59 +02:00
Maxim Poliakovski e01d0e3d59 GrandCentral: external SCSI (Curio style). 2022-03-28 18:26:47 +02:00
Maxim Poliakovski 1d86375061 Implement MPC601 style RTC. 2022-03-28 18:26:47 +02:00
Maxim Poliakovski 4525fd50cc GrandCentral: fix access to NVRAM subdevice. 2022-03-28 18:26:47 +02:00
Maxim Poliakovski 29ce960dbf GrandCentral: access to ESCC and board reg 1. 2022-03-28 18:26:47 +02:00
Maxim Poliakovski ac5078f133 Platinum: implement memory controller registers. 2022-03-28 18:26:47 +02:00
Maxim Poliakovski 276cd37cfe Heathrow: interrupt registers & mode 1 interrupts. 2022-03-14 18:13:47 +01:00
Maxim Poliakovski 1500c63e26 Initial emulation of the GrandCentral I/O controller. 2022-03-14 18:13:47 +01:00
Maxim Poliakovski 4c45b3dfa2 Heathrow: use common PCI configuration code. 2022-03-14 18:13:47 +01:00
Maxim Poliakovski 574677490f MPC106: use common PCI configuration code. 2022-03-14 18:13:47 +01:00
Maxim Poliakovski e47b66e1af ATIRage: use common PCI configuration code. 2022-03-14 18:13:47 +01:00
Maxim Poliakovski 73aa68bc30 Bandit: use common PCI configuration code. 2022-03-14 18:13:47 +01:00
Maxim Poliakovski 3bce7bb1ea Common code for handling PCI config space. 2022-03-14 18:13:47 +01:00
Maxim Poliakovski 6d004f0bf8 MACE: add MaceEnet namespace. 2022-03-14 18:13:47 +01:00
Maxim Poliakovski e913f39812 MESH: add MeshScsi namespace. 2022-03-14 18:13:47 +01:00
Maxim Poliakovski 3235018260 amic: fix MACE register range. 2022-03-14 18:13:47 +01:00
Maxim Poliakovski a7e4dc9d83 Initial emulation of the Platinum Memory controller. 2022-03-14 18:13:47 +01:00
Maxim Poliakovski 13f18c416d Refine Bandit config address handling. 2022-03-14 18:13:47 +01:00
Maxim Poliakovski 400ce0b713 Initial emulation of the Bandit ARBus-to-PCI bridge. 2022-03-14 18:13:47 +01:00
Maxim Poliakovski 289df32817 pcihost: pull common code from Grackle. 2022-03-14 18:13:47 +01:00
dingusdev 36fa53e8c1 MSVC compilation fixes 2022-03-12 15:43:45 -07:00
dingusdev 86bc91028d Removed non-functioning interpreter code 2022-03-02 19:22:13 -07:00
Maxim Poliakovski d83fdd8866 ppcexec: remove old code. 2022-03-02 17:02:32 +01:00
Maxim Poliakovski 646880cbf2 Interpreter loop is now controlled by exec_flags. 2022-03-02 16:55:20 +01:00
dingusdev 8d3427c475 Proper write protection settings 2022-02-27 22:41:59 -07:00
Maxim Poliakovski 827d7f10dd pdmonboard: reduce logging messages. 2022-02-26 13:37:47 +01:00
Maxim Poliakovski 932a6ae01e debugger: GO command. 2022-02-26 12:47:51 +01:00
Maxim Poliakovski 4de10898ea Improve ESCC stub to bypass LocalTalk. 2022-02-26 10:57:13 +01:00
Maxim Poliakovski 205b5a4956 escc: implement reset commands. 2022-02-26 10:57:13 +01:00
Maxim Poliakovski c946693450 escc: unify compatible and MacRISC addressing. 2022-02-26 10:57:13 +01:00
Maxim Poliakovski f1ed56ae9a debugger: a couple of cosmetic improvements. 2022-02-26 10:57:13 +01:00
dingusdev 80a4864a92 Floppy disk write protection 2022-02-24 07:33:30 -07:00
Maxim Poliakovski edd3979647 Cuda: increase size of the input buffer. 2022-02-19 23:23:24 +01:00
Maxim Poliakovski 123f820775 ppcopcodes: fix lswi/stswi emulation. 2022-02-19 23:23:24 +01:00
Maxim Poliakovski 477ad7ddee ppcopcodes: fix lswx/stswx emulation. 2022-02-19 23:23:24 +01:00
Maxim Poliakovski 689fe51d80 Add required includes for gcc. 2022-02-17 00:50:37 +01:00
Maxim Poliakovski b39e884d61 ppcfpopcodes: add missing std:: prefixes. 2022-02-17 00:20:18 +01:00
Maxim Poliakovski de65196c4f ppcfpopcodes: remove unused template parameters. 2022-02-17 00:11:14 +01:00
Maxim Poliakovski 8f528184b8 Fix compilation with gcc, part 1. 2022-02-16 23:52:43 +01:00
Maxim Poliakovski 2ce2cae48c SWIM3: implement disk reading. 2022-02-15 15:55:16 +01:00
Maxim Poliakovski 579a56f749 AMIC: implement floppy DMA channel. 2022-02-15 15:54:21 +01:00
Maxim Poliakovski 2525398b6e SWIM3: add support for floppy DMA. 2022-02-15 15:53:18 +01:00
Maxim Poliakovski e91843034b Superdrive: method for retrieving disk data. 2022-02-15 15:49:12 +01:00
Maxim Poliakovski 9da4a9ec6a SWIM3: respect interrupt enable flag in mode register. 2022-02-14 23:06:07 +01:00
Maxim Poliakovski cfb8977f09 AMIC: implement floppy DMA registers. 2022-02-13 23:47:45 +01:00
Maxim Poliakovski 54107b2aac SWIM3: track seeking and header reading. 2022-02-13 03:07:32 +01:00
Maxim Poliakovski 8d8cecbaba Superdrive: implement track seeking. 2022-02-13 03:05:55 +01:00
Maxim Poliakovski 0d3fd01fef Improve emulation of the VIA timers. 2022-02-13 03:02:17 +01:00
Maxim Poliakovski 9f3f46603f AMIC: handle SWIM3 interrupts. 2022-02-07 23:10:17 +01:00
Maxim Poliakovski c77155199b Superdrive: report track zero status. 2022-02-07 23:05:58 +01:00
Maxim Poliakovski 1872eca44f SWIM3: implement head stepping. 2022-02-07 23:05:58 +01:00
Maxim Poliakovski 9aaf441625 Superdrive: more commands and status requests. 2022-02-07 15:05:57 +01:00
Maxim Poliakovski b9fbd9b7c9 Superdrive: support for inserting of virtual disks. 2022-02-06 21:23:20 +01:00
Maxim Poliakovski 5e2f2b12e4 Properly connect Superdrive to SWIM3 and machines. 2022-02-06 15:23:30 +01:00
Maxim Poliakovski b25b526582 hwcomponent: add floppy disk drive component type. 2022-02-06 15:20:07 +01:00
Maxim Poliakovski dea863b6e6 Superdrive: support disk-in-drive status. 2022-02-06 03:25:35 +01:00
Maxim Poliakovski 00093bdc95 sc53c94: support interrupts. 2022-02-06 01:50:54 +01:00
Maxim Poliakovski 298135fd7a AMIC: process VIA2 interrupts. 2022-02-06 01:50:54 +01:00
Maxim Poliakovski 7c53620a40 sc53c94: implement sequencer and some commands. 2022-02-06 01:50:54 +01:00
Maxim Poliakovski 2edb50a821 machinepdm: add internal SCSI bus. 2022-02-06 01:50:54 +01:00
Maxim Poliakovski b5f70feb28 Initial SCSI bus emulation. 2022-02-06 01:50:54 +01:00
Maxim Poliakovski 25a4fd1107 Allow adding timers from timer callbacks. 2022-02-05 17:10:57 +01:00
Maxim Poliakovski 5c177cc50f Simplify registration of HW component types. 2022-01-26 16:45:21 +01:00
Maxim Poliakovski dc34f282b7 53C94: support more registers and commands. 2022-01-24 22:55:49 +01:00
dingusdev 3258abe190 crnand and crnor fixes 2022-01-22 22:33:13 -07:00
Maxim Poliakovski 5883524fb8 53C94: chip initialization and identification. 2022-01-22 04:37:52 +01:00
Maxim Poliakovski c8d39d5ee5 ppcopcodes: fix creqv emulation. 2022-01-21 16:32:07 +01:00
Maxim Poliakovski 2442bd17b3 ppcopcodes: fix crand emulation. 2022-01-21 14:56:10 +01:00
Maxim Poliakovski c864b9b7d9 ppcopcodes: fix cror emulation. 2022-01-21 14:37:51 +01:00
Maxim Poliakovski 3bdc6f915a AMIC: implement periodic VBL (60.15 Hz) interrupt. 2022-01-21 12:42:05 +01:00
Maxim Poliakovski 0899186ffc ViaCuda: implement post-initialization. 2022-01-21 12:42:05 +01:00
Maxim Poliakovski c1208b398e Add posti-initialization to HW components. 2022-01-21 12:42:05 +01:00
Maxim Poliakovski 4867a68e11 VIA: public method for asserting control lines. 2022-01-21 11:08:32 +01:00
Maxim Poliakovski d8c3cfc38e Fix next instruction address after external exceptions. 2022-01-20 01:38:41 +01:00
Maxim Poliakovski 604b4bc2e5 Fix main CMakeLists.txt to include core for all targets. 2022-01-19 22:18:00 +01:00
Maxim Poliakovski e1e651966e Revert "Further condition reg opcode patches"
This reverts commit 756d32df07.
2022-01-10 18:10:39 +01:00
Maxim Poliakovski c12bab9e27 Revert "Quick fix for creqv"
This reverts commit 8efc61e1b9.
2022-01-10 18:01:48 +01:00
Maxim Poliakovski 9c4e6c8a86 Rewrite ViaCuda to support SR and T2 interrupts. 2022-01-10 17:56:24 +01:00
Maxim Poliakovski c218badd5a Clean up some ctors/dtors. 2022-01-10 17:56:24 +01:00
Maxim Poliakovski d61d1d71eb Add interrupt processing to AMIC. 2022-01-10 17:56:24 +01:00
Maxim Poliakovski d9d8384d4a Add interrupt controller interface and definitions. 2022-01-10 17:56:24 +01:00
Maxim Poliakovski aefc66d118 Fix external exception processing. 2022-01-10 17:56:24 +01:00
Maxim Poliakovski d4ecb77b24 pdmonboard: enable periodic video updates. 2022-01-10 17:56:24 +01:00
Maxim Poliakovski 339db4a078 Add timers management. 2022-01-10 17:56:24 +01:00
dingusdev 8efc61e1b9 Quick fix for creqv 2022-01-08 15:22:27 -07:00
dingusdev 756d32df07 Further condition reg opcode patches 2022-01-07 20:40:07 -07:00
Maxim Poliakovski 9cd7ca0077 ppcopcodes: fix crxor. 2022-01-08 04:21:09 +01:00
Maxim Poliakovski 7bdad7703c Predict TBR values based on elapsed virtual time. 2021-12-20 00:12:44 +01:00
Maxim Poliakovski 087402290d Implement virtual time based on instruction counting. 2021-12-20 00:10:02 +01:00
Maxim Poliakovski 9a0c340712 Basic SWIM3 and Superdrive emulation. 2021-12-12 21:40:04 +01:00
Maxim Poliakovski fd33c10712 ppcmmu.cpp: remove unused local variables. 2021-12-07 23:23:39 +01:00
Maxim Poliakovski 9caaf0f538 Basic emulation of the PDM on-board video. 2021-12-07 22:54:03 +01:00
Maxim Poliakovski 476d893094 videoctrl: add framebuffer conversion callback. 2021-12-07 22:47:25 +01:00
Maxim Poliakovski fff597075d Monitor type can be now specified from the command line. 2021-12-06 00:40:40 +01:00
Maxim Poliakovski 793335d9b8 Clean up includes. 2021-12-05 20:01:57 +01:00
Maxim Poliakovski f609839894 machineproperties: improve StrProperty class. 2021-12-05 19:16:39 +01:00
Maxim Poliakovski f39188beb1 Initial support for floppy disk images. 2021-12-04 14:22:02 +01:00
Maxim Poliakovski decb8342be Delete unused machinepresets.h header. 2021-12-04 12:05:47 +01:00
Maxim Poliakovski 609fb43726 Rewrite DisplayID to work with AMIC & ATI Rage. 2021-11-30 01:26:32 +01:00
Maxim Poliakovski c2991149fd zdocs/openfirmware.md: minor corrections. 2021-11-29 22:35:12 +01:00
Maxim Poliakovski 99f5aba12e atirage: replace raw pointers with unique_ptr. 2021-11-17 23:30:43 +01:00
Maxim Poliakovski cbf4e266e1 atirage: better name for HW registers. 2021-11-11 14:57:31 +01:00
dingusdev 84ded9fc7a Added further CUDA commands 2021-11-10 07:56:50 -07:00
Maxim Poliakovski a01cd9c993 Make display ID method selectable in video controller. 2021-11-09 14:15:21 +01:00
Maxim Poliakovski fc44cdcc83 Heathrow: logging monitor sense status. 2021-11-09 13:41:48 +01:00
Maxim Poliakovski 958d3ee96a Factor out common video controller code. 2021-11-09 13:40:13 +01:00
dingusdev c2a63bab09 FP Opcode Test Fixes 2021-10-30 16:43:13 -07:00
Maxim Poliakovski 392fa87ba4 Add NCR 53C90 stub. 2021-10-26 19:00:04 +02:00
Maxim Poliakovski 87b8e1759a Connect ESCC to AMIC and Heathrow. 2021-10-25 22:19:45 +02:00
Maxim Poliakovski cb946e41b5 Initial ESCC emulation. 2021-10-25 22:19:45 +02:00
Maxim Poliakovski 3f20d0a700 heathrow: use unique_ptr with internal objects. 2021-10-25 22:19:45 +02:00
dingusdev 767735251b FP comp tests & various fixes 2021-10-24 14:00:35 -07:00
Maxim Poliakovski 6a756df5e3 Add MACE Ethernet emulation stub. 2021-10-24 21:02:30 +02:00
Maxim Poliakovski 281aaa1902 Remove libsoundio library. 2021-10-23 23:10:40 +02:00
Maxim Poliakovski c7f21040ae Update cubeb submodule. 2021-10-23 22:08:23 +02:00
dingusdev a28ef677fd Fixing FP mul ops & tests 2021-10-23 13:05:20 -07:00
Maxim Poliakovski d70adac564 Better name for 68k debugger build control option. 2021-10-23 21:51:16 +02:00
Maxim Poliakovski c0cd6eb38f Add missing licence headers, update license date. 2021-10-23 21:00:31 +02:00
Maxim Poliakovski 9329d56d83 Move devices into dedicated subdirectories. 2021-10-23 20:17:47 +02:00
dingusdev 7919cd0590 Merge pull request #21 from dingusdev/floating-point
Floating point fixes. Rehauled floating-point emulation code. Fixed a test suite for floating points.
2021-10-19 07:18:21 -07:00
Maxim Poliakovski 7daf4aa317 viacuda: improve READ_MCU_MEM & WRITE_MCU_MEM emulation.
68k boot code in ROM uses those commands for applying patches
to Cuda and getting Cuda firmware version. This commit
implements as much as needed for boot code to work.
2021-10-18 23:52:12 +02:00
Maxim Poliakovski f194887d34 viacuda: properly initialize VIA registers. 2021-10-18 16:38:12 +02:00
Maxim Poliakovski 46549d68a2 viacuda: remove superfluous newlines and casts from messages. 2021-10-18 16:38:12 +02:00
Maxim Poliakovski c7544d9c2f amic: reorganize registers in blocks. 2021-10-16 15:07:53 +02:00
Maxim Poliakovski 2f725fe3e4 viacuda: fix PRAM reading and writing. 2021-10-14 00:01:30 +02:00
Maxim Poliakovski e53296f7a9 Implement all required context-synchronizing events. 2021-10-13 20:58:09 +02:00
Maxim Poliakovski 7c47b9c1e7 amic: implement AMIC2 identification. 2021-10-13 09:06:16 +02:00
Maxim Poliakovski e66979d30f debugger: improve disassembling of 68k F-Traps. 2021-10-13 09:04:52 +02:00
Maxim Poliakovski 9caef55c19 amic: implement diagnostics register. 2021-10-10 22:01:02 +02:00
Maxim Poliakovski 2a9d364b1b ppcmmu: implement separate TLB pointers for read/write. 2021-10-10 22:01:02 +02:00
Maxim Poliakovski 3eb2d3cba7 ppcmmu: verbose MMU state with mmu_print_regs(). 2021-10-10 22:01:02 +02:00
Maxim Poliakovski bde5b71167 ppcmmu: re-arrange code to avoid formward declarations. 2021-10-10 22:01:02 +02:00
Maxim Poliakovski 89e79d05cb Load bootrom code to primary ROM region. 2021-10-10 22:01:02 +02:00
Maxim Poliakovski bb0ca2ac40 ppcmmu: fix TLB flushing. 2021-10-10 22:01:02 +02:00
192 changed files with 14629 additions and 22472 deletions

No files matched your search

+43
View File
@@ -0,0 +1,43 @@
name: CMake
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
env:
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
BUILD_TYPE: Release
jobs:
build:
# The CMake configure and build commands are platform agnostic and should work equally well on Windows or Mac.
# You can convert this to a matrix build if you need cross-platform coverage.
# See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: install_dependencies
run: |
sudo apt-get update -y -qq
sudo apt-get install libsdl2-dev
- name: init_submodules
run: git submodule update --init --recursive
- name: Configure CMake
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DDPPC_68K_DEBUGGER=ON
- name: Build
# Build your program with the given configuration
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}
- name: Test
working-directory: ${{github.workspace}}/build
# Execute tests defined by the CMake configuration.
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: ctest -C ${{env.BUILD_TYPE}}
+16 -13
View File
@@ -21,9 +21,9 @@ endif()
option(DPPC_BUILD_PPC_TESTS "Build PowerPC tests" OFF)
option(DPPC_BUILD_BENCHMARKS "Build benchmarking programs" OFF)
option(ENABLE_68K_DEBUGGER "Enable 68k debugging" OFF)
option(DPPC_68K_DEBUGGER "Enable 68k debugging" OFF)
if (ENABLE_68K_DEBUGGER)
if (DPPC_68K_DEBUGGER)
# Build capstone as static library.
set(CAPSTONE_BUILD_STATIC ON CACHE BOOL "")
@@ -56,10 +56,10 @@ if (ENABLE_68K_DEBUGGER)
add_subdirectory(thirdparty/capstone EXCLUDE_FROM_ALL)
endif()
add_subdirectory("${PROJECT_SOURCE_DIR}/core")
add_subdirectory("${PROJECT_SOURCE_DIR}/cpu/ppc/")
add_subdirectory("${PROJECT_SOURCE_DIR}/debugger/")
add_subdirectory("${PROJECT_SOURCE_DIR}/devices/")
add_subdirectory("${PROJECT_SOURCE_DIR}/execution")
add_subdirectory("${PROJECT_SOURCE_DIR}/machines/")
add_subdirectory("${PROJECT_SOURCE_DIR}/utils/")
add_subdirectory("${PROJECT_SOURCE_DIR}/thirdparty/loguru/")
@@ -79,7 +79,9 @@ add_subdirectory(thirdparty/cubeb EXCLUDE_FROM_ALL)
set(CLI11_ROOT ${PROJECT_SOURCE_DIR}/thirdparty/CLI11)
include_directories("${PROJECT_SOURCE_DIR}" "${PROJECT_SOURCE_DIR}/devices"
include_directories("${PROJECT_SOURCE_DIR}"
"${PROJECT_SOURCE_DIR}/core"
"${PROJECT_SOURCE_DIR}/devices"
"${PROJECT_SOURCE_DIR}/cpu/ppc"
"${PROJECT_SOURCE_DIR}/debugger"
"${PROJECT_SOURCE_DIR}/utils"
@@ -99,10 +101,10 @@ file(GLOB SOURCES "${PROJECT_SOURCE_DIR}/*.cpp"
file(GLOB TEST_SOURCES "${PROJECT_SOURCE_DIR}/cpu/ppc/test/*.cpp")
add_executable(dingusppc ${SOURCES} $<TARGET_OBJECTS:debugger>
add_executable(dingusppc ${SOURCES} $<TARGET_OBJECTS:core>
$<TARGET_OBJECTS:cpu_ppc>
$<TARGET_OBJECTS:debugger>
$<TARGET_OBJECTS:devices>
$<TARGET_OBJECTS:execution>
$<TARGET_OBJECTS:machines>
$<TARGET_OBJECTS:utils>
$<TARGET_OBJECTS:loguru>)
@@ -110,6 +112,7 @@ add_executable(dingusppc ${SOURCES} $<TARGET_OBJECTS:debugger>
if (WIN32)
target_link_libraries(dingusppc "${PROJECT_SOURCE_DIR}/thirdparty/SDL2/lib/x64/SDL2.lib"
"${PROJECT_SOURCE_DIR}/thirdparty/SDL2/lib/x64/SDL2main.lib"
ws2_32
cubeb)
else()
#target_link_libraries(dingusppc libsoundio_static ${LIBSOUNDIO_LIBS} ${SDL2_LIBRARIES})
@@ -117,15 +120,15 @@ target_link_libraries(dingusppc cubeb ${SDL2_LIBRARIES} ${CMAKE_DL_LIBS} ${CMAKE
target_include_directories(dingusppc PRIVATE ${CLI11_ROOT})
endif()
if (ENABLE_68K_DEBUGGER)
if (DPPC_68K_DEBUGGER)
target_link_libraries(dingusppc capstone-static)
endif()
if (DPPC_BUILD_PPC_TESTS)
add_executable(testppc ${TEST_SOURCES} $<TARGET_OBJECTS:debugger>
add_executable(testppc ${TEST_SOURCES} $<TARGET_OBJECTS:core>
$<TARGET_OBJECTS:cpu_ppc>
$<TARGET_OBJECTS:debugger>
$<TARGET_OBJECTS:devices>
$<TARGET_OBJECTS:execution>
$<TARGET_OBJECTS:machines>
$<TARGET_OBJECTS:utils>
$<TARGET_OBJECTS:loguru>)
@@ -141,17 +144,17 @@ if (DPPC_BUILD_PPC_TESTS)
${CMAKE_THREAD_LIBS_INIT})
endif()
if (ENABLE_68K_DEBUGGER)
if (DPPC_68K_DEBUGGER)
target_link_libraries(testppc capstone-static)
endif()
endif()
if (DPPC_BUILD_BENCHMARKS)
file(GLOB BENCH_SOURCES "${PROJECT_SOURCE_DIR}/benchmark/*.cpp")
add_executable(bench1 ${BENCH_SOURCES} $<TARGET_OBJECTS:debugger>
add_executable(bench1 ${BENCH_SOURCES} $<TARGET_OBJECTS:core>
$<TARGET_OBJECTS:cpu_ppc>
$<TARGET_OBJECTS:debugger>
$<TARGET_OBJECTS:devices>
$<TARGET_OBJECTS:execution>
$<TARGET_OBJECTS:machines>
$<TARGET_OBJECTS:utils>
$<TARGET_OBJECTS:loguru>)
@@ -159,7 +162,7 @@ if (DPPC_BUILD_BENCHMARKS)
target_link_libraries(bench1 cubeb ${SDL2_LIBRARIES} ${CMAKE_DL_LIBS}
${CMAKE_THREAD_LIBS_INIT})
if (ENABLE_68K_DEBUGGER)
if (DPPC_68K_DEBUGGER)
target_link_libraries(bench1 capstone-static)
endif()
endif()
+2 -2
View File
@@ -12,7 +12,7 @@ This program aims to not only improve upon what Sheepshaver, PearPC, and other P
## Implemented Features
This emulator has a debugging environment, complete with a disassembler. We also have implemented enough to allow OpenFirmware to boot, going so far as to allow audio playback of the boot-up jingles.
This emulator has a debugging environment, complete with a disassembler. We also have implemented enough to allow Open Firmware to boot, going so far as to allow audio playback of the boot-up jingles.
## How to Use
@@ -52,7 +52,7 @@ You need to install development tools first.
At least, a C++11 compliant compiler and [CMake](https://cmake.org) are required.
You will also have to recursive clone or run
You will also have to recursive clone or run
```
git submodule update --init --recursive
```
+21
View File
@@ -1,3 +1,24 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-21 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <chrono>
#include "cpu/ppc/ppcemu.h"
+8
View File
@@ -0,0 +1,8 @@
include_directories("${PROJECT_SOURCE_DIR}"
"${PROJECT_SOURCE_DIR}/thirdparty/loguru/"
)
file(GLOB SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")
add_library(core OBJECT ${SOURCES})
target_link_libraries(core)
+139
View File
@@ -0,0 +1,139 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** Timer management system. */
#include <loguru.hpp>
#include "timermanager.h"
#include <cinttypes>
#include <memory>
TimerManager* TimerManager::timer_manager;
uint32_t TimerManager::add_oneshot_timer(uint64_t timeout, timer_cb cb)
{
if (!timeout || timeout <= MIN_TIMEOUT_NS) {
LOG_F(WARNING, "One-shot timer too short, timeout=%llu ns", timeout);
}
TimerInfo* ti = new TimerInfo;
ti->id = ++this->id;
ti->timeout_ns = this->get_time_now() + timeout;
ti->interval_ns = 0;
ti->cb = cb;
std::shared_ptr<TimerInfo> timer_desc(ti);
// add new timer to the timer queue
this->timer_queue.push(timer_desc);
// notify listeners about changes in the timer queue
if (!this->cb_active) {
this->notify_timer_changes();
}
return ti->id;
}
uint32_t TimerManager::add_cyclic_timer(uint64_t interval, timer_cb cb)
{
if (!interval || interval <= MIN_TIMEOUT_NS) {
LOG_F(WARNING, "Cyclic timer interval too short, timeout=%llu ns", interval);
}
TimerInfo* ti = new TimerInfo;
ti->id = ++this->id;
ti->timeout_ns = this->get_time_now() + interval;
ti->interval_ns = interval;
ti->cb = cb;
std::shared_ptr<TimerInfo> timer_desc(ti);
// add new timer to the timer queue
this->timer_queue.push(timer_desc);
// notify listeners about changes in the timer queue
if (!this->cb_active) {
this->notify_timer_changes();
}
return ti->id;
}
void TimerManager::cancel_timer(uint32_t id)
{
TimerInfo* cur_timer = this->timer_queue.top().get();
if (cur_timer->id == id) {
this->timer_queue.pop();
} else {
this->timer_queue.remove_by_id(id);
}
if (!this->cb_active) {
this->notify_timer_changes();
}
}
uint64_t TimerManager::process_timers(uint64_t time_now)
{
TimerInfo* cur_timer;
if (this->timer_queue.empty()) {
return 0ULL;
}
// scan for expired timers
cur_timer = this->timer_queue.top().get();
while (cur_timer->timeout_ns <= time_now ||
cur_timer->timeout_ns <= (time_now + MIN_TIMEOUT_NS)) {
timer_cb cb = cur_timer->cb;
// re-arm cyclic timers
if (cur_timer->interval_ns) {
auto new_timer = this->timer_queue.top();
new_timer->timeout_ns = time_now + cur_timer->interval_ns;
this->timer_queue.pop();
this->timer_queue.push(new_timer);
} else {
// remove one-shot timers from queue
this->timer_queue.pop();
}
this->cb_active = true;
// invoke timer callback
cb();
this->cb_active = false;
// process next timer
if (this->timer_queue.empty()) {
return 0ULL;
}
cur_timer = this->timer_queue.top().get();
}
// return time slice in nanoseconds until next timer's expiry
return cur_timer->timeout_ns - time_now;
}
+121
View File
@@ -0,0 +1,121 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef TIMER_MANAGER_H
#define TIMER_MANAGER_H
#include <algorithm>
#include <cinttypes>
#include <functional>
#include <memory>
#include <queue>
#include <vector>
using namespace std;
#define MIN_TIMEOUT_NS 200
#define NS_PER_SEC 1E9
#define ONE_BILLION_NS 0x3B9ACA00UL
#define USECS_TO_NSECS(us) (us) * 1000
typedef function<void()> timer_cb;
/** Extend std::priority_queue as suggested here:
https://stackoverflow.com/a/36711682
to be able to remove arbitrary elements.
*/
template <typename T, class Container = std::vector<T>, class Compare = std::less<typename Container::value_type>>
class my_priority_queue : public std::priority_queue<T, Container, Compare> {
public:
bool remove_by_id(const uint32_t id){
auto it = std::find_if(
this->c.begin(), this->c.end(), [id](const T& el) { return el->id == id; });
if (it != this->c.end()) {
this->c.erase(it);
std::make_heap(this->c.begin(), this->c.end(), this->comp);
return true;
} else {
return false;
}
};
};
typedef struct TimerInfo {
uint32_t id;
uint64_t timeout_ns; // timer expiry
uint64_t interval_ns; // 0 for one-shot timers
timer_cb cb; // timer callback
} TimerInfo;
// Custom comparator for sorting our timer queue in ascending order
class MyGtComparator {
public:
bool operator()(const shared_ptr<TimerInfo>& l, const shared_ptr<TimerInfo>& r) {
return l.get()->timeout_ns > r.get()->timeout_ns;
}
};
class TimerManager {
public:
static TimerManager* get_instance() {
if (!timer_manager) {
timer_manager = new TimerManager();
}
return timer_manager;
};
// callback for retrieving current time
void set_time_now_cb(const function<uint64_t()> &cb) {
this->get_time_now = cb;
};
// callback for acknowledging time changes
void set_notify_changes_cb(const timer_cb &cb) {
this->notify_timer_changes = cb;
};
// return current virtual time in nanoseconds
uint64_t current_time_ns() { return get_time_now(); };
// creating and cancelling timers
uint32_t add_oneshot_timer(uint64_t timeout, timer_cb cb);
uint32_t add_cyclic_timer(uint64_t interval, timer_cb cb);
void cancel_timer(uint32_t id);
uint64_t process_timers(uint64_t time_now);
private:
static TimerManager* timer_manager;
TimerManager(){}; // private constructor to implement a singleton
// timer queue
my_priority_queue<shared_ptr<TimerInfo>, vector<shared_ptr<TimerInfo>>, MyGtComparator> timer_queue;
function<uint64_t()> get_time_now;
function<void()> notify_timer_changes;
uint32_t id = 0;
bool cb_active = false; // true if a timer callback is executing
};
#endif // TIMER_MANAGER_H
+10 -10
View File
@@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
Copyright (C) 2018-21 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@@ -42,7 +42,7 @@ inline void power_setsoov(uint32_t a, uint32_t b, uint32_t d) {
}
}
/** mask generator for rotate and shift instructions (§ 4.2.1.4 PowerpC PEM) */
/** mask generator for rotate and shift instructions (§ 4.2.1.4 PowerpC PEM) */
static inline uint32_t power_rot_mask(unsigned rot_mb, unsigned rot_me) {
uint32_t m1 = 0xFFFFFFFFUL >> rot_mb;
uint32_t m2 = 0xFFFFFFFFUL << (31 - rot_me);
@@ -154,9 +154,9 @@ void dppc_interpreter::power_lscbx() {
ppc_result_d = 0xFFFFFFFF;
ppc_store_result_regd();
}
switch (byte_offset) {
switch (byte_offset) {
case 0:
bitmask = 0x00FFFFFF;
shift_amount = 24;
@@ -174,7 +174,7 @@ void dppc_interpreter::power_lscbx() {
shift_amount = 0;
break;
}
return_value = mmu_read_vmem<uint8_t>(ppc_effective_address);
// return_value = mem_grab_byte(ppc_effective_address);
ppc_result_d = (ppc_result_d & bitmask) | (return_value << shift_amount);
@@ -185,7 +185,7 @@ void dppc_interpreter::power_lscbx() {
//Match has been found - Time to break out
break;
}
byte_offset += 8;
if (byte_offset == 32) {
@@ -211,10 +211,10 @@ void dppc_interpreter::power_maskg() {
if (mask_start < (mask_end + 1)) {
insert_mask = power_rot_mask(mask_start, mask_end);
}
}
else if (mask_start == (mask_end + 1)) {
insert_mask = 0xFFFFFFFF;
}
}
else {
insert_mask = ~(power_rot_mask(mask_end + 1, mask_start - 1));
}
@@ -359,7 +359,7 @@ void dppc_interpreter::power_sllq() {
if (ppc_result_b >= 0x20) {
ppc_result_a = (ppc_state.spr[SPR::MQ] & mask);
}
}
else {
ppc_result_a = ((r & mask) | (ppc_state.spr[SPR::MQ] & ~mask));
}
@@ -508,7 +508,7 @@ void dppc_interpreter::power_srlq() {
if (ppc_result_b >= 0x20) {
ppc_result_a = (ppc_state.spr[SPR::MQ] & mask);
}
}
else {
ppc_result_a = ((r & mask) | (ppc_state.spr[SPR::MQ] & ~mask));
}
+1 -1
View File
@@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
Copyright (C) 2018-21 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
+1 -1
View File
@@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
Copyright (C) 2018-21 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
+57 -43
View File
@@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
Copyright (C) 2018-21 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@@ -22,8 +22,9 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#ifndef PPCEMU_H
#define PPCEMU_H
#include "devices/memctrlbase.h"
#include "endianswap.h"
#include <devices/memctrl/memctrlbase.h>
#include <endianswap.h>
#include <cinttypes>
#include <functional>
#include <setjmp.h>
@@ -83,22 +84,29 @@ extern SetPRS ppc_state;
/** symbolic names for frequently used SPRs */
enum SPR : int {
MQ = 0,
XER = 1,
LR = 8,
CTR = 9,
DSISR = 18,
DAR = 19,
DEC = 22,
SDR1 = 25,
SRR0 = 26,
SRR1 = 27,
PVR = 287
MQ = 0,
XER = 1,
RTCU_U = 4, // user RTCU
RTCL_U = 5, // user RTCL
LR = 8,
CTR = 9,
DSISR = 18,
DAR = 19,
RTCU_S = 20, // supervisor RTCU
RTCL_S = 21, // supervisor RTCL
DEC = 22,
SDR1 = 25,
SRR0 = 26,
SRR1 = 27,
TBL_U = 268, // user mode TBL
TBU_U = 269, // user mode TBU
TBL_S = 284, // supervisor TBL
TBU_S = 285, // supervisor TBU
PVR = 287,
HID0 = 1008,
HID1 = 1009,
};
/** symbolic names for frequently used SPRs */
enum TBR : int { TBL = 0, TBU = 1 };
/** symbolic names for common PPC processors */
enum PPC_VER : uint32_t {
MPC601 = 0x00010001,
@@ -147,8 +155,13 @@ SUPERVISOR MODEL
536 - 543 are the Data BAT registers
**/
extern uint32_t opcode_value; // used for interpreting opcodes
extern uint64_t timebase_counter; // used for storing time base value
extern uint32_t opcode_value; // used for interpreting opcodes
extern uint64_t timebase_counter;
extern uint64_t tbr_wr_timestamp;
extern uint64_t tbr_wr_value;
extern uint64_t tbr_freq_hz;
extern uint32_t rtc_lo, rtc_hi;
// Additional steps to prevent overflow?
extern int32_t add_result;
@@ -191,13 +204,12 @@ extern uint64_t ppc_result64_b;
extern uint64_t ppc_result64_c;
extern uint64_t ppc_result64_d;
/* The precise end of a basic block. */
enum class BB_end_kind {
BB_NONE = 0, /* no basic block end is reached */
BB_BRANCH = 1, /* a branch instruction is encountered */
BB_EXCEPTION, /* an exception is occured */
BB_RFI /* the rfi instruction is encountered */
/* Flags for controlling interpreter execution. */
enum {
EXEF_BRANCH = 1 << 0,
EXEF_EXCEPTION = 1 << 1,
EXEF_RFI = 1 << 2,
EXEF_TIMER = 1 << 7
};
enum CR_select : int32_t {
@@ -206,14 +218,13 @@ enum CR_select : int32_t {
};
enum CRx_bit : uint32_t {
CR_SO = 0,
CR_EQ,
CR_GT,
CR_LT
};
CR_SO = 28,
CR_EQ = 29,
CR_GT = 30,
CR_LT = 31 };
enum CR1_bit : uint32_t {
CR1_OX = 24,
CR1_OX = 24,
CR1_VX,
CR1_FEX,
CR1_FX,
@@ -259,7 +270,7 @@ enum FPOP : int {
SUB = 0x14,
ADD = 0x15,
MUL = 0x19,
FMSUB = 0x1C,
FMSUB = 0x1C,
FMADD = 0x1D,
FNMSUB = 0x1E,
FNMADD = 0x1F,
@@ -288,15 +299,14 @@ enum Exc_Cause : uint32_t {
TRAP = 1 << (31 - 14),
};
// extern bool bb_end;
extern BB_end_kind bb_kind;
extern unsigned exec_flags;
extern jmp_buf exc_env;
extern bool grab_exception;
extern bool grab_return;
extern bool power_on;
extern bool int_pin;
extern bool is_601; // For PowerPC 601 Emulation
extern bool is_altivec; // For Altivec Emulation
@@ -320,11 +330,13 @@ extern uint64_t exceptions_processed;
#endif
// Function prototypes
extern void ppc_cpu_init(MemCtrlBase* mem_ctrl, uint32_t cpu_version);
extern void ppc_cpu_init(MemCtrlBase* mem_ctrl, uint32_t cpu_version, uint64_t tb_freq);
extern void ppc_mmu_init(uint32_t cpu_version);
[[noreturn]] void ppc_illegalop();
[[noreturn]] void ppc_fpu_off();
void ppc_illegalop();
void ppc_fpu_off();
void ppc_assert_int();
void ppc_release_int();
//void ppc_opcode4();
void ppc_opcode16();
@@ -360,14 +372,14 @@ void ppc_changecrf0(uint32_t set_result);
void ppc_fp_changecrf1();
/* Exception handlers. */
[[noreturn]] void ppc_exception_handler(Except_Type exception_type, uint32_t srr1_bits);
void ppc_exception_handler(Except_Type exception_type, uint32_t srr1_bits);
[[noreturn]] void dbg_exception_handler(Except_Type exception_type, uint32_t srr1_bits);
// MEMORY DECLARATIONS
extern MemCtrlBase* mem_ctrl_instance;
//typedef std::function<void()> CtxSyncCallback;
extern void add_ctx_sync_action(const std::function<void()> &);
extern void do_ctx_sync(void);
// The functions used by the PowerPC processor
namespace dppc_interpreter {
@@ -615,6 +627,8 @@ extern void power_srq();
// G5+ instructions
extern uint64_t get_virt_time_ns(void);
extern void ppc_main_opcode(void);
extern void ppc_exec(void);
extern void ppc_exec_single(void);
@@ -623,7 +637,7 @@ extern void ppc_exec_dbg(uint32_t start_addr, uint32_t size);
/* debugging support API */
void print_fprs(void); /* print content of the floating-point registers */
uint64_t get_reg(std::string& reg_name); /* get content of the register reg_name */
void set_reg(std::string& reg_name, uint64_t val); /* set reg_name to val */
uint64_t get_reg(std::string reg_name); /* get content of the register reg_name */
void set_reg(std::string reg_name, uint64_t val); /* set reg_name to val */
#endif /* PPCEMU_H */
+21 -8
View File
@@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
Copyright (C) 2018-21 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@@ -21,20 +21,20 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
/** @file Handling of low-level PPC exceptions. */
#include <loguru.hpp>
#include "ppcemu.h"
#include "ppcmmu.h"
#include <setjmp.h>
#include <stdexcept>
#include <string>
jmp_buf exc_env; /* Global exception environment. */
[[noreturn]] void ppc_exception_handler(Except_Type exception_type, uint32_t srr1_bits) {
grab_exception = true;
void ppc_exception_handler(Except_Type exception_type, uint32_t srr1_bits) {
#ifdef CPU_PROFILING
exceptions_processed++;
#endif
bb_kind = BB_end_kind::BB_EXCEPTION;
switch (exception_type) {
case Except_Type::EXC_SYSTEM_RESET:
@@ -61,7 +61,11 @@ jmp_buf exc_env; /* Global exception environment. */
break;
case Except_Type::EXC_EXT_INT:
ppc_state.spr[SPR::SRR0] = ppc_next_instruction_address;
if (exec_flags & ~EXEF_TIMER) {
ppc_state.spr[SPR::SRR0] = ppc_next_instruction_address;
} else {
ppc_state.spr[SPR::SRR0] = (ppc_state.pc & 0xFFFFFFFCUL) + 4;
}
ppc_next_instruction_address = 0x0500;
break;
@@ -96,8 +100,7 @@ jmp_buf exc_env; /* Global exception environment. */
break;
default:
// printf("Unknown exception occured: %X\n", exception_type);
// exit(-1);
ABORT_F("Unknown exception occured: %X\n", exception_type);
break;
}
@@ -110,9 +113,19 @@ jmp_buf exc_env; /* Global exception environment. */
ppc_next_instruction_address |= 0xFFF00000;
}
exec_flags = EXEF_EXCEPTION;
// perform context synchronization for recoverable exceptions
if (exception_type != Except_Type::EXC_MACHINE_CHECK &&
exception_type != Except_Type::EXC_SYSTEM_RESET) {
do_ctx_sync();
}
mmu_change_mode();
longjmp(exc_env, 2); /* return to the main execution loop. */
if (exception_type != Except_Type::EXC_EXT_INT) {
longjmp(exc_env, 2); /* return to the main execution loop. */
}
}
+287 -287
View File
@@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
Copyright (C) 2018-21 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@@ -19,21 +19,21 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <core/timermanager.h>
#include <loguru.hpp>
#include "ppcemu.h"
#include "ppcmmu.h"
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <setjmp.h>
#include <stdexcept>
#include <stdio.h>
#include <string>
#include <loguru.hpp>
#include <unordered_map>
#include "ppcemu.h"
#include "ppcmmu.h"
#define NEW_TBR_UPDATE_ALGO
using namespace std;
using namespace dppc_interpreter;
@@ -43,10 +43,9 @@ bool power_on = 1;
SetPRS ppc_state;
bool rc_flag = 0; // Record flag
bool oe_flag = 0; // Overflow flag
bool rc_flag = 0; // Record flag
bool oe_flag = 0; // Overflow flag
bool grab_exception;
bool grab_return;
bool grab_breakpoint;
@@ -54,7 +53,8 @@ uint32_t ppc_cur_instruction; // Current instruction for the PPC
uint32_t ppc_effective_address;
uint32_t ppc_next_instruction_address; // Used for branching, setting up the NIA
BB_end_kind bb_kind; /* basic block end */
unsigned exec_flags; // execution control flags
bool int_pin = false; // interrupt request pin state: true - asserted
/* copy of local variable bb_start_la. Need for correct
calculation of CPU cycles after setjmp that clobbers
@@ -62,12 +62,17 @@ BB_end_kind bb_kind; /* basic block end */
uint32_t glob_bb_start_la;
/* variables related to virtual time */
uint64_t cycles_count; /* contains number of cycles executed so far */
uint64_t old_cycles_count; /* previous value for cycles_count */
uint64_t timebase_counter; /* internal timebase counter */
uint32_t decr; /* current value of PPC DEC register */
uint8_t old_decr_msb; /* MSB value for previous DEC value */
uint8_t tbr_factor; /* cycles_count to TBR freq ratio in 2^x units */
uint64_t g_icycles;
int icnt_factor;
/* global variables related to the timebase facility */
uint64_t tbr_wr_timestamp; // stores vCPU virtual time of the last TBR/RTC write
uint64_t tbr_wr_value; // last value written to the TBR
uint64_t tbr_freq_hz; // TBR/RTC driving frequency in Hz
uint64_t timebase_counter; // internal timebase counter
uint32_t decr; // current value of PPC DEC register
uint32_t rtc_lo; // MPC601 RTC lower, counts nanoseconds
uint32_t rtc_hi; // MPC601 RTC upper, counts seconds
#ifdef CPU_PROFILING
@@ -156,28 +161,30 @@ PPCOpcode SubOpcode31Grabber[1024] = { ppc_illegalop };
PPCOpcode SubOpcode59Grabber[32] = { ppc_illegalop };
PPCOpcode SubOpcode63Grabber[1024] = { ppc_illegalop };
#define UPDATE_TBR_DEC \
if ((delta = (cycles_count - old_cycles_count) >> tbr_factor)) { \
timebase_counter += delta; \
decr -= delta; \
if ((decr & 0x80000000) && !old_decr_msb) { \
old_decr_msb = decr >> 31; \
/* signal_decr_int(); */ \
} \
old_cycles_count += delta << tbr_factor; \
}
/** Exception helpers. */
[[noreturn]] void ppc_illegalop() {
void ppc_illegalop() {
ppc_exception_handler(Except_Type::EXC_PROGRAM, Exc_Cause::ILLEGAL_OP);
}
[[noreturn]] void ppc_fpu_off() {
void ppc_fpu_off() {
ppc_exception_handler(Except_Type::EXC_PROGRAM, Exc_Cause::FPU_OFF);
}
void ppc_assert_int() {
int_pin = true;
if (ppc_state.msr & 0x8000) {
LOG_F(5, "CPU ExtIntHandler called");
ppc_exception_handler(Except_Type::EXC_EXT_INT, 0);
} else {
LOG_F(5, "CPU IRQ ignored!");
}
}
void ppc_release_int() {
int_pin = false;
}
/** Opcode decoding functions. */
void ppc_opcode16() {
@@ -193,7 +200,7 @@ void ppc_opcode19() {
#ifdef EXHAUSTIVE_DEBUG
uint32_t regrab = (uint32_t)subop_grab;
LOG_F(INFO, "Executing Opcode 19 table subopcode entry \n", regrab);
LOG_F(INFO, "Executing Opcode 19 table subopcode entry", regrab);
#endif // EXHAUSTIVE_DEBUG
if (subop_grab == 32) {
@@ -253,7 +260,7 @@ void ppc_opcode31() {
oe_flag = ppc_cur_instruction & 0x400;
#ifdef EXHAUSTIVE_DEBUG
LOG_F(INFO, "Executing Opcode 31 table subopcode entry %n \n", (uint32_t)subop_grab);
LOG_F(INFO, "Executing Opcode 31 table subopcode entry %n", (uint32_t)subop_grab);
#endif // EXHAUSTIVE_DEBUG
SubOpcode31Grabber[subop_grab]();
@@ -263,7 +270,7 @@ void ppc_opcode59() {
uint16_t subop_grab = (ppc_cur_instruction >> 1) & 0x1F;
rc_flag = ppc_cur_instruction & 1;
#ifdef EXHAUSTIVE_DEBUG
LOG_F(INFO, "Executing Opcode 59 table subopcode entry %n \n", (uint32_t)subop_grab);
LOG_F(INFO, "Executing Opcode 59 table subopcode entry %n", (uint32_t)subop_grab);
#endif // EXHAUSTIVE_DEBUG
SubOpcode59Grabber[subop_grab]();
}
@@ -272,7 +279,7 @@ void ppc_opcode63() {
uint16_t subop_grab = (ppc_cur_instruction >> 1) & 0x3FF;
rc_flag = ppc_cur_instruction & 1;
#ifdef EXHAUSTIVE_DEBUG
LOG_F(INFO, "Executing Opcode 63 table subopcode entry %n \n", (uint32_t)subop_grab);
LOG_F(INFO, "Executing Opcode 63 table subopcode entry %n", (uint32_t)subop_grab);
#endif // EXHAUSTIVE_DEBUG
SubOpcode63Grabber[subop_grab]();
}
@@ -286,262 +293,281 @@ void ppc_main_opcode()
OpcodeGrabber[(ppc_cur_instruction >> 26) & 0x3F]();
}
uint64_t get_virt_time_ns()
{
return g_icycles << icnt_factor;
}
uint64_t process_events()
{
uint64_t slice_ns = TimerManager::get_instance()->process_timers(get_virt_time_ns());
if (slice_ns == 0) {
// execute 10.000 cycles
// if there are no pending timers
return g_icycles + 10000;
}
return g_icycles + ((slice_ns + (1 << icnt_factor)) >> icnt_factor);
}
void force_cycle_counter_reload()
{
// tell the interpreter loop to reload cycle counter
exec_flags |= EXEF_TIMER;
}
/** Execute PPC code as long as power is on. */
void ppc_exec()
// inner interpreter loop
static void ppc_exec_inner()
{
uint32_t bb_start_la, page_start, delta;
uint64_t max_cycles;
uint32_t page_start, eb_start, eb_end;
uint8_t* pc_real;
/* start new basic block */
glob_bb_start_la = bb_start_la = ppc_state.pc;
bb_kind = BB_end_kind::BB_NONE;
max_cycles = 0;
if (setjmp(exc_env)) {
/* reaching here means we got a low-level exception */
#ifdef NEW_TBR_UPDATE_ALGO
cycles_count += ((ppc_state.pc - glob_bb_start_la) >> 2) + 1;
UPDATE_TBR_DEC
#else
timebase_counter += ((ppc_state.pc - glob_bb_start_la) >> 2) + 1;
#endif
glob_bb_start_la = bb_start_la = ppc_next_instruction_address;
//pc_real = mmu_translate_imem(bb_start_la);
pc_real = mmu_translate_imem(bb_start_la);
page_start = bb_start_la & 0xFFFFF000;
ppc_state.pc = bb_start_la;
bb_kind = BB_end_kind::BB_NONE;
goto again;
}
/* initial MMU translation for the current code page. */
//pc_real = quickinstruction_translate(bb_start_la);
pc_real = mmu_translate_imem(bb_start_la);
/* set current code page limits */
page_start = bb_start_la & 0xFFFFF000;
again:
while (power_on) {
ppc_main_opcode();
if (bb_kind != BB_end_kind::BB_NONE) {
#ifdef NEW_TBR_UPDATE_ALGO
cycles_count += ((ppc_state.pc - bb_start_la) >> 2) + 1;
UPDATE_TBR_DEC
#else
timebase_counter += ((ppc_state.pc - bb_start_la) >> 2) + 1;
#endif
glob_bb_start_la = bb_start_la = ppc_next_instruction_address;
if ((ppc_next_instruction_address & 0xFFFFF000) != page_start) {
page_start = bb_start_la & 0xFFFFF000;
pc_real = mmu_translate_imem(bb_start_la);
// define boundaries of the next execution block
// max execution block length = one memory page
eb_start = ppc_state.pc;
eb_end = (eb_start + PAGE_SIZE) & PAGE_MASK;
exec_flags = 0;
page_start = eb_start & PAGE_MASK;
pc_real = mmu_translate_imem(eb_start);
// interpret execution block
while (ppc_state.pc < eb_end) {
ppc_main_opcode();
if (g_icycles++ >= max_cycles) {
max_cycles = process_events();
}
if (exec_flags) {
if (!power_on)
break;
// reload cycle counter if requested
if (exec_flags & EXEF_TIMER) {
max_cycles = process_events();
if (!(exec_flags & ~EXEF_TIMER)) {
ppc_state.pc += 4;
pc_real += 4;
ppc_set_cur_instruction(pc_real);
exec_flags = 0;
continue;
}
}
// define next execution block
eb_start = ppc_next_instruction_address;
eb_end = (eb_start + PAGE_SIZE) & PAGE_MASK;
if ((eb_start & PAGE_MASK) == page_start) {
pc_real += (int)eb_start - (int)ppc_state.pc;
ppc_set_cur_instruction(pc_real);
} else {
page_start = eb_start & PAGE_MASK;
pc_real = mmu_translate_imem(eb_start);
}
ppc_state.pc = eb_start;
exec_flags = 0;
} else {
pc_real += (int)bb_start_la - (int)ppc_state.pc;
ppc_state.pc += 4;
pc_real += 4;
ppc_set_cur_instruction(pc_real);
}
ppc_state.pc = bb_start_la;
bb_kind = BB_end_kind::BB_NONE;
} else {
ppc_state.pc += 4;
pc_real += 4;
ppc_set_cur_instruction(pc_real);
}
}
}
// outer interpreter loop
void ppc_exec()
{
if (setjmp(exc_env)) {
// process low-level exceptions
//LOG_F(9, "PPC-EXEC: low_level exception raised!");
ppc_state.pc = ppc_next_instruction_address;
}
while (power_on) {
ppc_exec_inner();
}
}
/** Execute one PPC instruction. */
void ppc_exec_single()
{
uint32_t delta;
if (setjmp(exc_env)) {
/* reaching here means we got a low-level exception */
#ifdef NEW_TBR_UPDATE_ALGO
cycles_count++;
UPDATE_TBR_DEC
#else
timebase_counter++;
#endif
// process low-level exceptions
//LOG_F(9, "PPC-EXEC: low_level exception raised!");
ppc_state.pc = ppc_next_instruction_address;
bb_kind = BB_end_kind::BB_NONE;
exec_flags = 0;
return;
}
//quickinstruction_translate(ppc_state.pc);
mmu_translate_imem(ppc_state.pc);
ppc_main_opcode();
if (bb_kind != BB_end_kind::BB_NONE) {
ppc_state.pc = ppc_next_instruction_address;
bb_kind = BB_end_kind::BB_NONE;
g_icycles++;
process_events();
if (exec_flags) {
if (exec_flags & EXEF_TIMER) {
ppc_state.pc += 4;
} else {
ppc_state.pc = ppc_next_instruction_address;
}
exec_flags = 0;
} else {
ppc_state.pc += 4;
}
#ifdef NEW_TBR_UPDATE_ALGO
cycles_count++;
UPDATE_TBR_DEC
#else
timebase_counter++;
#endif
}
/** Execute PPC code until goal_addr is reached. */
void ppc_exec_until(volatile uint32_t goal_addr)
// inner interpreter loop
static void ppc_exec_until_inner(const uint32_t goal_addr)
{
uint32_t bb_start_la, page_start, delta;
uint64_t max_cycles;
uint32_t page_start, eb_start, eb_end;
uint8_t* pc_real;
/* start new basic block */
glob_bb_start_la = bb_start_la = ppc_state.pc;
bb_kind = BB_end_kind::BB_NONE;
max_cycles = 0;
if (setjmp(exc_env)) {
/* reaching here means we got a low-level exception */
#ifdef NEW_TBR_UPDATE_ALGO
cycles_count += ((ppc_state.pc - glob_bb_start_la) >> 2) + 1;
UPDATE_TBR_DEC
#else
timebase_counter += ((ppc_state.pc - glob_bb_start_la) >> 2) + 1;
#endif
glob_bb_start_la = bb_start_la = ppc_next_instruction_address;
pc_real = mmu_translate_imem(bb_start_la);
page_start = bb_start_la & 0xFFFFF000;
ppc_state.pc = bb_start_la;
bb_kind = BB_end_kind::BB_NONE;
goto again;
}
/* initial MMU translation for the current code page. */
//pc_real = quickinstruction_translate(bb_start_la);
pc_real = mmu_translate_imem(bb_start_la);
/* set current code page limits */
page_start = bb_start_la & 0xFFFFF000;
again:
while (ppc_state.pc != goal_addr) {
ppc_main_opcode();
if (bb_kind != BB_end_kind::BB_NONE) {
#ifdef NEW_TBR_UPDATE_ALGO
cycles_count += ((ppc_state.pc - bb_start_la) >> 2) + 1;
UPDATE_TBR_DEC
#else
timebase_counter += ((ppc_state.pc - bb_start_la) >> 2) + 1;
#endif
glob_bb_start_la = bb_start_la = ppc_next_instruction_address;
if ((ppc_next_instruction_address & 0xFFFFF000) != page_start) {
page_start = bb_start_la & 0xFFFFF000;
pc_real = mmu_translate_imem(bb_start_la);
// define boundaries of the next execution block
// max execution block length = one memory page
eb_start = ppc_state.pc;
eb_end = (eb_start + PAGE_SIZE) & PAGE_MASK;
exec_flags = 0;
page_start = eb_start & PAGE_MASK;
pc_real = mmu_translate_imem(eb_start);
// interpret execution block
while ((ppc_state.pc != goal_addr) && (ppc_state.pc < eb_end)) {
ppc_main_opcode();
if (g_icycles++ >= max_cycles) {
max_cycles = process_events();
}
if (exec_flags) {
// reload cycle counter if requested
if (exec_flags & EXEF_TIMER) {
max_cycles = process_events();
if (!(exec_flags & ~EXEF_TIMER)) {
ppc_state.pc += 4;
pc_real += 4;
ppc_set_cur_instruction(pc_real);
exec_flags = 0;
continue;
}
}
// define next execution block
eb_start = ppc_next_instruction_address;
eb_end = (eb_start + PAGE_SIZE) & PAGE_MASK;
if ((eb_start & PAGE_MASK) == page_start) {
pc_real += (int)eb_start - (int)ppc_state.pc;
ppc_set_cur_instruction(pc_real);
} else {
page_start = eb_start & PAGE_MASK;
pc_real = mmu_translate_imem(eb_start);
}
ppc_state.pc = eb_start;
exec_flags = 0;
} else {
pc_real += (int)bb_start_la - (int)ppc_state.pc;
ppc_state.pc += 4;
pc_real += 4;
ppc_set_cur_instruction(pc_real);
}
ppc_state.pc = bb_start_la;
bb_kind = BB_end_kind::BB_NONE;
} else {
ppc_state.pc += 4;
pc_real += 4;
ppc_set_cur_instruction(pc_real);
}
}
}
// outer interpreter loop
void ppc_exec_until(volatile uint32_t goal_addr)
{
if (setjmp(exc_env)) {
// process low-level exceptions
//LOG_F(9, "PPC-EXEC: low_level exception raised!");
ppc_state.pc = ppc_next_instruction_address;
}
while (ppc_state.pc != goal_addr) {
ppc_exec_until_inner(goal_addr);
}
}
/** Execute PPC code until control is reached the specified region. */
void ppc_exec_dbg(volatile uint32_t start_addr, volatile uint32_t size)
// inner interpreter loop
static void ppc_exec_dbg_inner(const uint32_t start_addr, const uint32_t size)
{
uint32_t bb_start_la, page_start, delta;
uint64_t max_cycles;
uint32_t page_start, eb_start, eb_end;
uint8_t* pc_real;
/* start new basic block */
glob_bb_start_la = bb_start_la = ppc_state.pc;
bb_kind = BB_end_kind::BB_NONE;
max_cycles = 0;
if (setjmp(exc_env)) {
/* reaching here means we got a low-level exception */
#ifdef NEW_TBR_UPDATE_ALGO
cycles_count += ((ppc_state.pc - glob_bb_start_la) >> 2) + 1;
UPDATE_TBR_DEC
#else
timebase_counter += ((ppc_state.pc - glob_bb_start_la) >> 2) + 1;
#endif
glob_bb_start_la = bb_start_la = ppc_next_instruction_address;
//pc_real = quickinstruction_translate(bb_start_la);
pc_real = mmu_translate_imem(bb_start_la);
page_start = bb_start_la & 0xFFFFF000;
ppc_state.pc = bb_start_la;
bb_kind = BB_end_kind::BB_NONE;
//printf("DBG Exec: got exception, continue at %X\n", ppc_state.pc);
goto again;
}
/* initial MMU translation for the current code page. */
pc_real = mmu_translate_imem(bb_start_la);
/* set current code page limits */
page_start = bb_start_la & 0xFFFFF000;
again:
while (ppc_state.pc < start_addr || ppc_state.pc >= start_addr + size) {
ppc_main_opcode();
if (bb_kind != BB_end_kind::BB_NONE) {
#ifdef NEW_TBR_UPDATE_ALGO
cycles_count += ((ppc_state.pc - bb_start_la) >> 2) + 1;
UPDATE_TBR_DEC
#else
timebase_counter += ((ppc_state.pc - bb_start_la) >> 2) + 1;
#endif
glob_bb_start_la = bb_start_la = ppc_next_instruction_address;
if ((ppc_next_instruction_address & 0xFFFFF000) != page_start) {
page_start = bb_start_la & 0xFFFFF000;
//pc_real = quickinstruction_translate(bb_start_la);
pc_real = mmu_translate_imem(bb_start_la);
// define boundaries of the next execution block
// max execution block length = one memory page
eb_start = ppc_state.pc;
eb_end = (eb_start + PAGE_SIZE) & PAGE_MASK;
exec_flags = 0;
page_start = eb_start & PAGE_MASK;
pc_real = mmu_translate_imem(eb_start);
// interpret execution block
while ((ppc_state.pc < start_addr || ppc_state.pc >= start_addr + size)
&& (ppc_state.pc < eb_end)) {
ppc_main_opcode();
if (g_icycles++ >= max_cycles) {
max_cycles = process_events();
}
if (exec_flags) {
// reload cycle counter if requested
if (exec_flags & EXEF_TIMER) {
max_cycles = process_events();
if (!(exec_flags & ~EXEF_TIMER)) {
ppc_state.pc += 4;
pc_real += 4;
ppc_set_cur_instruction(pc_real);
exec_flags = 0;
continue;
}
}
// define next execution block
eb_start = ppc_next_instruction_address;
eb_end = (eb_start + PAGE_SIZE) & PAGE_MASK;
if ((eb_start & PAGE_MASK) == page_start) {
pc_real += (int)eb_start - (int)ppc_state.pc;
ppc_set_cur_instruction(pc_real);
} else {
page_start = eb_start & PAGE_MASK;
pc_real = mmu_translate_imem(eb_start);
}
ppc_state.pc = eb_start;
exec_flags = 0;
} else {
pc_real += (int)bb_start_la - (int)ppc_state.pc;
ppc_state.pc += 4;
pc_real += 4;
ppc_set_cur_instruction(pc_real);
}
ppc_state.pc = bb_start_la;
bb_kind = BB_end_kind::BB_NONE;
//printf("DBG Exec: new basic block at %X, start_addr=%X\n", ppc_state.pc, start_addr);
} else {
ppc_state.pc += 4;
pc_real += 4;
ppc_set_cur_instruction(pc_real);
}
}
}
uint64_t instr_count, old_instr_count;
void test_timebase_update()
// outer interpreter loop
void ppc_exec_dbg(volatile uint32_t start_addr, volatile uint32_t size)
{
uint32_t delta, factor;
uint8_t old_decr_msb;
uint8_t intervals[10] = {4, 7, 10, 2, 16, 6, 3, 20, 12, 8};
timebase_counter = 0;
decr = 0x00000003;
old_decr_msb = decr >> 31;
old_instr_count = 0xFFFFFFFFFFFFFF80UL;//0xFFFFFFFFFFFFFFD0UL;
instr_count = 0xFFFFFFFFFFFFFF80UL;//0xFFFFFFFFFFFFFFD0UL;
factor = 4;
for (int i = 0; i < 10; i++) {
cycles_count += intervals[i];
if ((delta = (cycles_count - old_cycles_count) >> factor)) {
timebase_counter += delta;
decr -= delta;
if ((decr & 0x80000000) && !old_decr_msb) {
old_decr_msb = decr >> 31;
LOG_F(ERROR, "DEC exception signaled!\n");
}
old_instr_count += delta << factor;
LOG_F(INFO, "Iteration %d, TBR=0x%llX, DEC=0x%X", i, timebase_counter, decr);
LOG_F(INFO, " icount=0x%llX, old_icount=0x%llX\n", cycles_count, old_cycles_count);
}
if (setjmp(exc_env)) {
// process low-level exceptions
//LOG_F(9, "PPC-EXEC: low_level exception raised!");
ppc_state.pc = ppc_next_instruction_address;
}
while (ppc_state.pc < start_addr || ppc_state.pc >= start_addr + size) {
ppc_exec_dbg_inner(start_addr, size);
}
}
void initialize_ppc_opcode_tables() {
@@ -684,26 +710,11 @@ void initialize_ppc_opcode_tables() {
SubOpcode59Grabber[21] = ppc_fadds;
SubOpcode59Grabber[22] = ppc_fsqrts;
SubOpcode59Grabber[24] = ppc_fres;
for (int i = 25; i < 1024; i += 32) {
SubOpcode59Grabber[i] = ppc_fmuls;
}
for (int i = 28; i < 1024; i += 32) {
SubOpcode59Grabber[i] = ppc_fmsubs;
}
for (int i = 29; i < 1024; i += 32) {
SubOpcode59Grabber[i] = ppc_fmadds;
}
for (int i = 30; i < 1024; i += 32) {
SubOpcode59Grabber[i] = ppc_fnmsubs;
}
for (int i = 31; i < 1024; i += 32) {
SubOpcode59Grabber[i] = ppc_fnmadds;
}
SubOpcode59Grabber[25] = ppc_fmuls;
SubOpcode59Grabber[28] = ppc_fmsubs;
SubOpcode59Grabber[29] = ppc_fmadds;
SubOpcode59Grabber[30] = ppc_fnmsubs;
SubOpcode59Grabber[31] = ppc_fnmadds;
SubOpcode63Grabber[0] = ppc_fcmpu;
SubOpcode63Grabber[12] = ppc_frsp;
@@ -726,50 +737,39 @@ void initialize_ppc_opcode_tables() {
SubOpcode63Grabber[583] = ppc_mffs;
SubOpcode63Grabber[711] = ppc_mtfsf;
for (int i = 23; i < 1024; i += 32) {
SubOpcode63Grabber[i] = ppc_fsel;
}
for (int i = 25; i < 1024; i += 32) {
SubOpcode63Grabber[i] = ppc_fmul;
}
for (int i = 28; i < 1024; i += 32) {
SubOpcode63Grabber[i] = ppc_fmsub;
}
for (int i = 29; i < 1024; i += 32) {
SubOpcode63Grabber[i] = ppc_fmadd;
}
for (int i = 30; i < 1024; i += 32) {
SubOpcode63Grabber[i] = ppc_fnmsub;
}
for (int i = 31; i < 1024; i += 32) {
SubOpcode63Grabber[i] = ppc_fnmadd;
for (int i = 0; i < 1024; i += 32) {
SubOpcode63Grabber[i + 23] = ppc_fsel;
SubOpcode63Grabber[i + 25] = ppc_fmul;
SubOpcode63Grabber[i + 28] = ppc_fmsub;
SubOpcode63Grabber[i + 29] = ppc_fmadd;
SubOpcode63Grabber[i + 30] = ppc_fnmsub;
SubOpcode63Grabber[i + 31] = ppc_fnmadd;
}
}
void ppc_cpu_init(MemCtrlBase* mem_ctrl, uint32_t cpu_version) {
void ppc_cpu_init(MemCtrlBase* mem_ctrl, uint32_t cpu_version, uint64_t tb_freq)
{
int i;
mem_ctrl_instance = mem_ctrl;
//test_timebase_update();
initialize_ppc_opcode_tables();
/* initialize timer variables */
#ifdef NEW_TBR_UPDATE_ALGO
cycles_count = 0;
old_cycles_count = 0;
tbr_factor = 4;
#endif
// initialize emulator timers
TimerManager::get_instance()->set_time_now_cb(&get_virt_time_ns);
TimerManager::get_instance()->set_notify_changes_cb(&force_cycle_counter_reload);
// initialize time base facility
g_icycles = 0;
icnt_factor = 4;
tbr_wr_timestamp = 0;
tbr_wr_value = 0;
tbr_freq_hz = tb_freq;
exec_flags = 0;
timebase_counter = 0;
decr = 0;
old_decr_msb = decr >> 31;
/* zero all GPRs as prescribed for MPC601 */
/* For later PPC CPUs, GPR content is undefined */
@@ -919,10 +919,10 @@ bail_out:
throw std::invalid_argument(string("Unknown register ") + reg_name);
}
uint64_t get_reg(string& reg_name) {
uint64_t get_reg(string reg_name) {
return reg_op(reg_name, 0, false);
}
void set_reg(string& reg_name, uint64_t val) {
void set_reg(string reg_name, uint64_t val) {
reg_op(reg_name, val, true);
}
+132 -117
View File
@@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
Copyright (C) 2018-21 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@@ -19,7 +19,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// The opcodes for the processor - ppcopcodes.cpp
// The floating point opcodes for the processor - ppcfpopcodes.cpp
#include "ppcemu.h"
#include "ppcmmu.h"
@@ -153,22 +153,22 @@ void ppc_fp_changecrf1() {
int64_t round_to_nearest(double f) {
if (f >= 0.0) {
return static_cast<int32_t>(static_cast<int64_t> (ceil(f)));
return static_cast<int32_t>(static_cast<int64_t> (std::ceil(f)));
} else {
return static_cast<int32_t>(static_cast<int64_t> (floor(f)));
return static_cast<int32_t>(static_cast<int64_t> (std::floor(f)));
}
}
int64_t round_to_zero(double f) {
return static_cast<int32_t>(trunc(f));
return static_cast<int32_t>(std::trunc(f));
}
int64_t round_to_pos_inf(double f) {
return static_cast<int32_t>(ceil(f));
return static_cast<int32_t>(std::ceil(f));
}
int64_t round_to_neg_inf(double f) {
return static_cast<int32_t>(floor(f));
return static_cast<int32_t>(std::floor(f));
}
void update_fex() {
@@ -186,7 +186,7 @@ void ppc_confirm_inf_nan(int chosen_reg_1, int chosen_reg_2, int chosen_reg_3, b
switch (fpop) {
case FPOP::DIV:
if (isnan(input_a) && isnan(input_b)) {
if (std::isinf(input_a) && std::isinf(input_b)) {
ppc_state.fpscr |= (FPSCR::FX | FPSCR::VXIDI);
} else if ((input_a == FP_ZERO) && (input_b == FP_ZERO)) {
ppc_state.fpscr |= (FPSCR::FX | FPSCR::VXZDZ);
@@ -194,13 +194,13 @@ void ppc_confirm_inf_nan(int chosen_reg_1, int chosen_reg_2, int chosen_reg_3, b
update_fex();
break;
case FPOP::SUB:
if (isnan(input_a) && isnan(input_b)) {
if (std::isnan(input_a) && std::isnan(input_b)) {
ppc_state.fpscr |= (FPSCR::FX | FPSCR::VXISI);
}
update_fex();
break;
case FPOP::ADD:
if (isnan(input_a) && isnan(input_b)) {
if (std::isnan(input_a) && std::isnan(input_b)) {
ppc_state.fpscr |= (FPSCR::FX | FPSCR::VXISI);
}
update_fex();
@@ -216,8 +216,8 @@ void ppc_confirm_inf_nan(int chosen_reg_1, int chosen_reg_2, int chosen_reg_3, b
break;
case FPOP::FMSUB:
case FPOP::FNMSUB:
if (isnan(input_a) || isnan(input_b) || isnan(input_c)) {
ppc_state.fpscr |= FPSCR::VXSNAN;
if (std::isnan(input_a) || std::isnan(input_b) || std::isnan(input_c)) {
ppc_state.fpscr |= (FPSCR::FX | FPSCR::VXSNAN);
if (((input_a == FP_ZERO) && (input_c == FP_INFINITE)) ||
((input_c == FP_ZERO) && (input_a == FP_INFINITE))) {
ppc_state.fpscr |= FPSCR::VXIMZ;
@@ -228,7 +228,7 @@ void ppc_confirm_inf_nan(int chosen_reg_1, int chosen_reg_2, int chosen_reg_3, b
break;
case FPOP::FMADD:
case FPOP::FNMADD:
if (isnan(input_a) || isnan(input_b) || isnan(input_c)) {
if (std::isnan(input_a) || std::isnan(input_b) || std::isnan(input_c)) {
ppc_state.fpscr |= (FPSCR::VXSNAN | FPSCR::FPCC_FUNAN);
}
update_fex();
@@ -236,60 +236,70 @@ void ppc_confirm_inf_nan(int chosen_reg_1, int chosen_reg_2, int chosen_reg_3, b
}
}
void fpresult_update(double set_result, bool confirm_arc) {
bool confirm_ov = (bool)std::fetestexcept(FE_OVERFLOW);
static void fpresult_update(double set_result, bool confirm_arc) {
if (ppc_state.fpscr & 0x3)
ppc_state.cr |= 0x2;
if (set_result > 0.0) {
ppc_state.fpscr |= FPSCR::FPCC_POS;
ppc_state.fpscr |= FPCC_POS;
} else if (set_result < 0.0) {
ppc_state.fpscr |= FPSCR::FPCC_NEG;
} else if (set_result == 0.0) {
ppc_state.fpscr |= FPSCR::FPCC_ZERO;
ppc_state.fpscr |= FPCC_NEG;
} else {
ppc_state.fpscr |= FPSCR::FPCC_FPRCD;
ppc_state.fpscr |= FPCC_ZERO;
}
if (confirm_ov) {
ppc_state.fpscr |= (FPSCR::FX | (FPSCR::FPRF & FPSCR::FPCC_FUNAN));
}
if (confirm_arc) {
ppc_state.fpscr |= (FPSCR::FX | (FPSCR::FPRF & FPSCR::FPCC_FUNAN));
//ppc_state.fpscr &= 0xFFFF0FFF;
if (std::isnan(set_result) || std::isinf(set_result)) {
ppc_state.fpscr |= FPCC_FPRCD;
}
}
void ppc_changecrf1() {
ppc_state.cr &= ~((uint32_t)CR_select::CR1_field);
ppc_state.cr |= (ppc_state.fpscr & (uint32_t)CR_select::CR0_field) >> 4;
void ppc_changecrf1(double set_result) {
cmp_c = 0;
/*
if (isnan(set_result)) {
cmp_c |= (1 << CRx_bit::CR_SO);
}
if (set_result > 0.0) {
cmp_c |= (1 << CRx_bit::CR_GT);
}
if (set_result < 0.0) {
cmp_c |= (1 << CRx_bit::CR_LT);
}
if (set_result == 0.0) {
cmp_c |= (1 << CRx_bit::CR_EQ);
}*/
ppc_state.cr = ((ppc_state.cr & ~(CR_select::CR1_field)) | ((cmp_c) >> crf_d));
}
// Floating Point Arithmetic
void dppc_interpreter::ppc_fadd() {
ppc_grab_regsfpdab();
ppc_dblresult64_d = val_reg_a + val_reg_b;
ppc_dblresult64_d = double(val_reg_a + val_reg_b);
if (!isnan(ppc_dblresult64_d)) {
if (!std::isnan(ppc_dblresult64_d) || !std::isinf(ppc_dblresult64_d)) {
ppc_store_dfpresult_flt(reg_d);
fpresult_update(ppc_dblresult64_d, rc_flag);
} else {
}
else {
ppc_confirm_inf_nan<double, ADD>(reg_a, reg_b, 0, rc_flag);
}
if (rc_flag)
ppc_changecrf1();
ppc_changecrf1(ppc_dblresult64_d);
}
void dppc_interpreter::ppc_fsub() {
ppc_grab_regsfpdab();
ppc_dblresult64_d = val_reg_a - val_reg_b;
ppc_dblresult64_d = double(val_reg_a - val_reg_b);
if (!isnan(ppc_dblresult64_d)) {
if (!std::isnan(ppc_dblresult64_d) || !std::isinf(ppc_dblresult64_d)) {
ppc_store_dfpresult_flt(reg_d);
fpresult_update(ppc_dblresult64_d, rc_flag);
} else {
@@ -297,7 +307,7 @@ void dppc_interpreter::ppc_fsub() {
}
if (rc_flag)
ppc_changecrf1();
ppc_changecrf1(ppc_dblresult64_d);
}
void dppc_interpreter::ppc_fdiv() {
@@ -305,7 +315,7 @@ void dppc_interpreter::ppc_fdiv() {
ppc_dblresult64_d = val_reg_a / val_reg_b;
if (!isnan(ppc_dblresult64_d)) {
if (!std::isnan(ppc_dblresult64_d) || !std::isinf(ppc_dblresult64_d)) {
ppc_store_dfpresult_flt(reg_d);
fpresult_update(ppc_dblresult64_d, rc_flag);
} else {
@@ -313,7 +323,7 @@ void dppc_interpreter::ppc_fdiv() {
}
if (rc_flag)
ppc_changecrf1();
ppc_changecrf1(ppc_dblresult64_d);
}
void dppc_interpreter::ppc_fmul() {
@@ -321,7 +331,7 @@ void dppc_interpreter::ppc_fmul() {
ppc_dblresult64_d = val_reg_a * val_reg_c;
if (!isnan(ppc_dblresult64_d)) {
if (!std::isnan(ppc_dblresult64_d) || !std::isinf(ppc_dblresult64_d)) {
ppc_store_dfpresult_flt(reg_d);
fpresult_update(ppc_dblresult64_d, rc_flag);
} else {
@@ -329,7 +339,7 @@ void dppc_interpreter::ppc_fmul() {
}
if (rc_flag)
ppc_changecrf1();
ppc_changecrf1(ppc_dblresult64_d);
}
void dppc_interpreter::ppc_fmadd() {
@@ -337,7 +347,7 @@ void dppc_interpreter::ppc_fmadd() {
ppc_dblresult64_d = std::fma(val_reg_a, val_reg_c, val_reg_b);
if (!isnan(ppc_dblresult64_d)) {
if (!std::isnan(ppc_dblresult64_d) || !std::isinf(ppc_dblresult64_d)) {
ppc_store_dfpresult_flt(reg_d);
fpresult_update(ppc_dblresult64_d, rc_flag);
} else {
@@ -345,7 +355,7 @@ void dppc_interpreter::ppc_fmadd() {
}
if (rc_flag)
ppc_changecrf1();
ppc_changecrf1(ppc_dblresult64_d);
}
void dppc_interpreter::ppc_fmsub() {
@@ -354,7 +364,7 @@ void dppc_interpreter::ppc_fmsub() {
ppc_dblresult64_d = (val_reg_a * val_reg_c);
ppc_dblresult64_d -= val_reg_b;
if (!isnan(ppc_dblresult64_d)) {
if (!std::isnan(ppc_dblresult64_d) || !std::isinf(ppc_dblresult64_d)) {
ppc_store_dfpresult_flt(reg_d);
fpresult_update(ppc_dblresult64_d, rc_flag);
} else {
@@ -362,7 +372,7 @@ void dppc_interpreter::ppc_fmsub() {
}
if (rc_flag)
ppc_changecrf1();
ppc_changecrf1(ppc_dblresult64_d);
}
void dppc_interpreter::ppc_fnmadd() {
@@ -370,8 +380,9 @@ void dppc_interpreter::ppc_fnmadd() {
ppc_dblresult64_d = (val_reg_a * val_reg_c);
ppc_dblresult64_d += val_reg_b;
ppc_dblresult64_d = -(ppc_dblresult64_d);
if (!isnan(ppc_dblresult64_d)) {
if (!std::isnan(ppc_dblresult64_d) || !std::isinf(ppc_dblresult64_d)) {
ppc_store_dfpresult_flt(reg_d);
fpresult_update(ppc_dblresult64_d, rc_flag);
}
@@ -380,7 +391,7 @@ void dppc_interpreter::ppc_fnmadd() {
}
if (rc_flag)
ppc_changecrf1();
ppc_changecrf1(ppc_dblresult64_d);
}
void dppc_interpreter::ppc_fnmsub() {
@@ -388,9 +399,9 @@ void dppc_interpreter::ppc_fnmsub() {
ppc_dblresult64_d = (val_reg_a * val_reg_c);
ppc_dblresult64_d -= val_reg_b;
ppc_dblresult64_d = -ppc_dblresult64_d;
ppc_dblresult64_d = -(ppc_dblresult64_d);
if (!isnan(ppc_dblresult64_d)) {
if (!std::isnan(ppc_dblresult64_d) || !std::isinf(ppc_dblresult64_d)) {
ppc_store_dfpresult_flt(reg_d);
fpresult_update(ppc_dblresult64_d, rc_flag);
} else {
@@ -398,33 +409,32 @@ void dppc_interpreter::ppc_fnmsub() {
}
if (rc_flag)
ppc_changecrf1();
ppc_changecrf1(ppc_dblresult64_d);
}
void dppc_interpreter::ppc_fadds() {
ppc_grab_regsfpdab();
float intermediate = (float)val_reg_a + (float)val_reg_b;
ppc_dblresult64_d = static_cast<double>(intermediate);
ppc_dblresult64_d = (float)(val_reg_a + val_reg_b);
if (!isnan(ppc_dblresult64_d)) {
if (!std::isnan(ppc_dblresult64_d)) {
ppc_store_sfpresult_flt(reg_d);
fpresult_update(ppc_dblresult64_d, rc_flag);
} else {
}
else {
ppc_confirm_inf_nan<float, ADD>(reg_a, reg_b, 0);
}
if (rc_flag)
ppc_changecrf1();
ppc_changecrf1(ppc_dblresult64_d);
}
void dppc_interpreter::ppc_fsubs() {
ppc_grab_regsfpdab();
float intermediate = (float)val_reg_a - (float)val_reg_b;
ppc_dblresult64_d = static_cast<double>(intermediate);
ppc_dblresult64_d = (float)(val_reg_a - val_reg_b);
if (!isnan(ppc_dblresult64_d)) {
if (!std::isnan(ppc_dblresult64_d)) {
ppc_store_sfpresult_flt(reg_d);
fpresult_update(ppc_dblresult64_d, rc_flag);
} else {
@@ -432,16 +442,15 @@ void dppc_interpreter::ppc_fsubs() {
}
if (rc_flag)
ppc_changecrf1();
ppc_changecrf1(ppc_dblresult64_d);
}
void dppc_interpreter::ppc_fdivs() {
ppc_grab_regsfpdab();
float intermediate = (float)val_reg_a / (float)val_reg_b;
ppc_dblresult64_d = static_cast<double>(intermediate);
ppc_dblresult64_d = (float)(val_reg_a / val_reg_b);
if (!isnan(ppc_dblresult64_d)) {
if (!std::isnan(ppc_dblresult64_d)) {
ppc_store_sfpresult_flt(reg_d);
fpresult_update(ppc_dblresult64_d, rc_flag);
} else {
@@ -449,16 +458,15 @@ void dppc_interpreter::ppc_fdivs() {
}
if (rc_flag)
ppc_changecrf1();
ppc_changecrf1(ppc_dblresult64_d);
}
void dppc_interpreter::ppc_fmuls() {
ppc_grab_regsfpdac();
float intermediate = (float)val_reg_a * (float)val_reg_c;
ppc_dblresult64_d = static_cast<double>(intermediate);
ppc_dblresult64_d = (float)(val_reg_a * val_reg_c);
if (!isnan(ppc_dblresult64_d)) {
if (!std::isnan(ppc_dblresult64_d)) {
ppc_store_sfpresult_flt(reg_d);
fpresult_update(ppc_dblresult64_d, rc_flag);
} else {
@@ -466,7 +474,7 @@ void dppc_interpreter::ppc_fmuls() {
}
if (rc_flag)
ppc_changecrf1();
ppc_changecrf1(ppc_dblresult64_d);
}
void dppc_interpreter::ppc_fmadds() {
@@ -475,7 +483,7 @@ void dppc_interpreter::ppc_fmadds() {
ppc_dblresult64_d = static_cast<double>(
std::fma((float)val_reg_a, (float)val_reg_c, (float)val_reg_b));
if (!isnan(ppc_dblresult64_d)) {
if (!std::isnan(ppc_dblresult64_d)) {
ppc_store_sfpresult_flt(reg_d);
fpresult_update(ppc_dblresult64_d, rc_flag);
} else {
@@ -483,17 +491,17 @@ void dppc_interpreter::ppc_fmadds() {
}
if (rc_flag)
ppc_changecrf1();
ppc_changecrf1(ppc_dblresult64_d);
}
void dppc_interpreter::ppc_fmsubs() {
ppc_grab_regsfpdabc();
float intermediate = (float)val_reg_a * (float)val_reg_c;
float intermediate = float(val_reg_a * val_reg_c);
intermediate -= (float)val_reg_b;
ppc_dblresult64_d = static_cast<double>(intermediate);
if (!isnan(ppc_dblresult64_d)) {
if (!std::isnan(ppc_dblresult64_d)) {
ppc_store_sfpresult_flt(reg_d);
fpresult_update(ppc_dblresult64_d, rc_flag);
} else {
@@ -501,7 +509,7 @@ void dppc_interpreter::ppc_fmsubs() {
}
if (rc_flag)
ppc_changecrf1();
ppc_changecrf1(ppc_dblresult64_d);
}
void dppc_interpreter::ppc_fnmadds() {
@@ -512,7 +520,7 @@ void dppc_interpreter::ppc_fnmadds() {
intermediate = -intermediate;
ppc_dblresult64_d = static_cast<double>(intermediate);
if (!isnan(ppc_dblresult64_d)) {
if (!std::isnan(ppc_dblresult64_d)) {
ppc_store_sfpresult_flt(reg_d);
fpresult_update(ppc_dblresult64_d, rc_flag);
} else {
@@ -520,7 +528,7 @@ void dppc_interpreter::ppc_fnmadds() {
}
if (rc_flag)
ppc_changecrf1();
ppc_changecrf1(ppc_dblresult64_d);
}
void dppc_interpreter::ppc_fnmsubs() {
@@ -532,7 +540,7 @@ void dppc_interpreter::ppc_fnmsubs() {
ppc_dblresult64_d = static_cast<double>(intermediate);
if (!isnan(ppc_dblresult64_d)) {
if (!std::isnan(ppc_dblresult64_d)) {
ppc_store_sfpresult_flt(reg_d);
fpresult_update(ppc_dblresult64_d, rc_flag);
} else {
@@ -540,7 +548,7 @@ void dppc_interpreter::ppc_fnmsubs() {
}
if (rc_flag)
ppc_changecrf1();
ppc_changecrf1(ppc_dblresult64_d);
}
void dppc_interpreter::ppc_fabs() {
@@ -551,7 +559,7 @@ void dppc_interpreter::ppc_fabs() {
ppc_store_dfpresult_flt(reg_d);
if (rc_flag)
ppc_changecrf1();
ppc_changecrf1(ppc_dblresult64_d);
}
void dppc_interpreter::ppc_fnabs() {
@@ -563,7 +571,7 @@ void dppc_interpreter::ppc_fnabs() {
ppc_store_dfpresult_flt(reg_d);
if (rc_flag)
ppc_changecrf1();
ppc_changecrf1(ppc_dblresult64_d);
}
void dppc_interpreter::ppc_fneg() {
@@ -574,7 +582,7 @@ void dppc_interpreter::ppc_fneg() {
ppc_store_dfpresult_flt(reg_d);
if (rc_flag)
ppc_changecrf1();
ppc_changecrf1(ppc_dblresult64_d);
}
void dppc_interpreter::ppc_fsel() {
@@ -585,7 +593,7 @@ void dppc_interpreter::ppc_fsel() {
ppc_store_dfpresult_flt(reg_d);
if (rc_flag)
ppc_changecrf1();
ppc_changecrf1(ppc_dblresult64_d);
}
void dppc_interpreter::ppc_fsqrt() {
@@ -594,7 +602,7 @@ void dppc_interpreter::ppc_fsqrt() {
ppc_store_dfpresult_flt(reg_d);
if (rc_flag)
ppc_changecrf1();
ppc_changecrf1(ppc_dblresult64_d);
}
void dppc_interpreter::ppc_fsqrts() {
@@ -607,7 +615,7 @@ void dppc_interpreter::ppc_fsqrts() {
ppc_store_sfpresult_flt(reg_d);
if (rc_flag)
ppc_changecrf1();
ppc_changecrf1(ppc_dblresult64_d);
}
void dppc_interpreter::ppc_frsqrte() {
@@ -621,7 +629,7 @@ void dppc_interpreter::ppc_frsqrte() {
ppc_store_dfpresult_flt(reg_d);
if (rc_flag)
ppc_changecrf1();
ppc_changecrf1(ppc_dblresult64_d);
}
void dppc_interpreter::ppc_frsp() {
@@ -630,7 +638,7 @@ void dppc_interpreter::ppc_frsp() {
ppc_store_dfpresult_flt(reg_d);
if (rc_flag)
ppc_changecrf1();
ppc_changecrf1(ppc_dblresult64_d);
}
void dppc_interpreter::ppc_fres() {
@@ -653,7 +661,7 @@ void dppc_interpreter::ppc_fres() {
}
if (rc_flag)
ppc_changecrf1();
ppc_changecrf1(ppc_dblresult64_d);
}
void dppc_interpreter::ppc_fctiw() {
@@ -693,7 +701,7 @@ void dppc_interpreter::ppc_fctiw() {
}
if (rc_flag)
ppc_changecrf1();
ppc_changecrf1(ppc_dblresult64_d);
}
void dppc_interpreter::ppc_fctiwz() {
@@ -719,7 +727,7 @@ void dppc_interpreter::ppc_fctiwz() {
}
if (rc_flag)
ppc_changecrf1();
ppc_changecrf1(ppc_dblresult64_d);
}
// Floating Point Store and Load
@@ -981,26 +989,30 @@ void dppc_interpreter::ppc_fcmpo() {
ppc_grab_regsfpsab();
ppc_state.fpscr &= 0xFFFF0FFF;
cmp_c = 0;
crf_d = 4;
if (std::isnan(db_test_a) || std::isnan(db_test_b)) {
cmp_c |= (1 << (uint32_t)CRx_bit::CR_SO);
} else if (db_test_a < db_test_b) {
cmp_c |= (1 << (uint32_t)CRx_bit::CR_LT);
} else if (db_test_a > db_test_b) {
cmp_c |= (1 << (uint32_t)CRx_bit::CR_GT);
} else {
cmp_c |= (1 << (uint32_t)CRx_bit::CR_EQ);
cmp_c |= (1 << CRx_bit::CR_SO);
}
fpresult_update(db_test_a, true);
if (db_test_a < db_test_b) {
cmp_c |= (1 << CRx_bit::CR_LT);
}
else if (db_test_a > db_test_b) {
cmp_c |= (1 << CRx_bit::CR_GT);
}
else {
cmp_c |= (1 << CRx_bit::CR_EQ);
}
ppc_state.fpscr = (ppc_state.fpscr & ~(FPSCR::FPRF)) | (cmp_c << 12);
ppc_state.cr =
((ppc_state.cr & ~((uint32_t)CR_select::CR0_field >> crf_d)) | ((cmp_c + xercon) >> crf_d));
ppc_state.fpscr &= ~(FPSCR::FPRF);
ppc_state.fpscr |= (cmp_c << 12);
ppc_state.cr = ((ppc_state.cr & ~(0xF0000000 >> crf_d)) | ((cmp_c) >> crf_d));
if (std::isnan(db_test_a) || std::isnan(db_test_b)) {
ppc_state.fpscr |= FPSCR::VXSNAN;
if (ppc_state.fpscr & 0x80) {
ppc_state.fpscr |= FPSCR::FX | FPSCR::VXSNAN;
if ((ppc_state.fpscr & FPSCR::VE) == 0) {
ppc_state.fpscr |= FPSCR::VXVC;
}
} else if ((db_test_a == qnan) || (db_test_b == qnan)) {
@@ -1011,25 +1023,28 @@ void dppc_interpreter::ppc_fcmpo() {
void dppc_interpreter::ppc_fcmpu() {
ppc_grab_regsfpsab();
ppc_state.fpscr &= 0xFFFF0FFF;
cmp_c = 0;
crf_d = 4;
if (std::isnan(db_test_a) || std::isnan(db_test_b)) {
cmp_c |= (1 << (uint32_t)CRx_bit::CR_SO);
} else if (db_test_a < db_test_b) {
cmp_c |= (1 << (uint32_t)CRx_bit::CR_LT);
} else if (db_test_a > db_test_b) {
cmp_c |= (1 << (uint32_t)CRx_bit::CR_GT);
} else {
cmp_c |= (1 << (uint32_t)CRx_bit::CR_EQ);
cmp_c |= (1 << CRx_bit::CR_SO);
}
fpresult_update(db_test_a, true);
ppc_state.fpscr = (ppc_state.fpscr & ~(FPSCR::FPRF)) | (cmp_c << 12);
ppc_state.cr =
((ppc_state.cr & ~((uint32_t)CR_select::CR0_field >> crf_d)) | ((cmp_c + xercon) >> crf_d));
if (std::isnan(db_test_a) || std::isnan(db_test_b)) {
ppc_state.fpscr |= FPSCR::VXSNAN;
if (db_test_a < db_test_b) {
cmp_c |= (1 << CRx_bit::CR_LT);
}
else if (db_test_a > db_test_b) {
cmp_c |= (1 << CRx_bit::CR_GT);
}
else {
cmp_c |= (1 << CRx_bit::CR_EQ);
}
ppc_state.fpscr &= ~(FPSCR::FPRF);
ppc_state.fpscr |= (cmp_c << 12);
ppc_state.cr = ((ppc_state.cr & ~(0xF0000000 >> crf_d)) | ((cmp_c) >> crf_d));
//if (std::isnan(db_test_a) || std::isnan(db_test_b)) {
// ppc_state.fpscr |= FPSCR::VX | FPSCR::VXSNAN;
//}
}
+321 -243
View File
@@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
Copyright (C) 2018-21 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@@ -21,10 +21,11 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
/** @file PowerPC Memory Management Unit emulation. */
#include "ppcmmu.h"
#include "devices/memctrlbase.h"
#include "memaccess.h"
#include <devices/memctrl/memctrlbase.h>
#include <memaccess.h>
#include "ppcemu.h"
#include "ppcmmu.h"
#include <array>
#include <cinttypes>
#include <functional>
@@ -356,6 +357,9 @@ uint32_t tlb_size_mask = TLB_SIZE - 1;
uint64_t UnmappedVal = -1ULL;
TLBEntry UnmappedMem = {TLB_INVALID_TAG, TLBFlags::PAGE_NOPHYS, 0, 0};
// Dummy page for catching writes to physical read-only pages
static std::array<uint8_t, 4096> dummy_page;
uint8_t CurITLBMode = {0xFF}; // current ITLB mode
uint8_t CurDTLBMode = {0xFF}; // current DTLB mode
@@ -527,7 +531,7 @@ static TLBEntry* itlb2_refill(uint32_t guest_va)
tlb_entry = tlb2_target_entry<TLBType::ITLB>(tag);
tlb_entry->tag = tag;
tlb_entry->flags = flags | TLBFlags::PAGE_MEM;
tlb_entry->host_va_offset = (int64_t)reg_desc->mem_ptr - guest_va +
tlb_entry->host_va_offs_r = (int64_t)reg_desc->mem_ptr - guest_va +
(phys_addr - reg_desc->start);
} else {
ABORT_F("Instruction fetch from unmapped memory at 0x%08X!\n", phys_addr);
@@ -600,12 +604,18 @@ static TLBEntry* dtlb2_refill(uint32_t guest_va, int is_write)
tlb_entry->reg_desc = reg_desc;
} else { // memory region backed by host memory
tlb_entry->flags = flags | TLBFlags::PAGE_MEM;
tlb_entry->host_va_offset = (int64_t)reg_desc->mem_ptr - guest_va +
(phys_addr - reg_desc->start);
tlb_entry->host_va_offs_r = (int64_t)reg_desc->mem_ptr - guest_va +
(phys_addr - reg_desc->start);
if (reg_desc->type == RT_ROM) {
// redirect writes to the dummy page for ROM regions
tlb_entry->host_va_offs_w = (int64_t)&dummy_page - guest_va;
} else {
tlb_entry->host_va_offs_w = tlb_entry->host_va_offs_r;
}
}
return tlb_entry;
} else {
LOG_F(WARNING, "Access to unmapped physical memory, phys_addr=0x%08X\n", phys_addr);
LOG_F(WARNING, "Access to unmapped physical memory, phys_addr=0x%08X", phys_addr);
return &UnmappedMem;
}
}
@@ -670,7 +680,7 @@ uint8_t *mmu_translate_imem(uint32_t vaddr)
#ifdef TLB_PROFILING
num_primary_itlb_hits++;
#endif
host_va = (uint8_t *)(tlb1_entry->host_va_offset + vaddr);
host_va = (uint8_t *)(tlb1_entry->host_va_offs_r + vaddr);
} else {
// primary ITLB miss -> look up address in the secondary ITLB
tlb2_entry = lookup_secondary_tlb<TLBType::ITLB>(vaddr, tag);
@@ -690,8 +700,8 @@ uint8_t *mmu_translate_imem(uint32_t vaddr)
// refill the primary ITLB
tlb1_entry->tag = tag;
tlb1_entry->flags = tlb2_entry->flags;
tlb1_entry->host_va_offset = tlb2_entry->host_va_offset;
host_va = (uint8_t *)(tlb1_entry->host_va_offset + vaddr);
tlb1_entry->host_va_offs_r = tlb2_entry->host_va_offs_r;
host_va = (uint8_t *)(tlb1_entry->host_va_offs_r + vaddr);
}
ppc_set_cur_instruction(host_va);
@@ -699,6 +709,271 @@ uint8_t *mmu_translate_imem(uint32_t vaddr)
return host_va;
}
void tlb_flush_entry(uint32_t ea)
{
TLBEntry *tlb_entry, *tlb1, *tlb2;
const uint32_t tag = ea & ~0xFFFUL;
for (int m = 0; m < 6; m++) {
switch (m) {
case 0:
tlb1 = &itlb1_mode1[0];
tlb2 = &itlb2_mode1[0];
break;
case 1:
tlb1 = &itlb1_mode2[0];
tlb2 = &itlb2_mode2[0];
break;
case 2:
tlb1 = &itlb1_mode3[0];
tlb2 = &itlb2_mode3[0];
break;
case 3:
tlb1 = &dtlb1_mode1[0];
tlb2 = &dtlb2_mode1[0];
break;
case 4:
tlb1 = &dtlb1_mode2[0];
tlb2 = &dtlb2_mode2[0];
break;
case 5:
tlb1 = &dtlb1_mode3[0];
tlb2 = &dtlb2_mode3[0];
break;
}
// flush primary TLB
tlb_entry = &tlb1[(ea >> PAGE_SIZE_BITS) & tlb_size_mask];
if (tlb_entry->tag == tag) {
tlb_entry->tag = TLB_INVALID_TAG;
//LOG_F(INFO, "Invalidated primary TLB entry at 0x%X", ea);
}
// flush secondary TLB
tlb_entry = &tlb2[((ea >> PAGE_SIZE_BITS) & tlb_size_mask) * TLB2_WAYS];
for (int i = 0; i < TLB2_WAYS; i++) {
if (tlb_entry[i].tag == tag) {
tlb_entry[i].tag = TLB_INVALID_TAG;
//LOG_F(INFO, "Invalidated secondary TLB entry at 0x%X", ea);
}
}
}
}
template <const TLBType tlb_type>
void tlb_flush_entries(TLBFlags type)
{
TLBEntry *m1_tlb, *m2_tlb, *m3_tlb;
int i;
if (tlb_type == TLBType::ITLB) {
m1_tlb = &itlb1_mode1[0];
m2_tlb = &itlb1_mode2[0];
m3_tlb = &itlb1_mode3[0];
} else {
m1_tlb = &dtlb1_mode1[0];
m2_tlb = &dtlb1_mode2[0];
m3_tlb = &dtlb1_mode3[0];
}
// Flush entries from the primary TLBs
for (i = 0; i < TLB_SIZE; i++) {
if (m1_tlb[i].flags & type) {
m1_tlb[i].tag = TLB_INVALID_TAG;
}
if (m2_tlb[i].flags & type) {
m2_tlb[i].tag = TLB_INVALID_TAG;
}
if (m3_tlb[i].flags & type) {
m3_tlb[i].tag = TLB_INVALID_TAG;
}
}
if (tlb_type == TLBType::ITLB) {
m1_tlb = &itlb2_mode1[0];
m2_tlb = &itlb2_mode2[0];
m3_tlb = &itlb2_mode3[0];
} else {
m1_tlb = &dtlb2_mode1[0];
m2_tlb = &dtlb2_mode2[0];
m3_tlb = &dtlb2_mode3[0];
}
// Flush entries from the secondary TLBs
for (i = 0; i < TLB_SIZE * TLB2_WAYS; i++) {
if (dtlb2_mode1[i].flags & type) {
dtlb2_mode1[i].tag = TLB_INVALID_TAG;
}
if (dtlb2_mode2[i].flags & type) {
dtlb2_mode2[i].tag = TLB_INVALID_TAG;
}
if (dtlb2_mode3[i].flags & type) {
dtlb2_mode3[i].tag = TLB_INVALID_TAG;
}
}
}
bool gTLBFlushBatEntries = false;
bool gTLBFlushPatEntries = false;
template <const TLBType tlb_type>
void tlb_flush_bat_entries()
{
if (!gTLBFlushBatEntries)
return;
tlb_flush_entries<tlb_type>(TLBE_FROM_BAT);
gTLBFlushBatEntries = false;
}
template <const TLBType tlb_type>
void tlb_flush_pat_entries()
{
if (!gTLBFlushPatEntries)
return;
tlb_flush_entries<tlb_type>(TLBE_FROM_PAT);
gTLBFlushPatEntries = false;
}
static void mpc601_bat_update(uint32_t bat_reg)
{
PPC_BAT_entry *ibat_entry, *dbat_entry;
uint32_t bsm, hi_mask;
int upper_reg_num;
upper_reg_num = bat_reg & 0xFFFFFFFE;
ibat_entry = &ibat_array[(bat_reg - 528) >> 1];
dbat_entry = &dbat_array[(bat_reg - 528) >> 1];
if (ppc_state.spr[bat_reg | 1] & 0x40) {
bsm = ppc_state.spr[upper_reg_num + 1] & 0x3F;
hi_mask = ~((bsm << 17) | 0x1FFFF);
ibat_entry->valid = true;
ibat_entry->access = (ppc_state.spr[upper_reg_num] >> 2) & 3;
ibat_entry->prot = ppc_state.spr[upper_reg_num] & 3;
ibat_entry->hi_mask = hi_mask;
ibat_entry->phys_hi = ppc_state.spr[upper_reg_num + 1] & hi_mask;
ibat_entry->bepi = ppc_state.spr[upper_reg_num] & hi_mask;
// copy IBAT entry to DBAT entry
*dbat_entry = *ibat_entry;
} else {
// disable the corresponding BAT paars
ibat_entry->valid = false;
dbat_entry->valid = false;
}
// MPC601 has unified BATs so we're going to fush both ITLB and DTLB
if (!gTLBFlushBatEntries) {
gTLBFlushBatEntries = true;
add_ctx_sync_action(&tlb_flush_bat_entries<TLBType::ITLB>);
add_ctx_sync_action(&tlb_flush_bat_entries<TLBType::DTLB>);
}
}
static void ppc_ibat_update(uint32_t bat_reg)
{
int upper_reg_num;
uint32_t bl, hi_mask;
PPC_BAT_entry* bat_entry;
upper_reg_num = bat_reg & 0xFFFFFFFE;
if (ppc_state.spr[upper_reg_num] & 3) { // is that BAT pair valid?
bat_entry = &ibat_array[(bat_reg - 528) >> 1];
bl = (ppc_state.spr[upper_reg_num] >> 2) & 0x7FF;
hi_mask = ~((bl << 17) | 0x1FFFF);
bat_entry->access = ppc_state.spr[upper_reg_num] & 3;
bat_entry->prot = ppc_state.spr[upper_reg_num + 1] & 3;
bat_entry->hi_mask = hi_mask;
bat_entry->phys_hi = ppc_state.spr[upper_reg_num + 1] & hi_mask;
bat_entry->bepi = ppc_state.spr[upper_reg_num] & hi_mask;
if (!gTLBFlushBatEntries) {
gTLBFlushBatEntries = true;
add_ctx_sync_action(&tlb_flush_bat_entries<TLBType::ITLB>);
}
}
}
static void ppc_dbat_update(uint32_t bat_reg)
{
int upper_reg_num;
uint32_t bl, hi_mask;
PPC_BAT_entry* bat_entry;
upper_reg_num = bat_reg & 0xFFFFFFFE;
if (ppc_state.spr[upper_reg_num] & 3) { // is that BAT pair valid?
bat_entry = &dbat_array[(bat_reg - 536) >> 1];
bl = (ppc_state.spr[upper_reg_num] >> 2) & 0x7FF;
hi_mask = ~((bl << 17) | 0x1FFFF);
bat_entry->access = ppc_state.spr[upper_reg_num] & 3;
bat_entry->prot = ppc_state.spr[upper_reg_num + 1] & 3;
bat_entry->hi_mask = hi_mask;
bat_entry->phys_hi = ppc_state.spr[upper_reg_num + 1] & hi_mask;
bat_entry->bepi = ppc_state.spr[upper_reg_num] & hi_mask;
if (!gTLBFlushBatEntries) {
gTLBFlushBatEntries = true;
add_ctx_sync_action(&tlb_flush_bat_entries<TLBType::DTLB>);
}
}
}
void mmu_pat_ctx_changed()
{
// Page address translation context changed so we need to flush
// all PAT entries from both ITLB and DTLB
if (!gTLBFlushPatEntries) {
gTLBFlushPatEntries = true;
add_ctx_sync_action(&tlb_flush_pat_entries<TLBType::ITLB>);
add_ctx_sync_action(&tlb_flush_pat_entries<TLBType::DTLB>);
}
}
void mmu_print_regs()
{
LOG_SCOPE_FUNCTION(INFO);
LOG_F(INFO, "MSR = 0x%X", ppc_state.msr);
LOG_F(INFO, "BAT registers:");
for (int i = 0; i < 4; i++) {
LOG_F(INFO, "IBAT%dU = 0x%X, IBAT%dL = 0x%X",
i, ppc_state.spr[528+i*2],
i, ppc_state.spr[529+i*2]);
}
if (!is_601_MMU) {
for (int i = 0; i < 4; i++) {
LOG_F(INFO, "DBAT%dU = 0x%X, DBAT%dL = 0x%X",
i, ppc_state.spr[536+i*2],
i, ppc_state.spr[537+i*2]);
}
}
LOG_F(INFO, "");
LOG_F(INFO, "SDR1 = 0x%X", ppc_state.spr[SPR::SDR1]);
LOG_F(INFO, "Segment registers:");
for (int i = 0; i < 16; i++) {
LOG_F(INFO, "SR%d = 0x%X", i, ppc_state.sr[i]);
}
}
// Forward declarations.
static uint32_t read_unaligned(uint32_t guest_va, uint8_t *host_va, uint32_t size);
static void write_unaligned(uint32_t guest_va, uint8_t *host_va, uint32_t value,
@@ -718,7 +993,7 @@ inline T mmu_read_vmem(uint32_t guest_va)
#ifdef TLB_PROFILING
num_primary_dtlb_hits++;
#endif
host_va = (uint8_t *)(tlb1_entry->host_va_offset + guest_va);
host_va = (uint8_t *)(tlb1_entry->host_va_offs_r + guest_va);
} else {
// primary TLB miss -> look up address in the secondary TLB
tlb2_entry = lookup_secondary_tlb<TLBType::DTLB>(guest_va, tag);
@@ -741,10 +1016,8 @@ inline T mmu_read_vmem(uint32_t guest_va)
if (tlb2_entry->flags & TLBFlags::PAGE_MEM) { // is it a real memory region?
// refill the primary TLB
tlb1_entry->tag = tag;
tlb1_entry->flags = tlb2_entry->flags;
tlb1_entry->host_va_offset = tlb2_entry->host_va_offset;
host_va = (uint8_t *)(tlb1_entry->host_va_offset + guest_va);
*tlb1_entry = *tlb2_entry;
host_va = (uint8_t *)(tlb1_entry->host_va_offs_r + guest_va);
} else { // otherwise, it's an access to a memory-mapped device
#ifdef MMU_PROFILING
iomem_reads_total++;
@@ -816,7 +1089,7 @@ inline void mmu_write_vmem(uint32_t guest_va, T value)
tlb2_entry->flags |= TLBFlags::PTE_SET_C;
}
}
host_va = (uint8_t *)(tlb1_entry->host_va_offset + guest_va);
host_va = (uint8_t *)(tlb1_entry->host_va_offs_w + guest_va);
} else {
// primary TLB miss -> look up address in the secondary TLB
tlb2_entry = lookup_secondary_tlb<TLBType::DTLB>(guest_va, tag);
@@ -852,10 +1125,8 @@ inline void mmu_write_vmem(uint32_t guest_va, T value)
if (tlb2_entry->flags & TLBFlags::PAGE_MEM) { // is it a real memory region?
// refill the primary TLB
tlb1_entry->tag = tag;
tlb1_entry->flags = tlb2_entry->flags;
tlb1_entry->host_va_offset = tlb2_entry->host_va_offset;
host_va = (uint8_t *)(tlb1_entry->host_va_offset + guest_va);
*tlb1_entry = *tlb2_entry;
host_va = (uint8_t *)(tlb1_entry->host_va_offs_w + guest_va);
} else { // otherwise, it's an access to a memory-mapped device
#ifdef MMU_PROFILING
iomem_writes_total++;
@@ -966,205 +1237,6 @@ static void write_unaligned(uint32_t guest_va, uint8_t *host_va, uint32_t value,
}
}
void tlb_flush_entry(uint32_t ea)
{
TLBEntry *tlb_entry, *tlb1, *tlb2;
const uint32_t tag = ea & ~0xFFFUL;
for (int m = 0; m < 6; m++) {
switch (m) {
case 0:
tlb1 = &itlb1_mode1[0];
tlb2 = &itlb2_mode1[0];
break;
case 1:
tlb1 = &itlb1_mode2[0];
tlb2 = &itlb2_mode2[0];
break;
case 2:
tlb1 = &itlb1_mode3[0];
tlb2 = &itlb2_mode3[0];
break;
case 3:
tlb1 = &dtlb1_mode1[0];
tlb2 = &dtlb2_mode1[0];
break;
case 4:
tlb1 = &dtlb1_mode2[0];
tlb2 = &dtlb2_mode2[0];
break;
case 5:
tlb1 = &dtlb1_mode3[0];
tlb2 = &dtlb2_mode3[0];
break;
}
// flush primary TLB
tlb_entry = &tlb1[(ea >> PAGE_SIZE_BITS) & tlb_size_mask];
if (tlb_entry->tag == tag) {
tlb_entry->tag = TLB_INVALID_TAG;
//LOG_F(INFO, "Invalidated primary TLB entry at 0x%X", ea);
}
// flush secondary TLB
tlb_entry = &tlb2[((ea >> PAGE_SIZE_BITS) & tlb_size_mask) * TLB2_WAYS];
for (int i = 0; i < TLB2_WAYS; i++) {
if (tlb_entry[i].tag == tag) {
tlb_entry[i].tag = TLB_INVALID_TAG;
//LOG_F(INFO, "Invalidated secondary TLB entry at 0x%X", ea);
}
}
}
}
void tlb_flush_entries(TLBFlags type)
{
int i;
// Flush BAT entries from the primary TLBs
for (i = 0; i < TLB_SIZE; i++) {
if (dtlb1_mode2[i].flags & type) {
dtlb1_mode2[i].tag = TLB_INVALID_TAG;
}
if (dtlb1_mode3[i].flags & type) {
dtlb1_mode3[i].tag = TLB_INVALID_TAG;
}
}
// Flush BAT entries from the secondary TLBs
for (i = 0; i < TLB_SIZE * TLB2_WAYS; i++) {
if (dtlb2_mode2[i].flags & type) {
dtlb2_mode2[i].tag = TLB_INVALID_TAG;
}
if (dtlb2_mode3[i].flags & type) {
dtlb2_mode3[i].tag = TLB_INVALID_TAG;
}
}
}
bool gTLBFlushBatEntries = false;
bool gTLBFlushPatEntries = false;
void tlb_flush_bat_entries()
{
if (!gTLBFlushBatEntries)
return;
tlb_flush_entries(TLBE_FROM_BAT);
gTLBFlushBatEntries = false;
}
void tlb_flush_pat_entries()
{
if (!gTLBFlushPatEntries)
return;
tlb_flush_entries(TLBE_FROM_PAT);
gTLBFlushPatEntries = false;
}
static void mpc601_bat_update(uint32_t bat_reg)
{
PPC_BAT_entry *ibat_entry, *dbat_entry;
uint32_t bsm, hi_mask;
uint8_t key, pp, prot;
int upper_reg_num;
upper_reg_num = bat_reg & 0xFFFFFFFE;
ibat_entry = &ibat_array[(bat_reg - 528) >> 1];
dbat_entry = &dbat_array[(bat_reg - 528) >> 1];
if (ppc_state.spr[bat_reg | 1] & 0x40) {
bsm = ppc_state.spr[upper_reg_num + 1] & 0x3F;
hi_mask = ~((bsm << 17) | 0x1FFFF);
ibat_entry->valid = true;
ibat_entry->access = (ppc_state.spr[upper_reg_num] >> 2) & 3;
ibat_entry->prot = ppc_state.spr[upper_reg_num] & 3;
ibat_entry->hi_mask = hi_mask;
ibat_entry->phys_hi = ppc_state.spr[upper_reg_num + 1] & hi_mask;
ibat_entry->bepi = ppc_state.spr[upper_reg_num] & hi_mask;
// copy IBAT entry to DBAT entry
*dbat_entry = *ibat_entry;
} else {
// disable the corresponding BAT paars
ibat_entry->valid = false;
dbat_entry->valid = false;
}
if (!gTLBFlushBatEntries) {
gTLBFlushBatEntries = true;
add_ctx_sync_action(&tlb_flush_bat_entries);
}
}
static void ppc_ibat_update(uint32_t bat_reg)
{
int upper_reg_num;
uint32_t bl, hi_mask;
PPC_BAT_entry* bat_entry;
upper_reg_num = bat_reg & 0xFFFFFFFE;
if (ppc_state.spr[upper_reg_num] & 3) { // is that BAT pair valid?
bat_entry = &ibat_array[(bat_reg - 528) >> 1];
bl = (ppc_state.spr[upper_reg_num] >> 2) & 0x7FF;
hi_mask = ~((bl << 17) | 0x1FFFF);
bat_entry->access = ppc_state.spr[upper_reg_num] & 3;
bat_entry->prot = ppc_state.spr[upper_reg_num + 1] & 3;
bat_entry->hi_mask = hi_mask;
bat_entry->phys_hi = ppc_state.spr[upper_reg_num + 1] & hi_mask;
bat_entry->bepi = ppc_state.spr[upper_reg_num] & hi_mask;
if (!gTLBFlushBatEntries) {
gTLBFlushBatEntries = true;
add_ctx_sync_action(&tlb_flush_bat_entries);
}
}
}
static void ppc_dbat_update(uint32_t bat_reg)
{
int upper_reg_num;
uint32_t bl, hi_mask;
PPC_BAT_entry* bat_entry;
upper_reg_num = bat_reg & 0xFFFFFFFE;
if (ppc_state.spr[upper_reg_num] & 3) { // is that BAT pair valid?
bat_entry = &dbat_array[(bat_reg - 536) >> 1];
bl = (ppc_state.spr[upper_reg_num] >> 2) & 0x7FF;
hi_mask = ~((bl << 17) | 0x1FFFF);
bat_entry->access = ppc_state.spr[upper_reg_num] & 3;
bat_entry->prot = ppc_state.spr[upper_reg_num + 1] & 3;
bat_entry->hi_mask = hi_mask;
bat_entry->phys_hi = ppc_state.spr[upper_reg_num + 1] & hi_mask;
bat_entry->bepi = ppc_state.spr[upper_reg_num] & hi_mask;
if (!gTLBFlushBatEntries) {
gTLBFlushBatEntries = true;
add_ctx_sync_action(&tlb_flush_bat_entries);
}
}
}
void mmu_pat_ctx_changed()
{
if (!gTLBFlushPatEntries) {
gTLBFlushPatEntries = true;
add_ctx_sync_action(&tlb_flush_pat_entries);
}
}
/* MMU profiling. */
#ifdef MMU_PROFILING
@@ -1300,7 +1372,7 @@ static inline T read_phys_mem(AddressMapEntry *mru_rgn, uint32_t addr)
if (entry) {
*mru_rgn = *entry;
} else {
LOG_F(ERROR, "Read from unmapped memory at 0x%08X!\n", addr);
LOG_F(ERROR, "Read from unmapped memory at 0x%08X!", addr);
return (-1ULL ? sizeof(T) == 8 : -1UL);
}
}
@@ -1330,7 +1402,7 @@ static inline T read_phys_mem(AddressMapEntry *mru_rgn, uint32_t addr)
return READ_QWORD_BE_A(mru_rgn->mem_ptr + (addr - mru_rgn->start));
}
default:
LOG_F(ERROR, "READ_PHYS: invalid size %lu passed\n", sizeof(T));
LOG_F(ERROR, "READ_PHYS: invalid size %lu passed", sizeof(T));
return (-1ULL ? sizeof(T) == 8 : -1UL);
}
} else if (mru_rgn->type & RT_MMIO) {
@@ -1341,7 +1413,7 @@ static inline T read_phys_mem(AddressMapEntry *mru_rgn, uint32_t addr)
return (mru_rgn->devobj->read(mru_rgn->start,
addr - mru_rgn->start, sizeof(T)));
} else {
LOG_F(ERROR, "READ_PHYS: invalid region type!\n");
LOG_F(ERROR, "READ_PHYS: invalid region type!");
return (-1ULL ? sizeof(T) == 8 : -1UL);
}
}
@@ -1354,7 +1426,7 @@ static inline void write_phys_mem(AddressMapEntry *mru_rgn, uint32_t addr, T val
if (entry) {
*mru_rgn = *entry;
} else {
LOG_F(ERROR, "Write to unmapped memory at 0x%08X!\n", addr);
LOG_F(ERROR, "Write to unmapped memory at 0x%08X!", addr);
return;
}
}
@@ -1388,7 +1460,7 @@ static inline void write_phys_mem(AddressMapEntry *mru_rgn, uint32_t addr, T val
}
break;
default:
LOG_F(ERROR, "WRITE_PHYS: invalid size %lu passed\n", sizeof(T));
LOG_F(ERROR, "WRITE_PHYS: invalid size %lu passed", sizeof(T));
return;
}
} else if (mru_rgn->type & RT_MMIO) {
@@ -1399,7 +1471,7 @@ static inline void write_phys_mem(AddressMapEntry *mru_rgn, uint32_t addr, T val
mru_rgn->devobj->write(mru_rgn->start, addr - mru_rgn->start, value,
sizeof(T));
} else {
LOG_F(ERROR, "WRITE_PHYS: invalid region type!\n");
LOG_F(ERROR, "WRITE_PHYS: invalid region type!");
}
}
@@ -1453,7 +1525,7 @@ static uint32_t ppc_mmu_addr_translate(uint32_t la, int is_write)
static void mem_write_unaligned(uint32_t addr, uint32_t value, uint32_t size) {
#ifdef MMU_DEBUG
LOG_F(WARNING, "Attempt to write unaligned %d bytes to 0x%08X\n", size, addr);
LOG_F(WARNING, "Attempt to write unaligned %d bytes to 0x%08X", size, addr);
#endif
if (((addr & 0xFFF) + size) > 0x1000) {
@@ -1504,7 +1576,7 @@ static inline uint64_t tlb_translate_addr(uint32_t guest_va)
// look up address in the primary TLB
tlb1_entry = &pCurDTLB1[(guest_va >> PAGE_SIZE_BITS) & tlb_size_mask];
if (tlb1_entry->tag == tag) { // primary TLB hit -> fast path
return tlb1_entry->host_va_offset + guest_va;
return tlb1_entry->host_va_offs_r + guest_va;
} else { // primary TLB miss -> look up address in the secondary TLB
tlb2_entry = &pCurDTLB2[((guest_va >> PAGE_SIZE_BITS) & tlb_size_mask) * TLB2_WAYS];
if (tlb2_entry->tag == tag) {
@@ -1543,8 +1615,8 @@ static inline uint64_t tlb_translate_addr(uint32_t guest_va)
// refill the primary TLB
tlb1_entry->tag = tag;
tlb1_entry->flags = tlb2_entry->flags;
tlb1_entry->host_va_offset = tlb2_entry->host_va_offset;
return tlb1_entry->host_va_offset + guest_va;
tlb1_entry->host_va_offs_r = tlb2_entry->host_va_offs_r;
return tlb1_entry->host_va_offs_r + guest_va;
} else { // an attempt to access a memory-mapped device
return guest_va - tlb2_entry->reg_desc->start;
}
@@ -1555,7 +1627,7 @@ static uint32_t mem_grab_unaligned(uint32_t addr, uint32_t size) {
uint32_t ret = 0;
#ifdef MMU_DEBUG
LOG_F(WARNING, "Attempt to read unaligned %d bytes from 0x%08X\n", size, addr);
LOG_F(WARNING, "Attempt to read unaligned %d bytes from 0x%08X", size, addr);
#endif
if (((addr & 0xFFF) + size) > 0x1000) {
@@ -1859,42 +1931,42 @@ void ppc_mmu_init(uint32_t cpu_version)
tlb_el.tag = TLB_INVALID_TAG;
tlb_el.flags = 0;
tlb_el.lru_bits = 0;
tlb_el.host_va_offset = 0;
tlb_el.host_va_offs_r = 0;
}
for (auto &tlb_el : itlb1_mode2) {
tlb_el.tag = TLB_INVALID_TAG;
tlb_el.flags = 0;
tlb_el.lru_bits = 0;
tlb_el.host_va_offset = 0;
tlb_el.host_va_offs_r = 0;
}
for (auto &tlb_el : itlb1_mode3) {
tlb_el.tag = TLB_INVALID_TAG;
tlb_el.flags = 0;
tlb_el.lru_bits = 0;
tlb_el.host_va_offset = 0;
tlb_el.host_va_offs_r = 0;
}
for (auto &tlb_el : itlb2_mode1) {
tlb_el.tag = TLB_INVALID_TAG;
tlb_el.flags = 0;
tlb_el.lru_bits = 0;
tlb_el.host_va_offset = 0;
tlb_el.host_va_offs_r = 0;
}
for (auto &tlb_el : itlb2_mode2) {
tlb_el.tag = TLB_INVALID_TAG;
tlb_el.flags = 0;
tlb_el.lru_bits = 0;
tlb_el.host_va_offset = 0;
tlb_el.host_va_offs_r = 0;
}
for (auto &tlb_el : itlb2_mode3) {
tlb_el.tag = TLB_INVALID_TAG;
tlb_el.flags = 0;
tlb_el.lru_bits = 0;
tlb_el.host_va_offset = 0;
tlb_el.host_va_offs_r = 0;
}
// invalidate all DTLB entries
@@ -1902,42 +1974,48 @@ void ppc_mmu_init(uint32_t cpu_version)
tlb_el.tag = TLB_INVALID_TAG;
tlb_el.flags = 0;
tlb_el.lru_bits = 0;
tlb_el.host_va_offset = 0;
tlb_el.host_va_offs_r = 0;
tlb_el.host_va_offs_w = 0;
}
for (auto &tlb_el : dtlb1_mode2) {
tlb_el.tag = TLB_INVALID_TAG;
tlb_el.flags = 0;
tlb_el.lru_bits = 0;
tlb_el.host_va_offset = 0;
tlb_el.host_va_offs_r = 0;
tlb_el.host_va_offs_w = 0;
}
for (auto &tlb_el : dtlb1_mode3) {
tlb_el.tag = TLB_INVALID_TAG;
tlb_el.flags = 0;
tlb_el.lru_bits = 0;
tlb_el.host_va_offset = 0;
tlb_el.host_va_offs_r = 0;
tlb_el.host_va_offs_w = 0;
}
for (auto &tlb_el : dtlb2_mode1) {
tlb_el.tag = TLB_INVALID_TAG;
tlb_el.flags = 0;
tlb_el.lru_bits = 0;
tlb_el.host_va_offset = 0;
tlb_el.host_va_offs_r = 0;
tlb_el.host_va_offs_w = 0;
}
for (auto &tlb_el : dtlb2_mode2) {
tlb_el.tag = TLB_INVALID_TAG;
tlb_el.flags = 0;
tlb_el.lru_bits = 0;
tlb_el.host_va_offset = 0;
tlb_el.host_va_offs_r = 0;
tlb_el.host_va_offs_w = 0;
}
for (auto &tlb_el : dtlb2_mode3) {
tlb_el.tag = TLB_INVALID_TAG;
tlb_el.flags = 0;
tlb_el.lru_bits = 0;
tlb_el.host_va_offset = 0;
tlb_el.host_va_offs_r = 0;
tlb_el.host_va_offs_w = 0;
}
mmu_change_mode();
+8 -3
View File
@@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
Copyright (C) 2018-21 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@@ -24,11 +24,12 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#ifndef PPCMMU_H
#define PPCMMU_H
#include <devices/memctrl/memctrlbase.h>
#include <array>
#include <cinttypes>
#include <functional>
#include <vector>
#include "devices/memctrlbase.h"
/* Uncomment this to exhaustive MMU integrity checks. */
//#define MMU_INTEGRITY_CHECKS
@@ -70,6 +71,8 @@ typedef struct PATResult {
} PATResult;
#define PAGE_SIZE_BITS 12
#define PAGE_SIZE (1 << PAGE_SIZE_BITS)
#define PAGE_MASK ~(PAGE_SIZE - 1)
#define TLB_SIZE 4096
#define TLB2_WAYS 4
#define TLB_INVALID_TAG 0xFFFFFFFF
@@ -79,9 +82,11 @@ typedef struct TLBEntry {
uint16_t flags;
uint16_t lru_bits;
union {
int64_t host_va_offset;
int64_t host_va_offs_r;
AddressMapEntry* reg_desc;
};
int64_t host_va_offs_w;
int64_t unused;
} TLBEntry;
enum TLBFlags : uint16_t {
+176 -127
View File
@@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
Copyright (C) 2018-21 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@@ -21,6 +21,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
// General opcodes for the processor - ppcopcodes.cpp
#include <core/timermanager.h>
#include "ppcemu.h"
#include "ppcmmu.h"
#include <array>
@@ -843,7 +844,29 @@ void dppc_interpreter::ppc_mtmsr() {
}
reg_s = (ppc_cur_instruction >> 21) & 31;
ppc_state.msr = ppc_state.gpr[reg_s];
mmu_change_mode();
// generate External Interrupt Exception
// if CPU interrupt line is asserted
if (ppc_state.msr & 0x8000 && int_pin) {
LOG_F(WARNING, "MTMSR: CPU INT pending, generate CPU exception");
ppc_exception_handler(Except_Type::EXC_EXT_INT, 0);
} else {
mmu_change_mode();
}
}
static inline uint64_t calc_rtcl_value()
{
uint64_t diff = get_virt_time_ns() - tbr_wr_timestamp;
uint64_t rtc_inc = diff * tbr_freq_hz / NS_PER_SEC;
uint64_t rtc_l = rtc_lo + (rtc_inc << 7);
if (rtc_l >= ONE_BILLION_NS) { // check RTCL overflow
rtc_hi += rtc_l / ONE_BILLION_NS;
rtc_lo = rtc_l % ONE_BILLION_NS;
tbr_wr_timestamp = get_virt_time_ns();
rtc_l = rtc_lo;
}
return rtc_l & 0x3FFFFF80UL;
}
void dppc_interpreter::ppc_mfspr() {
@@ -854,8 +877,31 @@ void dppc_interpreter::ppc_mfspr() {
num_supervisor_instrs++;
}
#endif
reg_d = (ppc_cur_instruction >> 21) & 31;
ppc_state.gpr[reg_d] = ppc_state.spr[ref_spr];
switch (ref_spr) {
case SPR::RTCL_U:
ppc_state.spr[SPR::RTCL_U] = calc_rtcl_value();
break;
case SPR::RTCU_U:
ppc_state.spr[SPR::RTCL_U] = rtc_hi;
break;
}
ppc_state.gpr[(ppc_cur_instruction >> 21) & 31] = ppc_state.spr[ref_spr];
}
static inline uint64_t calc_tbr_value()
{
uint64_t diff = get_virt_time_ns() - tbr_wr_timestamp;
uint64_t tbr_inc = diff * tbr_freq_hz / NS_PER_SEC;
return (tbr_wr_value + tbr_inc);
}
static void update_timebase(uint64_t mask, uint64_t new_val)
{
uint64_t tbr_value = calc_tbr_value();
tbr_wr_value = (tbr_value & mask) | new_val;
tbr_wr_timestamp = get_virt_time_ns();
}
void dppc_interpreter::ppc_mtspr() {
@@ -868,22 +914,30 @@ void dppc_interpreter::ppc_mtspr() {
}
#endif
if (ref_spr != 287) {
ppc_state.spr[ref_spr] = ppc_state.gpr[reg_s];
uint32_t val = ppc_state.gpr[reg_s];
if (ref_spr != SPR::PVR) { // prevent writes to the read-only PVR
ppc_state.spr[ref_spr] = val;
}
if (ref_spr == SPR::SDR1) {
if (ref_spr == SPR::SDR1) { // adapt to SDR1 changes
mmu_pat_ctx_changed();
}
switch (ref_spr) {
// Mirror the TBRs in the SPR range to the user-mode TBRs.
case 284:
timebase_counter = (timebase_counter & 0xFFFFFFFF00000000ULL) + ppc_state.gpr[reg_s];
case SPR::RTCL_S:
rtc_lo = val & 0x3FFFFF80UL;
tbr_wr_timestamp = get_virt_time_ns();
break;
case 285:
timebase_counter = (timebase_counter & 0x00000000FFFFFFFFULL) +
(((uint64_t)(ppc_state.gpr[reg_s])) << 32);
case SPR::RTCU_S:
rtc_hi = val;
tbr_wr_timestamp = get_virt_time_ns();
break;
case SPR::TBL_S:
update_timebase(0xFFFFFFFF00000000ULL, val);
break;
case SPR::TBU_S:
update_timebase(0x00000000FFFFFFFFULL, (uint64_t)(val) << 32);
break;
case 528:
case 529:
@@ -910,15 +964,18 @@ void dppc_interpreter::ppc_mtspr() {
void dppc_interpreter::ppc_mftb() {
uint32_t ref_spr = (((ppc_cur_instruction >> 11) & 31) << 5) | ((ppc_cur_instruction >> 16) & 31);
reg_d = (ppc_cur_instruction >> 21) & 31;
uint64_t tbr_value = calc_tbr_value();
switch (ref_spr) {
case 268:
ppc_state.gpr[reg_d] = timebase_counter & 0xFFFFFFFFUL;
break;
case 269:
ppc_state.gpr[reg_d] = (timebase_counter >> 32) & 0xFFFFFFFFUL;
break;
default:
LOG_F(ERROR, "Invalid TBR access attempted !\n");
case SPR::TBL_U:
ppc_state.gpr[reg_d] = tbr_value & 0xFFFFFFFFUL;
break;
case SPR::TBU_U:
ppc_state.gpr[reg_d] = (tbr_value >> 32) & 0xFFFFFFFFUL;
break;
default:
ppc_exception_handler(Except_Type::EXC_PROGRAM, Exc_Cause::ILLEGAL_OP);
}
}
@@ -978,7 +1035,7 @@ void dppc_interpreter::ppc_b() {
uint32_t quick_test = (ppc_cur_instruction & 0x03FFFFFC);
adr_li = (quick_test < 0x2000000) ? quick_test : (0xFC000000UL + quick_test);
ppc_next_instruction_address = (uint32_t)(ppc_state.pc + adr_li);
bb_kind = BB_end_kind::BB_BRANCH;
exec_flags = EXEF_BRANCH;
}
void dppc_interpreter::ppc_bl() {
@@ -986,14 +1043,14 @@ void dppc_interpreter::ppc_bl() {
adr_li = (quick_test < 0x2000000) ? quick_test : (0xFC000000UL + quick_test);
ppc_next_instruction_address = (uint32_t)(ppc_state.pc + adr_li);
ppc_state.spr[SPR::LR] = (uint32_t)(ppc_state.pc + 4);
bb_kind = BB_end_kind::BB_BRANCH;
exec_flags = EXEF_BRANCH;
}
void dppc_interpreter::ppc_ba() {
uint32_t quick_test = (ppc_cur_instruction & 0x03FFFFFC);
adr_li = (quick_test < 0x2000000) ? quick_test : (0xFC000000UL + quick_test);
ppc_next_instruction_address = adr_li;
bb_kind = BB_end_kind::BB_BRANCH;
exec_flags = EXEF_BRANCH;
}
void dppc_interpreter::ppc_bla() {
@@ -1001,7 +1058,7 @@ void dppc_interpreter::ppc_bla() {
adr_li = (quick_test < 0x2000000) ? quick_test : (0xFC000000UL + quick_test);
ppc_next_instruction_address = adr_li;
ppc_state.spr[SPR::LR] = ppc_state.pc + 4;
bb_kind = BB_end_kind::BB_BRANCH;
exec_flags = EXEF_BRANCH;
}
void dppc_interpreter::ppc_bc() {
@@ -1019,7 +1076,7 @@ void dppc_interpreter::ppc_bc() {
if (ctr_ok && cnd_ok) {
ppc_next_instruction_address = (ppc_state.pc + br_bd);
bb_kind = BB_end_kind::BB_BRANCH;
exec_flags = EXEF_BRANCH;
}
}
@@ -1038,7 +1095,7 @@ void dppc_interpreter::ppc_bca() {
if (ctr_ok && cnd_ok) {
ppc_next_instruction_address = br_bd;
bb_kind = BB_end_kind::BB_BRANCH;
exec_flags = EXEF_BRANCH;
}
}
@@ -1057,7 +1114,7 @@ void dppc_interpreter::ppc_bcl() {
if (ctr_ok && cnd_ok) {
ppc_next_instruction_address = (ppc_state.pc + br_bd);
bb_kind = BB_end_kind::BB_BRANCH;
exec_flags = EXEF_BRANCH;
}
ppc_state.spr[SPR::LR] = ppc_state.pc + 4;
}
@@ -1077,7 +1134,7 @@ void dppc_interpreter::ppc_bcla() {
if (ctr_ok && cnd_ok) {
ppc_next_instruction_address = br_bd;
bb_kind = BB_end_kind::BB_BRANCH;
exec_flags = EXEF_BRANCH;
}
ppc_state.spr[SPR::LR] = ppc_state.pc + 4;
}
@@ -1091,7 +1148,7 @@ void dppc_interpreter::ppc_bcctr() {
if (cnd_ok) {
ppc_next_instruction_address = (ppc_state.spr[SPR::CTR] & 0xFFFFFFFCUL);
bb_kind = BB_end_kind::BB_BRANCH;
exec_flags = EXEF_BRANCH;
}
}
@@ -1104,7 +1161,7 @@ void dppc_interpreter::ppc_bcctrl() {
if (cnd_ok) {
ppc_next_instruction_address = (ppc_state.spr[SPR::CTR] & 0xFFFFFFFCUL);
bb_kind = BB_end_kind::BB_BRANCH;
exec_flags = EXEF_BRANCH;
}
ppc_state.spr[SPR::LR] = ppc_state.pc + 4;
}
@@ -1123,7 +1180,7 @@ void dppc_interpreter::ppc_bclr() {
if (ctr_ok && cnd_ok) {
ppc_next_instruction_address = (ppc_state.spr[SPR::LR] & 0xFFFFFFFCUL);
bb_kind = BB_end_kind::BB_BRANCH;
exec_flags = EXEF_BRANCH;
}
}
@@ -1141,7 +1198,7 @@ void dppc_interpreter::ppc_bclrl() {
if (ctr_ok && cnd_ok) {
ppc_next_instruction_address = (ppc_state.spr[SPR::LR] & 0xFFFFFFFCUL);
bb_kind = BB_end_kind::BB_BRANCH;
exec_flags = EXEF_BRANCH;
}
ppc_state.spr[SPR::LR] = ppc_state.pc + 4;
}
@@ -1150,7 +1207,7 @@ void dppc_interpreter::ppc_bclrl() {
void dppc_interpreter::ppc_cmp() {
#ifdef CHECK_INVALID
if (ppc_cur_instruction & 0x200000) {
LOG_F(WARNING, "Invalid CMP instruction form (L=1)!\n");
LOG_F(WARNING, "Invalid CMP instruction form (L=1)!");
return;
}
#endif
@@ -1167,7 +1224,7 @@ void dppc_interpreter::ppc_cmp() {
void dppc_interpreter::ppc_cmpi() {
#ifdef CHECK_INVALID
if (ppc_cur_instruction & 0x200000) {
LOG_F(WARNING, "Invalid CMPI instruction form (L=1)!\n");
LOG_F(WARNING, "Invalid CMPI instruction form (L=1)!");
return;
}
#endif
@@ -1184,7 +1241,7 @@ void dppc_interpreter::ppc_cmpi() {
void dppc_interpreter::ppc_cmpl() {
#ifdef CHECK_INVALID
if (ppc_cur_instruction & 0x200000) {
LOG_F(WARNING, "Invalid CMPL instruction form (L=1)!\n");
LOG_F(WARNING, "Invalid CMPL instruction form (L=1)!");
return;
}
#endif
@@ -1201,7 +1258,7 @@ void dppc_interpreter::ppc_cmpl() {
void dppc_interpreter::ppc_cmpli() {
#ifdef CHECK_INVALID
if (ppc_cur_instruction & 0x200000) {
LOG_F(WARNING, "Invalid CMPLI instruction form (L=1)!\n");
LOG_F(WARNING, "Invalid CMPLI instruction form (L=1)!");
return;
}
#endif
@@ -1219,14 +1276,17 @@ void dppc_interpreter::ppc_cmpli() {
void dppc_interpreter::ppc_mcrf() {
int crf_d = (ppc_cur_instruction >> 21) & 0x1C;
int crf_s = (ppc_cur_instruction >> 16) & 0x1C;
uint32_t grab_s = ppc_state.cr & (0xf0000000UL >> crf_s);
ppc_state.cr = ((ppc_state.cr & ~(0xf0000000UL >> crf_d)) | (grab_s >> crf_d));
// extract and right justify source flags field
uint32_t grab_s = (ppc_state.cr >> (28 - crf_s)) & 0xF;
ppc_state.cr = (ppc_state.cr & ~(0xf0000000UL >> crf_d)) | (grab_s << (28 - crf_d));
}
void dppc_interpreter::ppc_crand() {
ppc_grab_regsdab();
if ((ppc_state.cr & (0x80000000UL >> reg_a)) && (ppc_state.cr & (0x80000000UL >> reg_b))) {
uint8_t ir = (ppc_state.cr >> (31 - reg_a)) & (ppc_state.cr >> (31 - reg_b));
if (ir & 1) {
ppc_state.cr |= (0x80000000UL >> reg_d);
} else {
ppc_state.cr &= ~(0x80000000UL >> reg_d);
@@ -1243,37 +1303,43 @@ void dppc_interpreter::ppc_crandc() {
}
void dppc_interpreter::ppc_creqv() {
ppc_grab_regsdab();
if (!((ppc_state.cr & (0x80000000UL >> reg_a)) ^ (ppc_state.cr & (0x80000000UL >> reg_b)))) {
ppc_state.cr |= (0x80000000UL >> reg_d);
} else {
uint8_t ir = (ppc_state.cr >> (31 - reg_a)) ^ (ppc_state.cr >> (31 - reg_b));
if (ir & 1) { // compliment is implemented by swapping the following if/else bodies
ppc_state.cr &= ~(0x80000000UL >> reg_d);
} else {
ppc_state.cr |= (0x80000000UL >> reg_d);
}
}
void dppc_interpreter::ppc_crnand() {
ppc_grab_regsdab();
if (!((ppc_state.cr & (0x80000000UL >> reg_a)) && (ppc_state.cr & (0x80000000UL >> reg_b)))) {
ppc_state.cr |= (0x80000000UL >> reg_d);
} else {
uint8_t ir = (ppc_state.cr >> (31 - reg_a)) & (ppc_state.cr >> (31 - reg_b));
if (ir & 1) {
ppc_state.cr &= ~(0x80000000UL >> reg_d);
} else {
ppc_state.cr |= (0x80000000UL >> reg_d);
}
}
void dppc_interpreter::ppc_crnor() {
ppc_grab_regsdab();
if (!((ppc_state.cr & (0x80000000UL >> reg_a)) || (ppc_state.cr & (0x80000000UL >> reg_b)))) {
uint8_t ir = (ppc_state.cr >> (31 - reg_a)) | (ppc_state.cr >> (31 - reg_b));
if (ir & 1) {
ppc_state.cr &= ~(0x80000000UL >> reg_d);
} else {
ppc_state.cr |= (0x80000000UL >> reg_d);
}
}
void dppc_interpreter::ppc_cror() {
ppc_grab_regsdab();
uint8_t ir = (ppc_state.cr >> (31 - reg_a)) | (ppc_state.cr >> (31 - reg_b));
if (ir & 1) {
ppc_state.cr |= (0x80000000UL >> reg_d);
} else {
ppc_state.cr &= ~(0x80000000UL >> reg_d);
}
}
void dppc_interpreter::ppc_cror() {
ppc_grab_regsdab();
if ((ppc_state.cr & (0x80000000UL >> reg_a)) || (ppc_state.cr & (0x80000000UL >> reg_b))) {
ppc_state.cr |= (0x80000000UL >> reg_d);
} else {
ppc_state.cr &= ~(0x80000000UL >> reg_d);
}
}
void dppc_interpreter::ppc_crorc() {
ppc_grab_regsdab();
if ((ppc_state.cr & (0x80000000UL >> reg_a)) || !(ppc_state.cr & (0x80000000UL >> reg_b))) {
@@ -1284,7 +1350,8 @@ void dppc_interpreter::ppc_crorc() {
}
void dppc_interpreter::ppc_crxor() {
ppc_grab_regsdab();
if ((ppc_state.cr & (0x80000000UL >> reg_a)) ^ (ppc_state.cr & (0x80000000UL >> reg_b))) {
uint8_t ir = (ppc_state.cr >> (31 - reg_a)) ^ (ppc_state.cr >> (31 - reg_b));
if (ir & 1) {
ppc_state.cr |= (0x80000000UL >> reg_d);
} else {
ppc_state.cr &= ~(0x80000000UL >> reg_d);
@@ -1300,15 +1367,28 @@ void dppc_interpreter::ppc_rfi() {
uint32_t new_srr1_val = (ppc_state.spr[SPR::SRR1] & 0x87C0FF73UL);
uint32_t new_msr_val = (ppc_state.msr & ~(0x87C0FF73UL));
ppc_state.msr = (new_msr_val | new_srr1_val) & 0xFFFBFFFFUL;
// generate External Interrupt Exception
// if CPU interrupt line is still asserted
if (ppc_state.msr & 0x8000 && int_pin) {
uint32_t save_srr0 = ppc_state.spr[SPR::SRR0] & 0xFFFFFFFCUL;
ppc_exception_handler(Except_Type::EXC_EXT_INT, 0);
ppc_state.spr[SPR::SRR0] = save_srr0;
return;
}
ppc_next_instruction_address = ppc_state.spr[SPR::SRR0] & 0xFFFFFFFCUL;
do_ctx_sync(); // RFI is context synchronizing
mmu_change_mode();
grab_return = true;
bb_kind = BB_end_kind::BB_RFI;
exec_flags = EXEF_RFI;
}
void dppc_interpreter::ppc_sc() {
do_ctx_sync(); // SC is context synchronizing!
ppc_exception_handler(Except_Type::EXC_SYSCALL, 0x20000);
}
@@ -1918,41 +1998,33 @@ void dppc_interpreter::ppc_lswi() {
num_int_loads++;
#endif
ppc_grab_regsda();
ppc_effective_address = reg_a ? ppc_result_a : 0;
grab_inb = (ppc_cur_instruction >> 11) & 31;
grab_inb = grab_inb ? grab_inb : 32;
ppc_effective_address = reg_a ? ppc_result_a : 0;
grab_inb = (ppc_cur_instruction >> 11) & 0x1F;
grab_inb = grab_inb ? grab_inb : 32;
uint32_t stringed_word = 0;
while (grab_inb > 0) {
switch (grab_inb) {
case 1:
stringed_word = mmu_read_vmem<uint8_t>(ppc_effective_address) << 24;
//stringed_word = mem_grab_byte(ppc_effective_address) << 24;
ppc_state.gpr[reg_d] = stringed_word;
ppc_state.gpr[reg_d] = mmu_read_vmem<uint8_t>(ppc_effective_address) << 24;
grab_inb = 0;
break;
case 2:
stringed_word = mmu_read_vmem<uint8_t>(ppc_effective_address) << 24;
//stringed_word = mem_grab_byte(ppc_effective_address) << 24;
stringed_word += mmu_read_vmem<uint8_t>(ppc_effective_address + 1) << 16;
//stringed_word += mem_grab_byte(ppc_effective_address + 1) << 16;
ppc_state.gpr[reg_d] = stringed_word;
ppc_state.gpr[reg_d] = mmu_read_vmem<uint16_t>(ppc_effective_address) << 16;
grab_inb = 0;
break;
case 3:
stringed_word = mmu_read_vmem<uint8_t>(ppc_effective_address) << 24;
//stringed_word = mem_grab_byte(ppc_effective_address) << 24;
stringed_word += mmu_read_vmem<uint8_t>(ppc_effective_address + 1) << 16;
//stringed_word += mem_grab_byte(ppc_effective_address + 1) << 16;
stringed_word += mmu_read_vmem<uint8_t>(ppc_effective_address + 2) << 8;
//stringed_word += mem_grab_byte(ppc_effective_address + 2) << 8;
stringed_word = mmu_read_vmem<uint16_t>(ppc_effective_address) << 16;
stringed_word += mmu_read_vmem<uint8_t>(ppc_effective_address + 2) << 8;
ppc_state.gpr[reg_d] = stringed_word;
grab_inb = 0;
break;
default:
//ppc_state.gpr[reg_d] = mem_grab_word(ppc_effective_address);
ppc_state.gpr[reg_d] = mmu_read_vmem<uint16_t>(ppc_effective_address);
ppc_state.gpr[reg_d] = mmu_read_vmem<uint32_t>(ppc_effective_address);
reg_d++;
if (reg_d >= 32) { // wrap around through GPR0
reg_d = 0;
}
ppc_effective_address += 4;
grab_inb -= 4;
}
@@ -1966,47 +2038,36 @@ void dppc_interpreter::ppc_lswx() {
ppc_grab_regsdab();
// Invalid instruction forms
if ((ppc_result_d == 0) && (ppc_result_a == 0)) {
ppc_exception_handler(Except_Type::EXC_PROGRAM, Exc_Cause::ILLEGAL_OP);
}
if ((ppc_result_d == ppc_result_a) || (ppc_result_a == ppc_result_b)) {
if ((reg_d | reg_a) == 0 || (reg_d == reg_a) || (reg_d == reg_b)) {
ppc_exception_handler(Except_Type::EXC_PROGRAM, Exc_Cause::ILLEGAL_OP);
}
ppc_effective_address = reg_a ? (ppc_result_a + ppc_result_b) : ppc_result_b;
grab_inb = ppc_state.spr[SPR::XER] & 127;
grab_inb = ppc_state.spr[SPR::XER] & 0x7F;
uint32_t stringed_word = 0;
while (grab_inb > 0) {
switch (grab_inb) {
case 1:
stringed_word = mmu_read_vmem<uint8_t>(ppc_effective_address) << 24;
//stringed_word = mem_grab_byte(ppc_effective_address) << 24;
ppc_state.gpr[reg_d] = stringed_word;
ppc_state.gpr[reg_d] = mmu_read_vmem<uint8_t>(ppc_effective_address) << 24;
grab_inb = 0;
break;
case 2:
stringed_word = mmu_read_vmem<uint8_t>(ppc_effective_address) << 24;
//stringed_word = mem_grab_byte(ppc_effective_address) << 24;
stringed_word += mmu_read_vmem<uint8_t>(ppc_effective_address + 1) << 16;
//stringed_word += mem_grab_byte(ppc_effective_address + 1) << 16;
ppc_state.gpr[reg_d] = stringed_word;
ppc_state.gpr[reg_d] = mmu_read_vmem<uint16_t>(ppc_effective_address) << 16;
grab_inb = 0;
break;
case 3:
stringed_word = mmu_read_vmem<uint8_t>(ppc_effective_address) << 24;
//stringed_word = mem_grab_byte(ppc_effective_address) << 24;
stringed_word += mmu_read_vmem<uint8_t>(ppc_effective_address + 1) << 16;
//stringed_word += mem_grab_byte(ppc_effective_address + 1) << 16;
stringed_word = mmu_read_vmem<uint16_t>(ppc_effective_address) << 16;
stringed_word += mmu_read_vmem<uint8_t>(ppc_effective_address + 2) << 8;
//stringed_word += mem_grab_byte(ppc_effective_address + 2) << 8;
ppc_state.gpr[reg_d] = stringed_word;
grab_inb = 0;
break;
default:
//ppc_state.gpr[reg_d] = mem_grab_word(ppc_effective_address);
ppc_state.gpr[reg_d] = mmu_read_vmem<uint16_t>(ppc_effective_address);
ppc_state.gpr[reg_d] = mmu_read_vmem<uint32_t>(ppc_effective_address);
reg_d++;
if (reg_d >= 32) { // wrap around through GPR0
reg_d = 0;
}
ppc_effective_address += 4;
grab_inb -= 4;
}
@@ -2018,37 +2079,31 @@ void dppc_interpreter::ppc_stswi() {
num_int_stores++;
#endif
ppc_grab_regssa();
ppc_effective_address = reg_a ? ppc_result_a : 0;
grab_inb = (ppc_cur_instruction >> 11) & 31;
grab_inb = (grab_inb & 32) == 0 ? 32 : grab_inb;
ppc_effective_address = reg_a ? ppc_result_a : 0;
grab_inb = (ppc_cur_instruction >> 11) & 0x1F;
grab_inb = grab_inb ? grab_inb : 32;
while (grab_inb > 0) {
switch (grab_inb) {
case 1:
mmu_write_vmem<uint8_t>(ppc_effective_address, (ppc_result_d >> 24));
//mem_write_byte(ppc_effective_address, (ppc_result_d >> 24));
mmu_write_vmem<uint8_t>(ppc_effective_address, ppc_state.gpr[reg_s] >> 24);
grab_inb = 0;
break;
case 2:
mmu_write_vmem<uint8_t>(ppc_effective_address, ((ppc_result_d >> 24) & 0xFF));
//mem_write_byte(ppc_effective_address, ((ppc_result_d >> 24) & 0xFF));
mmu_write_vmem<uint8_t>((ppc_effective_address + 1), ((ppc_result_d >> 16) & 0xFF));
//mem_write_byte((ppc_effective_address + 1), ((ppc_result_d >> 16) & 0xFF));
mmu_write_vmem<uint16_t>(ppc_effective_address, ppc_state.gpr[reg_s] >> 16);
grab_inb = 0;
break;
case 3:
mmu_write_vmem<uint8_t>(ppc_effective_address, ((ppc_result_d >> 24) & 0xFF));
//mem_write_byte(ppc_effective_address, ((ppc_result_d >> 24) & 0xFF));
mmu_write_vmem<uint8_t>((ppc_effective_address + 1), ((ppc_result_d >> 16) & 0xFF));
//mem_write_byte((ppc_effective_address + 1), ((ppc_result_d >> 16) & 0xFF));
mmu_write_vmem<uint8_t>((ppc_effective_address + 2), ((ppc_result_d >> 8) & 0xFF));
//mem_write_byte((ppc_effective_address + 2), ((ppc_result_d >> 8) & 0xFF));
mmu_write_vmem<uint16_t>(ppc_effective_address, ppc_state.gpr[reg_s] >> 16);
mmu_write_vmem<uint8_t>(ppc_effective_address + 2, (ppc_state.gpr[reg_s] >> 8) & 0xFF);
grab_inb = 0;
break;
default:
mmu_write_vmem<uint32_t>(ppc_effective_address, ppc_result_d);
//mem_write_dword(ppc_effective_address, ppc_result_d);
mmu_write_vmem<uint32_t>(ppc_effective_address, ppc_state.gpr[reg_s]);
reg_s++;
if (reg_s >= 32) { // wrap around through GPR0
reg_s = 0;
}
ppc_effective_address += 4;
grab_inb -= 4;
}
@@ -2066,30 +2121,24 @@ void dppc_interpreter::ppc_stswx() {
while (grab_inb > 0) {
switch (grab_inb) {
case 1:
mmu_write_vmem<uint8_t>(ppc_effective_address, (ppc_result_d >> 24));
//mem_write_byte(ppc_effective_address, (ppc_result_d >> 24));
mmu_write_vmem<uint8_t>(ppc_effective_address, ppc_state.gpr[reg_s] >> 24);
grab_inb = 0;
break;
case 2:
mmu_write_vmem<uint8_t>(ppc_effective_address, ((ppc_result_d >> 24) & 0xFF));
//mem_write_byte(ppc_effective_address, ((ppc_result_d >> 24) & 0xFF));
mmu_write_vmem<uint8_t>((ppc_effective_address + 1), ((ppc_result_d >> 16) & 0xFF));
//mem_write_byte((ppc_effective_address + 1), ((ppc_result_d >> 16) & 0xFF));
mmu_write_vmem<uint16_t>(ppc_effective_address, ppc_state.gpr[reg_s] >> 16);
grab_inb = 0;
break;
case 3:
mmu_write_vmem<uint8_t>(ppc_effective_address, ((ppc_result_d >> 24) & 0xFF));
//mem_write_byte(ppc_effective_address, ((ppc_result_d >> 24) & 0xFF));
mmu_write_vmem<uint8_t>((ppc_effective_address + 1), ((ppc_result_d >> 16) & 0xFF));
//mem_write_byte((ppc_effective_address + 1), ((ppc_result_d >> 16) & 0xFF));
mmu_write_vmem<uint8_t>((ppc_effective_address + 2), ((ppc_result_d >> 8) & 0xFF));
//mem_write_byte((ppc_effective_address + 2), ((ppc_result_d >> 8) & 0xFF));
mmu_write_vmem<uint16_t>(ppc_effective_address, ppc_state.gpr[reg_s] >> 16);
mmu_write_vmem<uint8_t>(ppc_effective_address + 2, (ppc_state.gpr[reg_s] >> 8) & 0xFF);
grab_inb = 0;
break;
default:
mmu_write_vmem<uint32_t>(ppc_effective_address, ppc_result_d);
//mem_write_dword(ppc_effective_address, ppc_result_d);
mmu_write_vmem<uint32_t>(ppc_effective_address, ppc_state.gpr[reg_s]);
reg_s++;
if (reg_s >= 32) { // wrap around through GPR0
reg_s = 0;
}
ppc_effective_address += 4;
grab_inb -= 4;
}
+17 -13
View File
@@ -112,9 +112,9 @@ def gen_ppc_opcode(opc_str, imm):
elif opc_str == "FADDS.":
return (0x3B << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (0x15 << 1) + 1
elif opc_str == "FCMPO":
return (0x3B << 26) + (3 << 16) + (4 << 11) + (0x20 << 1)
return (0x3F << 26)+ (4 << 21) + (3 << 16) + (4 << 11) + (0x20 << 1)
elif opc_str == "FCMPU":
return (0x3B << 26) + (3 << 16) + (4 << 11)
return (0x3F << 26)+ (4 << 21) + (3 << 16) + (4 << 11)
elif opc_str == "FCTIW":
return (0x3B << 26) + (3 << 16) + (4 << 11) + (0xE << 1)
elif opc_str == "FCTIW.":
@@ -140,19 +140,21 @@ def gen_ppc_opcode(opc_str, imm):
elif opc_str == "FMADDS.":
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (5 << 6) + (0x1D << 1) + 1
elif opc_str == "FMUL":
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 6) + (0x19 << 1)
return (0x3F << 26) + (3 << 21) + (3 << 16) + (5 << 6) + (0x19 << 1)
elif opc_str == "FMUL.":
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 6) + (0x19 << 1) + 1
return (0x3F << 26) + (3 << 21) + (3 << 16) + (5 << 6) + (0x19 << 1) + 1
elif opc_str == "FMULS":
return (0x3B << 26) + (3 << 21) + (3 << 16) + (4 << 6) + (0x19 << 1)
return (0x3B << 26) + (3 << 21) + (3 << 16) + (5 << 6) + (0x19 << 1)
elif opc_str == "FMULS.":
return (0x3B << 26) + (3 << 21) + (3 << 16) + (4 << 6) + (0x19 << 1) + 1
return (0x3B << 26) + (3 << 21) + (3 << 16) + (5 << 6) + (0x19 << 1) + 1
elif opc_str == "FNABS":
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 6) + (0x88 << 1)
return (0x3F << 26) + (3 << 21) + (4 << 11) + (0x88 << 1)
elif opc_str == "FNABS.":
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 6) + (0x88 << 1) + 1
return (0x3F << 26) + (3 << 21) + (4 << 11) + (0x88 << 1) + 1
elif opc_str == "FNEG":
return (0x3B << 26) + (3 << 21) + (3 << 16) + (4 << 6) + (0x28 << 1)
return (0x3F << 26) + (3 << 21) + (4 << 11) + (0x28 << 1)
elif opc_str == "FNEG.":
return (0x3F << 26) + (3 << 21) + (4 << 11) + (0x28 << 1) + 1
elif opc_str == "FMULS.":
return (0x3B << 26) + (3 << 21) + (3 << 16) + (4 << 6) + (0x28 << 1) + 1
elif opc_str == "FMSUB":
@@ -179,14 +181,16 @@ def gen_ppc_opcode(opc_str, imm):
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (5 << 6) + (0x1C << 1)
elif opc_str == "FNMSUBS.":
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (5 << 6) + (0x1C << 1) + 1
elif opc_str == "FRES":
return (0x3B << 26) + (3 << 21) + (4 << 11) + (0x15 << 1)
elif opc_str == "FSUB":
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (0x15 << 1)
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (0x14 << 1)
elif opc_str == "FSUB.":
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (0x15 << 1) + 1
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (0x14 << 1) + 1
elif opc_str == "FSUBS":
return (0x3B << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (0x15 << 1)
return (0x3B << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (0x14 << 1)
elif opc_str == "FSUBS.":
return (0x3B << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (0x15 << 1) + 1
return (0x3B << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (0x14 << 1) + 1
elif opc_str == "MULHW":
return (0x1F << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (0x4B << 1)
elif opc_str == "MULHW.":
+25 -1
View File
@@ -2027,4 +2027,28 @@ FSUBS. (RTN) :: frD 0x7FF8000000000000 | frA -inf | frB -inf | FPSCR: 0xA0811
FSUBS. (RTZ) :: frD 0x7FF8000000000000 | frA -inf | frB -inf | FPSCR: 0xA0811001 | CR: 0x4A000002
FSUBS. (RTPI) :: frD 0x7FF8000000000000 | frA -inf | frB -inf | FPSCR: 0xA0811002 | CR: 0x4A000002
FSUBS. (RTNI) :: frD 0x7FF8000000000000 | frA -inf | frB -inf | FPSCR: 0xA0811003 | CR: 0x4A000002
FSUBS. (VE) :: frD 0x0000000000000000 | frA -inf | frB -inf | FPSCR: 0xE0800080 | CR: 0x0E000000
FSUBS. (VE) :: frD 0x0000000000000000 | frA -inf | frB -inf | FPSCR: 0xE0800080 | CR: 0x0E000000
FCMPO :: frA 0.000000e+00 | frB 0.000000e+00 | FPSCR: 0x00002000 | CR: 0x02000000
FCMPO :: frA 0.000000e+00 | frB 1.000000e+00 | FPSCR: 0x00008000 | CR: 0x08000000
FCMPO :: frA 1.000000e+00 | frB 0.000000e+00 | FPSCR: 0x00004000 | CR: 0x04000000
FCMPO :: frA 5.000000e-01 | frB 5.000000e-01 | FPSCR: 0x00002000 | CR: 0x02000000
FCMPO :: frA 0.000000e+00 | frB 5.000000e-01 | FPSCR: 0x00008000 | CR: 0x08000000
FCMPO :: frA 5.000000e-01 | frB 0.000000e+00 | FPSCR: 0x00004000 | CR: 0x04000000
FCMPO :: frA nan | frB nan | FPSCR: 0xA0081000 | CR: 0x01000000
FCMPO :: frA nan | frB nan | FPSCR: 0xA1081000 | CR: 0x01000000
FCMPO :: frA inf | frB inf | FPSCR: 0x00002000 | CR: 0x02000000
FCMPO :: frA inf | frB -inf | FPSCR: 0x00004000 | CR: 0x04000000
FCMPO :: frA -inf | frB inf | FPSCR: 0x00008000 | CR: 0x08000000
FCMPO :: frA -inf | frB -inf | FPSCR: 0x00002000 | CR: 0x02000000
FCMPU :: frA 0.000000e+00 | frB 0.000000e+00 | FPSCR: 0x00002000 | CR: 0x02000000
FCMPU :: frA 0.000000e+00 | frB 1.000000e+00 | FPSCR: 0x00008000 | CR: 0x08000000
FCMPU :: frA 1.000000e+00 | frB 0.000000e+00 | FPSCR: 0x00004000 | CR: 0x04000000
FCMPU :: frA 5.000000e-01 | frB 5.000000e-01 | FPSCR: 0x00002000 | CR: 0x02000000
FCMPU :: frA 0.000000e+00 | frB 5.000000e-01 | FPSCR: 0x00008000 | CR: 0x08000000
FCMPU :: frA 5.000000e-01 | frB 0.000000e+00 | FPSCR: 0x00004000 | CR: 0x04000000
FCMPU :: frA nan | frB nan | FPSCR: 0x00001000 | CR: 0x01000000
FCMPU :: frA nan | frB nan | FPSCR: 0xA1001000 | CR: 0x01000000
FCMPU :: frA inf | frB inf | FPSCR: 0x00002000 | CR: 0x02000000
FCMPU :: frA inf | frB -inf | FPSCR: 0x00004000 | CR: 0x04000000
FCMPU :: frA -inf | frB inf | FPSCR: 0x00008000 | CR: 0x08000000
FCMPU :: frA -inf | frB -inf | FPSCR: 0x00002000 | CR: 0x02000000
File diff suppressed because it is too large. Load diff
+26 -2
View File
@@ -1,3 +1,24 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-21 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "../ppcdisasm.h"
#include "../ppcemu.h"
#include <cmath>
@@ -208,7 +229,7 @@ static void read_test_float_data() {
dfp_src3 = stod(tokens[i].substr(4), NULL);
} else if (tokens[i].rfind("round=", 0) == 0) {
rounding_mode = tokens[i].substr(6, 3);
ppc_state.fpscr &= 0xFFFFFFFC;
ppc_state.fpscr = 0;
if (rounding_mode.compare("RTN") == 0) {
ppc_state.fpscr = 0x0;
} else if (rounding_mode.compare("RTZ") == 0) {
@@ -242,7 +263,7 @@ static void read_test_float_data() {
ppc_state.fpr[4].dbl64_r = dfp_src2;
ppc_state.fpr[5].dbl64_r = dfp_src3;
ppc_state.cr = 0;
ppc_state.cr = 0;
ppc_cur_instruction = opcode;
@@ -282,6 +303,9 @@ int main() {
read_test_data();
cout << endl << "Float IEEE suport: " << (bool)std::numeric_limits<float>::is_iec559 << endl;
cout << endl << "Double IEEE suport: " << (bool)std::numeric_limits<double>::is_iec559 << endl;
cout << endl << "Testing floating point instructions:" << endl;
read_test_float_data();
+21
View File
@@ -1,3 +1,24 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-21 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "../ppcdisasm.h"
#include <fstream>
#include <iomanip>
+161 -36
View File
@@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@@ -19,20 +19,30 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <cpu/ppc/ppcdisasm.h>
#include <cpu/ppc/ppcemu.h>
#include <cpu/ppc/ppcmmu.h>
#include <devices/common/hwinterrupt.h>
#include <devices/common/ofnvram.h>
#include "memaccess.h"
#include <utils/profiler.h>
#include <array>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <loguru.hpp>
#include <map>
#include <memory>
#include <sstream>
#include <stdio.h>
#include <string>
#include "../cpu/ppc/ppcdisasm.h"
#include "../cpu/ppc/ppcemu.h"
#include "../cpu/ppc/ppcmmu.h"
#include "memaccess.h"
#include "utils/profiler.h"
#ifdef DEBUG_CPU_INT
#include <machines/machinebase.h>
#include <devices/common/viacuda.h>
#endif
#ifdef ENABLE_68K_DEBUGGER // optionally defined in CMakeLists.txt
#include <capstone/capstone.h>
@@ -65,6 +75,7 @@ static void show_help() {
cout << " as single instructions." << endl;
cout << " ni -- shortcut for next" << endl;
cout << " until X -- execute until address X is reached" << endl;
cout << " go -- exit debugger and continue emulator execution" << endl;
cout << " regs -- dump content of the GRPs" << endl;
cout << " set R=X -- assign value X to register R" << endl;
cout << " if R=loglevel, set the internal" << endl;
@@ -88,6 +99,8 @@ static void show_help() {
cout << " X can be either 'ppc' (default) or '68k'" << endl;
cout << " Use 68k for debugging emulated 68k code only." << endl;
#endif
cout << " printenv -- print current NVRAM settings." << endl;
cout << " setenv V N -- set NVRAM variable V to value N." << endl;
cout << " quit -- quit the debugger" << endl << endl;
cout << "Pressing ENTER will repeat last command." << endl;
}
@@ -117,13 +130,23 @@ static void disasm_68k(uint32_t count, uint32_t address) {
code_size = sizeof(code);
dis_addr = address;
// catch and handle F-Traps (Nanokernel calls) ourselves because
// Capstone will likely return no meaningful assembly for them
if ((code[0] & 0xF0) == 0xF0) {
goto print_bin;
}
if (cs_disasm_iter(cs_handle, &code_ptr, &code_size, &dis_addr, insn)) {
cout << uppercase << hex << insn->address << " ";
cout << setfill(' ');
cout << setw(10) << left << insn->mnemonic << insn->op_str << endl;
address = dis_addr;
} else {
cout << "DS.W " << hex << ((code[0] << 8) | code[1]) << endl;
print_bin:
cout << uppercase << hex << address << " ";
cout << setfill(' ');
cout << setw(10) << left << "dc.w" << "$" << hex <<
((code[0] << 8) | code[1]) << endl;
address += 2;
}
}
@@ -138,25 +161,21 @@ static void disasm_68k(uint32_t count, uint32_t address) {
/** Execute one emulated 68k instruction. */
void exec_single_68k()
{
string reg;
uint32_t emu_table_virt, cur_68k_pc, cur_instr_tab_entry, ppc_pc;
/* PPC r24 contains 68k PC advanced by two bytes
as part of instruction prefetching */
reg = "R24";
cur_68k_pc = get_reg(reg) - 2;
cur_68k_pc = get_reg(string("R24")) - 2;
/* PPC r29 contains base address of the emulator opcode table */
reg = "R29";
emu_table_virt = get_reg(reg) & 0xFFF80000;
emu_table_virt = get_reg(string("R29")) & 0xFFF80000;
/* calculate address of the current opcode table entry as follows:
get_word(68k_PC) * entry_size + table_base */
cur_instr_tab_entry = mmu_read_vmem<uint16_t>(cur_68k_pc) * 8 + emu_table_virt;
/* grab the PPC PC too */
reg = "PC";
ppc_pc = get_reg(reg);
ppc_pc = get_reg(string("PC"));
//printf("cur_instr_tab_entry = %X\n", cur_instr_tab_entry);
@@ -165,39 +184,29 @@ void exec_single_68k()
one by one until the execution goes outside the opcode table. */
while (ppc_pc >= cur_instr_tab_entry && ppc_pc < cur_instr_tab_entry + 8) {
ppc_exec_single();
reg = "PC";
ppc_pc = get_reg(reg);
LOG_F(9, "Tracing within emulator table, PC = %X\n", ppc_pc);
ppc_pc = get_reg(string("PC"));
}
/* Getting here means we're outside the emualtor opcode table.
Execute PPC code until we hit the opcode table again. */
LOG_F(9, "Tracing outside the emulator table, PC = %X\n", ppc_pc);
ppc_exec_dbg(emu_table_virt, EMU_68K_TABLE_SIZE - 1);
}
/** Execute emulated 68k code until target_addr is reached. */
void exec_until_68k(uint32_t target_addr)
{
string reg;
uint32_t emu_table_virt, ppc_pc;
reg = "R29";
emu_table_virt = get_reg(reg) & 0xFFF80000;
emu_table_virt = get_reg(string("R29")) & 0xFFF80000;
reg = "R24";
while (target_addr != (get_reg(reg) - 2)) {
reg = "PC";
ppc_pc = get_reg(reg);
while (target_addr != (get_reg(string("R24")) - 2)) {
ppc_pc = get_reg(string("PC"));
if (ppc_pc >= emu_table_virt && ppc_pc < (emu_table_virt + EMU_68K_TABLE_SIZE - 1)) {
LOG_F(9, "Tracing within emulator table, PC = %X\n", ppc_pc);
ppc_exec_single();
} else {
LOG_F(9, "Tracing outside the emulator table, PC = %X\n", ppc_pc);
ppc_exec_dbg(emu_table_virt, EMU_68K_TABLE_SIZE - 1);
}
reg = "R24";
}
}
@@ -215,17 +224,14 @@ void print_68k_regs()
reg = "R" + to_string(i + 16);
cout << "A" << dec << i << " : " << uppercase << hex << get_reg(reg) << endl;
}
reg = "R1";
cout << "A7 : " << uppercase << hex << get_reg(reg) << endl;
reg = "R24";
cout << "PC: " << uppercase << hex << get_reg(reg) - 2 << endl;
cout << "A7 : " << uppercase << hex << get_reg(string("R1")) << endl;
reg = "R25";
cout << "SR: " << uppercase << hex << ((get_reg(reg) & 0xFF) << 8) << endl;
cout << "PC: " << uppercase << hex << get_reg(string("R24")) - 2 << endl;
reg = "R26";
cout << "CCR: " << uppercase << hex << get_reg(reg) << endl;
cout << "SR: " << uppercase << hex << ((get_reg("R25") & 0xFF) << 8) << endl;
cout << "CCR: " << uppercase << hex << get_reg(string("R26")) << endl;
}
#endif // ENABLE_68K_DEBUGGER
@@ -370,6 +376,32 @@ static void print_gprs() {
}
}
#ifndef _WIN32
#include <signal.h>
#include <termios.h>
#include <unistd.h>
static struct sigaction old_act_sigint, new_act_sigint;
static struct sigaction old_act_sigterm, new_act_sigterm;
static struct termios orig_termios;
static void mysig_handler(int signum)
{
// restore original terminal state
tcsetattr(STDIN_FILENO, TCSANOW, &orig_termios);
// restore original signal handler for SIGINT
signal(SIGINT, old_act_sigint.sa_handler);
signal(SIGTERM, old_act_sigterm.sa_handler);
LOG_F(INFO, "Old terminal state restored, SIG#=%d", signum);
// re-post signal
raise(signum);
}
#endif
void enter_debugger() {
string inp, cmd, addr_str, expr_str, reg_expr, last_cmd, reg_value_str,
inst_string, inst_num_str, profile_name, sub_cmd;
@@ -378,6 +410,8 @@ void enter_debugger() {
int log_level, context;
size_t separator_pos;
unique_ptr<OfNvramUtils> ofnvram = unique_ptr<OfNvramUtils>(new OfNvramUtils);
context = 1; /* start with the PowerPC context */
cout << "Welcome to the DingusPPC command line debugger." << endl;
@@ -494,6 +528,9 @@ void enter_debugger() {
} catch (invalid_argument& exc) {
cout << exc.what() << endl;
}
} else if (cmd == "go") {
power_on = true;
ppc_exec(); // won't return!
} else if (cmd == "disas" || cmd == "da") {
expr_str = "";
ss >> expr_str;
@@ -573,6 +610,94 @@ void enter_debugger() {
} else {
cout << "Unknown debugging context: " << expr_str << endl;
}
#endif
} else if (cmd == "printenv") {
if (ofnvram->init())
continue;
ofnvram->printenv();
} else if (cmd == "setenv") {
string var_name, value;
ss >> var_name;
ss >> value;
if (ofnvram->init())
continue;
ofnvram->setenv(var_name, value);
#ifndef _WIN32
} else if (cmd == "nvedit") {
cout << "===== press CNTRL-C to save =====" << endl;
// save original terminal state
tcgetattr(STDIN_FILENO, &orig_termios);
struct termios new_termios = orig_termios;
new_termios.c_cflag &= ~(CSIZE | PARENB);
new_termios.c_cflag |= CS8;
new_termios.c_lflag &= ~(ISIG | NOFLSH | ICANON | ECHOCTL);
new_termios.c_lflag |= NOFLSH | ECHONL;
// new_termios.c_iflag &= ~(ICRNL | IGNCR);
// new_termios.c_iflag |= INLCR;
// new_termios.c_oflag &= ~(ONOCR | ONLCR);
// new_termios.c_oflag |= OPOST | OCRNL | ONLRET;
tcsetattr(STDIN_FILENO, TCSANOW, &new_termios);
// save original signal handler for SIGINT
// then redirect SIGINT to new handler
memset(&new_act_sigint, 0, sizeof(new_act_sigint));
new_act_sigint.sa_handler = mysig_handler;
sigaction(SIGINT, &new_act_sigint, &old_act_sigint);
// save original signal handler for SIGTERM
// then redirect SIGTERM to new handler
memset(&new_act_sigterm, 0, sizeof(new_act_sigterm));
new_act_sigterm.sa_handler = mysig_handler;
sigaction(SIGTERM, &new_act_sigterm, &old_act_sigterm);
getline(cin, inp, '\x03');
// restore original terminal state
tcsetattr(STDIN_FILENO, TCSANOW, &orig_termios);
// restore original signal handler for SIGINT
signal(SIGINT, old_act_sigint.sa_handler);
// restore original signal handler for SIGTERM
signal(SIGTERM, old_act_sigterm.sa_handler);
if (ofnvram->init())
continue;
ofnvram->setenv("nvramrc", inp);
#endif
#ifdef DEBUG_CPU_INT
} else if (cmd == "amicint") {
string value;
int irq_id;
ss >> value;
try {
irq_id = str2num(value);
} catch (invalid_argument& exc) {
cout << exc.what() << endl;
continue;
}
InterruptCtrl* int_ctrl = dynamic_cast<InterruptCtrl*>(
gMachineObj->get_comp_by_type(HWCompType::INT_CTRL));
int_ctrl->ack_int(irq_id, 1);
} else if (cmd == "viaint") {
string value;
int irq_bit;
ss >> value;
try {
irq_bit = str2num(value);
} catch (invalid_argument& exc) {
cout << exc.what() << endl;
continue;
}
ViaCuda* via_obj = dynamic_cast<ViaCuda*>(gMachineObj->get_comp_by_name("ViaCuda"));
ppc_state.pc -= 4;
via_obj->assert_int(irq_bit);
#endif
} else {
cout << "Unknown command: " << cmd << endl;
+21
View File
@@ -1,3 +1,24 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-21 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef DEBUGGER_H_
#define DEBUGGER_H_
+14 -1
View File
@@ -2,7 +2,20 @@ include_directories("${PROJECT_SOURCE_DIR}"
"${PROJECT_SOURCE_DIR}/thirdparty/loguru/"
"${PROJECT_SOURCE_DIR}/thirdparty/SDL2/")
file(GLOB SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")
file(GLOB SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/common/*.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/common/adb/*.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/common/i2c/*.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/common/pci/*.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/common/scsi/*.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/ethernet/*.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/floppy/*.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/ioctrl/*.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/memctrl/*.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/serial/*.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/sound/*.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/video/*.cpp"
)
add_library(devices OBJECT ${SOURCES})
target_link_libraries(devices PRIVATE cubeb)
-277
View File
@@ -1,277 +0,0 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-21 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** Apple memory-mapped I/O controller emulation.
Author: Max Poliakovski
*/
#include "amic.h"
#include "cpu/ppc/ppcmmu.h"
#include "dmacore.h"
#include "machines/machinebase.h"
#include "memctrlbase.h"
#include "viacuda.h"
#include <algorithm>
#include <cinttypes>
#include <loguru.hpp>
#include <memory>
AMIC::AMIC()
{
this->name = "Apple Memory-mapped I/O Controller";
MemCtrlBase *mem_ctrl = dynamic_cast<MemCtrlBase *>
(gMachineObj->get_comp_by_type(HWCompType::MEM_CTRL));
/* add memory mapped I/O region for the AMIC control registers */
if (!mem_ctrl->add_mmio_region(0x50F00000, 0x00040000, this)) {
LOG_F(ERROR, "Couldn't register AMIC registers!");
}
this->viacuda = std::unique_ptr<ViaCuda> (new ViaCuda());
this->snd_out_dma = std::unique_ptr<AmicSndOutDma> (new AmicSndOutDma());
this->awacs = std::unique_ptr<AwacDevicePdm> (new AwacDevicePdm());
this->awacs->set_dma_out(this->snd_out_dma.get());
}
bool AMIC::supports_type(HWCompType type) {
if (type == HWCompType::MMIO_DEV) {
return true;
} else {
return false;
}
}
uint32_t AMIC::read(uint32_t reg_start, uint32_t offset, int size)
{
uint32_t phase_val;
if (offset < 0x2000) {
return this->viacuda->read(offset >> 9);
}
switch(offset) {
case AMICReg::Snd_Stat_0:
case AMICReg::Snd_Stat_1:
case AMICReg::Snd_Stat_2:
return (this->awacs->read_stat() >> (offset & 3 * 8)) & 0xFF;
case AMICReg::Snd_Phase0:
case AMICReg::Snd_Phase1:
case AMICReg::Snd_Phase2:
// the sound phase register is organized as follows:
// 000000oo oooooooo oopppppp where 'o' is the 12-bit offset
// into the DMA buffer and 'p' is an undocumented prescale value
// HWInit doesn't care about. Let's hope it will be sufficient
// to return 0 for prescale.
phase_val = this->snd_out_dma->get_cur_buf_pos() << 6;
return (phase_val >> ((2 - (offset & 3)) * 8)) & 0xFF;
case AMICReg::Snd_Out_Ctrl:
return this->snd_out_ctrl;
case AMICReg::Snd_Out_DMA:
return this->snd_out_dma->read_stat();
default:
LOG_F(WARNING, "Unknown AMIC register read, offset=%x", offset);
}
return 0;
}
void AMIC::write(uint32_t reg_start, uint32_t offset, uint32_t value, int size)
{
uint32_t mask;
if (offset < 0x2000) {
this->viacuda->write(offset >> 9, value);
return;
}
switch(offset) {
case AMICReg::Snd_Ctrl_0:
case AMICReg::Snd_Ctrl_1:
case AMICReg::Snd_Ctrl_2:
// remember values of sound control registers
this->imm_snd_regs[offset & 3] = value;
// transfer control information to the sound codec when ready
if ((this->imm_snd_regs[0] & 0xC0) == PDM_SND_CTRL_VALID) {
this->awacs->write_ctrl(
(this->imm_snd_regs[1] >> 4) | (this->imm_snd_regs[0] & 0x3F),
((this->imm_snd_regs[1] & 0xF) << 8) | this->imm_snd_regs[2]
);
}
break;
case AMICReg::Snd_Buf_Size_Hi:
case AMICReg::Snd_Buf_Size_Lo:
mask = 0xFF00U >> (8 * (offset & 1));
this->snd_buf_size = (this->snd_buf_size & ~mask) |
((value & 0xFF) << (8 * ((offset & 1) ^1)));
this->snd_buf_size &= ~3; // sound buffer size is always a multiple of 4
LOG_F(9, "AMIC: Sound buffer size set to 0x%X", this->snd_buf_size);
break;
case AMICReg::Snd_Out_Ctrl:
LOG_F(9, "AMIC Sound Out Ctrl updated, val=%x", value);
if ((value & 1) != (this->snd_out_ctrl & 1)) {
if (value & 1) {
LOG_F(9, "AMIC Sound Out DMA enabled!");
this->snd_out_dma->init(this->dma_base & ~0x3FFFF,
this->snd_buf_size);
this->snd_out_dma->enable();
this->awacs->set_sample_rate((this->snd_out_ctrl >> 1) & 3);
this->awacs->start_output_dma();
} else {
LOG_F(9, "AMIC Sound Out DMA disabled!");
this->snd_out_dma->disable();
}
}
this->snd_out_ctrl = value;
break;
case AMICReg::Snd_In_Ctrl:
LOG_F(INFO, "AMIC Sound In Ctrl updated, val=%x", value);
break;
case AMICReg::Snd_Out_DMA:
this->snd_out_dma->write_dma_out_ctrl(value);
break;
case AMICReg::VIA2_Slot_IER:
LOG_F(INFO, "AMIC VIA2 Slot Interrupt Enable Register updated, val=%x", value);
break;
case AMICReg::VIA2_IER:
LOG_F(INFO, "AMIC VIA2 Interrupt Enable Register updated, val=%x", value);
break;
case AMICReg::Video_Mode_Reg:
LOG_F(INFO, "AMIC Video Mode Register set to %x", value);
break;
case AMICReg::Int_Ctrl:
LOG_F(INFO, "AMIC Interrupt Control Register set to %X", value);
break;
case AMICReg::DMA_Base_Addr_0:
case AMICReg::DMA_Base_Addr_1:
case AMICReg::DMA_Base_Addr_2:
case AMICReg::DMA_Base_Addr_3:
mask = 0xFF000000UL >> (8 * (offset & 3));
this->dma_base = (this->dma_base & ~mask) |
((value & 0xFF) << (8 * (3 - (offset & 3))));
LOG_F(9, "AMIC: DMA base address set to 0x%X", this->dma_base);
break;
case AMICReg::Enet_DMA_Xmt_Ctrl:
LOG_F(INFO, "AMIC Ethernet Transmit DMA Ctrl updated, val=%x", value);
break;
case AMICReg::SCSI_DMA_Ctrl:
LOG_F(INFO, "AMIC SCSI DMA Ctrl updated, val=%x", value);
break;
case AMICReg::Enet_DMA_Rcv_Ctrl:
LOG_F(INFO, "AMIC Ethernet Receive DMA Ctrl updated, val=%x", value);
break;
case AMICReg::SWIM3_DMA_Ctrl:
LOG_F(INFO, "AMIC SWIM3 DMA Ctrl updated, val=%x", value);
break;
case AMICReg::SCC_DMA_Xmt_A_Ctrl:
LOG_F(INFO, "AMIC SCC Transmit Ch A DMA Ctrl updated, val=%x", value);
break;
case AMICReg::SCC_DMA_Rcv_A_Ctrl:
LOG_F(INFO, "AMIC SCC Receive Ch A DMA Ctrl updated, val=%x", value);
break;
case AMICReg::SCC_DMA_Xmt_B_Ctrl:
LOG_F(INFO, "AMIC SCC Transmit Ch B DMA Ctrl updated, val=%x", value);
break;
case AMICReg::SCC_DMA_Rcv_B_Ctrl:
LOG_F(INFO, "AMIC SCC Receive Ch B DMA Ctrl updated, val=%x", value);
break;
default:
LOG_F(WARNING, "Unknown AMIC register write, offset=%x, val=%x",
offset, value);
}
}
// =========================== DMA related stuff =============================
AmicSndOutDma::AmicSndOutDma()
{
this->dma_out_ctrl = 0;
this->enabled = false;
}
bool AmicSndOutDma::is_active()
{
return true;
}
void AmicSndOutDma::init(uint32_t buf_base, uint32_t buf_samples)
{
this->out_buf0 = buf_base + AMIC_SND_BUF0_OFFS;
this->out_buf1 = buf_base + AMIC_SND_BUF1_OFFS;
this->out_buf_len = buf_samples * 2 * 2;
this->snd_buf_num = 0;
this->cur_buf_pos = 0;
}
uint8_t AmicSndOutDma::read_stat()
{
return this->dma_out_ctrl;
}
void AmicSndOutDma::write_dma_out_ctrl(uint8_t value)
{
// clear interrupt flags
value &= ~PDM_DMA_INTS_MASK;
this->dma_out_ctrl = value;
LOG_F(9, "AMIC: Sound out DMA control set to 0x%X", value);
}
DmaPullResult AmicSndOutDma::pull_data(uint32_t req_len, uint32_t *avail_len,
uint8_t **p_data)
{
*avail_len = 0;
int rem_len = this->out_buf_len - this->cur_buf_pos;
if (rem_len <= 0) {
if (!this->snd_buf_num) {
// signal buffer 0 drained
this->dma_out_ctrl |= PDM_DMA_IF0;
// TODO: generate IE0 interrupt if enabled
} else {
// signal buffer 1 drained
this->dma_out_ctrl |= PDM_DMA_IF1;
// TODO: generate IE1 interrupt if enabled
}
// check DMA enable flag after buffer 1 was processed
// if it's false stop delivering sound data
// this will effectively stop audio playback
if (this->snd_buf_num && !this->enabled) {
this->cur_buf_pos = 0;
return DmaPullResult::NoMoreData;
}
this->cur_buf_pos = 0; // reset buffer position
this->snd_buf_num ^= 1; // toggle sound buffers
rem_len = this->out_buf_len; // buffer size = full buffer
}
uint32_t len = std::min((uint32_t)rem_len, req_len);
*p_data = mmu_get_dma_mem(
(this->snd_buf_num ? this->out_buf1 : this->out_buf0) + this->cur_buf_pos,
len);
this->cur_buf_pos += len;
*avail_len = len;
return DmaPullResult::MoreData;
}
@@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
Copyright (C) 2018-21 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@@ -25,7 +25,9 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "devices/adb.h"
#include <devices/common/adb/adb.h>
#include <loguru.hpp>
#include <cinttypes>
#include <cstring>
@@ -37,8 +39,6 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <thirdparty/SDL2/include/SDL_stdinc.h>
#endif
#include <loguru.hpp>
using namespace std;
ADB_Bus::ADB_Bus() {
@@ -55,8 +55,6 @@ ADB_Bus::ADB_Bus() {
mouse_access_no = adb_relative;
}
ADB_Bus::~ADB_Bus() {}
bool ADB_Bus::listen(int device, int reg) {
if (device == keyboard_access_no) {
if (adb_keybd_listen(reg)) {
+2 -2
View File
@@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
Copyright (C) 2018-21 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@@ -51,7 +51,7 @@ enum adb_default_values {
class ADB_Bus {
public:
ADB_Bus();
~ADB_Bus();
~ADB_Bus() = default;
bool listen(int device, int reg);
bool talk(int device, int reg, uint16_t value);
@@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
Copyright (C) 2018-21 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@@ -21,10 +21,11 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
/** @file Descriptor-based direct memory access emulation. */
#include "dmacore.h"
#include "dbdma.h"
#include "cpu/ppc/ppcmmu.h"
#include "endianswap.h"
#include <cpu/ppc/ppcmmu.h>
#include <devices/common/dbdma.h>
#include <devices/common/dmacore.h>
#include <endianswap.h>
#include <cinttypes>
#include <cstring>
#include <loguru.hpp>
+3 -2
View File
@@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
Copyright (C) 2018-21 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@@ -29,7 +29,8 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#ifndef DB_DMA_H
#define DB_DMA_H
#include "dmacore.h"
#include <devices/common/dmacore.h>
#include <cinttypes>
#include <functional>
+10 -1
View File
@@ -33,7 +33,16 @@ enum DmaPullResult : int {
class DmaOutChannel {
public:
virtual bool is_active() = 0;
virtual bool is_active() { return true; };
virtual DmaPullResult pull_data(uint32_t req_len, uint32_t *avail_len,
uint8_t **p_data) = 0;
};
// Base class for bidirectional DMA channels.
class DmaBidirChannel {
public:
virtual bool is_active() { return true; };
virtual int push_data(const char* src_ptr, int len) = 0;
virtual DmaPullResult pull_data(uint32_t req_len, uint32_t *avail_len,
uint8_t **p_data) = 0;
};
+81
View File
@@ -0,0 +1,81 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef HW_COMPONENT_H
#define HW_COMPONENT_H
#include <cinttypes>
#include <string>
/** types of different HW components */
enum HWCompType {
UNKNOWN = 0ULL, /* unknown component type */
MEM_CTRL = 1ULL << 0, /* memory controller */
NVRAM = 1ULL << 1, /* non-volatile random access memory */
ROM = 1ULL << 2, /* read-only memory */
RAM = 1ULL << 3, /* random access memory */
MMIO_DEV = 1ULL << 4, /* memory mapped I/O device */
PCI_HOST = 1ULL << 5, /* PCI host */
PCI_DEV = 1ULL << 6, /* PCI device */
I2C_HOST = 1ULL << 8, /* I2C host */
I2C_DEV = 1ULL << 9, /* I2C device */
ADB_HOST = 1ULL << 12, /* ADB host */
ADB_DEV = 1ULL << 13, /* ADB device */
INT_CTRL = 1ULL << 16, /* interrupt controller */
SCSI_BUS = 1ULL << 20, /* SCSI bus */
SCSI_HOST = 1ULL << 21, /* SCSI host adapter */
SCSI_DEV = 1ULL << 22, /* SCSI device */
SND_SERVER = 1ULL << 31, /* host sound server */
FLOPPY_CTRL = 1ULL << 32, /* floppy disk controller */
FLOPPY_DRV = 1ULL << 33, /* floppy disk drive */
};
/** Base class for HW components. */
class HWComponent {
public:
HWComponent() = default;
virtual ~HWComponent() = default;
virtual std::string get_name(void) {
return this->name;
};
virtual void set_name(std::string name) {
this->name = name;
};
virtual bool supports_type(HWCompType type) {
return !!(this->supported_types & type);
};
virtual void supports_types(uint64_t types) {
this->supported_types = types;
};
virtual int device_postinit() {
return 0;
};
protected:
std::string name;
uint64_t supported_types = HWCompType::UNKNOWN;
};
#endif /* HW_COMPONENT_H */
+54
View File
@@ -0,0 +1,54 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef HW_INTERRUPT_H
#define HW_INTERRUPT_H
#include <cinttypes>
//#define DEBUG_CPU_INT // uncomment this to enable hacks for debugging HW interrupts
/** Enumerator for various interrupt sources. */
enum IntSrc : int {
VIA_CUDA = 1,
SCSI1 = 2,
SWIM3 = 3,
SCC = 4,
ETHERNET = 5,
NMI = 6,
};
/** Base class for interrupt controllers. */
class InterruptCtrl {
public:
InterruptCtrl() = default;
virtual ~InterruptCtrl() = default;
// register interrupt sources for a device
virtual uint32_t register_dev_int(IntSrc src_id) = 0;
virtual uint32_t register_dma_int(IntSrc src_id) = 0;
// acknowledge HW interrupt
virtual void ack_int(uint32_t irq_id, uint8_t irq_line_state) = 0;
virtual void ack_dma_int(uint32_t irq_id, uint8_t irq_line_state) = 0;
};
#endif // HW_INTERRUPT_H
+142
View File
@@ -0,0 +1,142 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** @file Athens clock generator emulation. */
/**
Athens (Apple part# 343S1191) is a programmable clock generator ASIC
used in the PCI Power Macintosh computers.
It has two outputs: system clock and video aka dot clock. Both can be
programmed using HW jumpers or I2C interface.
This high-level emulator focuses on the Athen's I2C interface providing
methods for querying the resulting virtual frequences.
This is helpful especially for calculating video parameters.
*/
#include <devices/common/i2c/athens.h>
#include <loguru.hpp>
#include <algorithm>
#include <cinttypes>
#include <vector>
AthensClocks::AthensClocks(uint8_t dev_addr)
{
supports_types(HWCompType::I2C_DEV);
this->my_addr = dev_addr;
// set up power on values
this->regs[AthensRegs::P2_MUX2] = 0x62;
}
void AthensClocks::start_transaction()
{
this->pos = 0; // reset read/write position
}
bool AthensClocks::send_subaddress(uint8_t sub_addr)
{
LOG_F(INFO, "Athens: subaddress set to 0x%X", sub_addr);
return true;
}
bool AthensClocks::send_byte(uint8_t data)
{
switch (this->pos) {
case 0:
this->reg_num = data;
this->pos++;
break;
case 1:
if (this->reg_num >= ATHENS_NUM_REGS) {
LOG_F(WARNING, "Athens: invalid register number %d", this->reg_num);
return false; // return NACK
}
this->regs[this->reg_num] = data;
if (reg_num == 3) {
LOG_F(INFO, "Athens: dot clock frequency set to %d Hz", get_dot_freq());
}
break;
default:
LOG_F(WARNING, "Athens: too much data received!");
return false; // return NACK
}
return true;
}
bool AthensClocks::receive_byte(uint8_t* p_data)
{
*p_data = 0x41; // return my ID (value has been guessed)
return true;
}
int AthensClocks::get_sys_freq()
{
return 0;
}
int AthensClocks::get_dot_freq()
{
static std::vector<int> D2_commons = {7, 11, 13, 14, 15, 17, 23, 31};
static std::vector<int> N2_commons = {
22, 27, 28, 31, 35, 37, 38, 42, 49, 55, 56, 78, 125
};
if (this->regs[AthensRegs::P2_MUX2] & 0xC0) {
LOG_F(INFO, "Athens: dot clock disabled");
return 0;
}
float out_freq = 0.0f;
int d2 = this->regs[AthensRegs::D2];
int n2 = this->regs[AthensRegs::N2];
int post_div = 1 << (3 - (this->regs[AthensRegs::P2_MUX2] & 3));
if (std::find(D2_commons.begin(), D2_commons.end(), d2) == D2_commons.end()) {
LOG_F(WARNING, "Athens: untested D2 value %d", d2);
}
if (std::find(N2_commons.begin(), N2_commons.end(), n2) == N2_commons.end()) {
LOG_F(WARNING, "Athens: untested N2 value %d", d2);
}
int mux = (this->regs[AthensRegs::P2_MUX2] >> 4) & 3;
switch (mux) {
case 0: // clock source -> dot cock VCO
out_freq = ATHENS_XTAL * ((float)n2 / (float)(d2 * post_div));
break;
case 1: // clock source -> system clock VCO
LOG_F(WARNING, "Athens: system clock VCO not supported yet!");
break;
case 2: // clock source -> crystal frequency
out_freq = ATHENS_XTAL / post_div;
break;
case 3:
LOG_F(WARNING, "Athens: attempt to use reserved Mux value!");
break;
}
return static_cast<int>(out_freq + 0.5f);
}
+75
View File
@@ -0,0 +1,75 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** @file Athens clock generator definitions. */
#ifndef ATHENS_H
#define ATHENS_H
#include <devices/common/hwcomponent.h>
#include <devices/common/i2c/i2c.h>
#include <cinttypes>
#define ATHENS_NUM_REGS 8
constexpr auto ATHENS_XTAL = 31334400.0f; // external crystal oscillator frequency
namespace AthensRegs {
enum AthensRegs: uint8_t {
ID = 0,
D2 = 1,
N2 = 2,
P2_MUX2 = 3,
VN_CTRL = 4, // vendor specific control bits
BD1 = 5,
BN1 = 6,
P1 = 7
};
}; // namespace AthensRegs
class AthensClocks : public I2CDevice, public HWComponent
{
public:
AthensClocks(uint8_t dev_addr);
~AthensClocks() = default;
// I2CDevice methods
void start_transaction();
bool send_subaddress(uint8_t sub_addr);
bool send_byte(uint8_t data);
bool receive_byte(uint8_t* p_data);
// methods for querying virtual frequences
int get_sys_freq();
int get_dot_freq();
private:
uint8_t my_addr = 0;
uint8_t reg_num = 0;
int pos = 0;
uint8_t regs[ATHENS_NUM_REGS];
};
#endif // ATHENS_H
+2 -4
View File
@@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
Copyright (C) 2018-21 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@@ -47,9 +47,7 @@ public:
I2CBus() {
std::memset(this->dev_list, 0, sizeof(this->dev_list));
};
~I2CBus() {
std::memset(this->dev_list, 0, sizeof(this->dev_list));
};
virtual ~I2CBus() = default;
virtual void register_device(uint8_t dev_addr, I2CDevice* dev_obj) {
if (this->dev_list[dev_addr]) {
+76
View File
@@ -0,0 +1,76 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** @file Generic PROM device programmable over I2C. */
#include <devices/common/i2c/i2cprom.h>
#include <loguru.hpp>
#include <cinttypes>
#include <cstring>
#include <memory>
I2CProm::I2CProm(uint8_t dev_addr, int size)
{
supports_types(HWCompType::I2C_DEV);
this->my_addr = dev_addr;
this->rom_size = size;
// allocate storage for ROM data
this->data = std::unique_ptr<uint8_t[]> (new uint8_t[this->rom_size]);
}
void I2CProm::fill_memory(int start, int size, uint8_t c)
{
if ((start + size) <= this->rom_size) {
std::memset(&this->data[start], c, size);
}
}
void I2CProm::set_memory(int start, const uint8_t* in_data, int size)
{
if ((start + size) <= this->rom_size) {
std::memcpy(&this->data[start], in_data, size);
}
}
void I2CProm::start_transaction() {
this->pos = 0;
};
bool I2CProm::send_subaddress(uint8_t sub_addr) {
this->pos = sub_addr;
return true;
};
bool I2CProm::send_byte(uint8_t data) {
LOG_F(9, "I2CRom: 0x%X received", data);
return true;
};
bool I2CProm::receive_byte(uint8_t* p_data) {
if (this->pos >= this->rom_size) {
this->pos = 0; // attempt to read past last byte should wrap around
}
*p_data = this->data[this->pos++];
return true;
};
+57
View File
@@ -0,0 +1,57 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** @file Generic PROM device programmable over I2C. */
#ifndef I2C_PROM_H
#define I2C_PROM_H
#include <devices/common/hwcomponent.h>
#include <devices/common/i2c/i2c.h>
#include <cinttypes>
#include <memory>
class I2CProm : public I2CDevice, public HWComponent
{
public:
I2CProm(uint8_t dev_addr, int size);
~I2CProm() = default;
// I2CDevice methods
void start_transaction();
bool send_subaddress(uint8_t sub_addr);
bool send_byte(uint8_t data);
bool receive_byte(uint8_t* p_data);
// data management methods
void fill_memory(int start, int size, uint8_t c);
void set_memory(int start, const uint8_t* in_data, int size);
private:
std::unique_ptr<uint8_t[]> data;
int rom_size = 0;
int pos = 0;
uint8_t my_addr = 0xA0;
};
#endif // I2C_PROM_H
@@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
Copyright (C) 2018-21 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@@ -22,8 +22,9 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#ifndef MACHINE_ID_H
#define MACHINE_ID_H
#include "hwcomponent.h"
#include "mmiodevice.h"
#include <devices/common/hwcomponent.h>
#include <devices/common/mmiodevice.h>
#include <cinttypes>
/**
@@ -50,19 +51,16 @@ public:
this->id[1] = 0x5A;
this->id[2] = (id >> 8) & 0xFF;
this->id[3] = id & 0xFF;
supports_types(HWCompType::MMIO_DEV);
};
~NubusMacID() = default;
bool supports_type(HWCompType type) {
return type == HWCompType::MMIO_DEV;
};
uint32_t read(uint32_t reg_start, uint32_t offset, int size) {
uint32_t read(uint32_t rgn_start, uint32_t offset, int size) {
return (offset < 4 ? this->id[offset] : 0);
};
/* not writable */
void write(uint32_t reg_start, uint32_t offset, uint32_t value, int size) {};
void write(uint32_t rgn_start, uint32_t offset, uint32_t value, int size) {};
private:
uint8_t id[4];
@@ -72,26 +70,22 @@ private:
The machine ID for the Gossamer board is accesible at 0xFF000004 (phys).
It contains a 16-bit value revealing machine's capabilities like bus speed,
ROM speed, I/O configuration etc.
Because the meaning of these bits is poorly documented, the code below
simply return a raw value obtained from real hardware.
See machines/machinegossamer.cpp for further details.
*/
class GossamerID : public MMIODevice {
public:
GossamerID(const uint16_t id) {
this->id = id, this->name = "Machine-id";
supports_types(HWCompType::MMIO_DEV);
};
~GossamerID() = default;
bool supports_type(HWCompType type) {
return type == HWCompType::MMIO_DEV;
};
uint32_t read(uint32_t reg_start, uint32_t offset, int size) {
uint32_t read(uint32_t rgn_start, uint32_t offset, int size) {
return ((!offset && size == 2) ? this->id : 0);
};
/* not writable */
void write(uint32_t reg_start, uint32_t offset, uint32_t value, int size) {};
void write(uint32_t rgn_start, uint32_t offset, uint32_t value, int size) {};
private:
uint16_t id;
@@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
Copyright (C) 2018-21 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@@ -22,15 +22,17 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#ifndef MMIO_DEVICE_H
#define MMIO_DEVICE_H
#include "hwcomponent.h"
#include <devices/common/hwcomponent.h>
#include <cinttypes>
#include <string>
/** Abstract class representing a simple, memory-mapped I/O device */
class MMIODevice : public HWComponent {
public:
virtual uint32_t read(uint32_t reg_start, uint32_t offset, int size) = 0;
virtual void write(uint32_t reg_start, uint32_t offset, uint32_t value, int size) = 0;
MMIODevice() = default;
virtual uint32_t read(uint32_t rgn_start, uint32_t offset, int size) = 0;
virtual void write(uint32_t rgn_start, uint32_t offset, uint32_t value, int size) = 0;
virtual ~MMIODevice() = default;
};
+18 -4
View File
@@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
Copyright (C) 2018-21 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@@ -19,7 +19,10 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "nvram.h"
#include <devices/common/hwcomponent.h>
#include <devices/common/nvram.h>
#include <devices/deviceregistry.h>
#include <cinttypes>
#include <cstring>
#include <fstream>
@@ -34,7 +37,12 @@ using namespace std;
/** the signature for NVRAM backing file identification. */
static char NVRAM_FILE_ID[] = "DINGUSPPCNVRAM";
NVram::NVram(std::string file_name, uint32_t ram_size) {
NVram::NVram(std::string file_name, uint32_t ram_size)
{
this->name = "NVRAM";
supports_types(HWCompType::NVRAM);
this->file_name = file_name;
this->ram_size = ram_size;
@@ -67,7 +75,7 @@ void NVram::init() {
!f.read((char*)&data_size, sizeof(data_size)) ||
memcmp(sig, NVRAM_FILE_ID, sizeof(NVRAM_FILE_ID)) || data_size != this->ram_size ||
!f.read((char*)this->storage, this->ram_size)) {
LOG_F(WARNING, "Could not restore NVRAM content from the given file. \n");
LOG_F(WARNING, "Could not restore NVRAM content from the given file.");
memset(this->storage, 0, sizeof(this->ram_size));
}
@@ -86,3 +94,9 @@ void NVram::save() {
f.close();
}
static const DeviceDescription Nvram_Descriptor = {
NVram::create, {}, {}
};
REGISTER_DEVICE(NVRAM, Nvram_Descriptor);
+12 -5
View File
@@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
Copyright (C) 2018-21 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@@ -22,7 +22,10 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#ifndef NVRAM_H
#define NVRAM_H
#include <devices/common/hwcomponent.h>
#include <cinttypes>
#include <memory>
#include <string>
/** @file Non-volatile RAM emulation.
@@ -31,18 +34,22 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
automatically saved to and restored from the dedicated file.
*/
class NVram {
class NVram : public HWComponent {
public:
NVram(std::string file_name = "nvram.bin", uint32_t ram_size = 8192);
~NVram();
static std::unique_ptr<HWComponent> create() {
return std::unique_ptr<NVram>(new NVram());
}
uint8_t read_byte(uint32_t offset);
void write_byte(uint32_t offset, uint8_t value);
private:
std::string file_name; /* file name for the backing file. */
uint16_t ram_size; /* NVRAM size. */
uint8_t* storage;
std::string file_name; // file name for the backing file
uint16_t ram_size; // NVRAM size
uint8_t* storage;
void init();
void save();
+305
View File
@@ -0,0 +1,305 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** @file Utilities for working with the Apple Open Firmware NVRAM partition. */
#include <devices/common/ofnvram.h>
#include <endianswap.h>
#include <loguru.hpp>
#include <machines/machinebase.h>
#include <memaccess.h>
#include <cinttypes>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <tuple>
using namespace std;
static uint32_t str2env(string& num_str) {
try {
return stoul(num_str, NULL, 0);
} catch (invalid_argument& exc) {
try {
string num_str2 = string("0x") + num_str;
return std::stoul(num_str2, NULL, 0);
} catch (invalid_argument& exc) {
throw invalid_argument(string("Cannot convert ") + num_str);
}
}
}
int OfNvramUtils::init()
{
this->nvram_obj = dynamic_cast<NVram*>(gMachineObj->get_comp_by_name("NVRAM"));
return this->nvram_obj == nullptr;
}
bool OfNvramUtils::validate()
{
int i;
OfNvramHdr hdr;
if (this->nvram_obj == nullptr)
return false;
// read OF partition header
for (i = 0; i < sizeof(OfNvramHdr); i++) {
((uint8_t*)&hdr)[i] = this->nvram_obj->read_byte(OF_NVRAM_OFFSET + i);
}
// validate partition signature and version
if (BYTESWAP_16(hdr.sig) != OF_NVRAM_SIG || hdr.version != 5)
return false;
this->size = hdr.num_pages * 256;
if (this->size != 0x800)
return false;
// read the entire partition into the local buffer
for (i = 0; i < this->size; i++) {
this->buf[i] = this->nvram_obj->read_byte(OF_NVRAM_OFFSET + i);
}
// verify partition checksum
if (this->checksum_partition() ^ 0xFFFFU)
return false;
return true;
}
uint16_t OfNvramUtils::checksum_partition()
{
uint32_t acc = 0;
for (int i = 0; i < this->size; i += 2) {
acc += READ_WORD_BE_A(&this->buf[i]);
}
return acc + (acc >> 16);
}
void OfNvramUtils::update_partition()
{
// set checksum in the header to zero
this->buf[4] = 0;
this->buf[5] = 0;
// calculate new checksum
uint16_t checksum = this->checksum_partition();
checksum = checksum ? ~checksum : 0xFFFFU;
// stuff it into the header
WRITE_WORD_BE_A(&this->buf[4], checksum);
// write the entire partition back to NVRAM
for (int i = 0; i < this->size; i++) {
this->nvram_obj->write_byte(OF_NVRAM_OFFSET + i, this->buf[i]);
}
}
static const string flag_names[8] = {
"little-endian?",
"real-mode?",
"auto-boot?",
"diag-switch?",
"fcode-debug?",
"oem-banner?",
"oem-logo?",
"use-nvramrc?"
};
static const map<string, std::tuple<int, uint16_t>> of_vars = {
// name, type, offset
{"real-base", {OF_VAR_TYPE_INT, 0x10}},
{"real-size", {OF_VAR_TYPE_INT, 0x14}},
{"virt-base", {OF_VAR_TYPE_INT, 0x18}},
{"virt-size", {OF_VAR_TYPE_INT, 0x1C}},
{"load-base", {OF_VAR_TYPE_INT, 0x20}},
{"pci-probe-list", {OF_VAR_TYPE_INT, 0x24}},
{"screen-#columns", {OF_VAR_TYPE_INT, 0x28}},
{"screen-#rows", {OF_VAR_TYPE_INT, 0x2C}},
{"selftest-#megs", {OF_VAR_TYPE_INT, 0x30}},
{"boot-device", {OF_VAR_TYPE_STR, 0x34}},
{"boot-file", {OF_VAR_TYPE_STR, 0x38}},
{"diag-device", {OF_VAR_TYPE_STR, 0x3C}},
{"diag-file", {OF_VAR_TYPE_STR, 0x40}},
{"input-device", {OF_VAR_TYPE_STR, 0x44}},
{"output-device", {OF_VAR_TYPE_STR, 0x48}},
{"oem-banner", {OF_VAR_TYPE_STR, 0x4C}},
{"oem-logo", {OF_VAR_TYPE_STR, 0x50}},
{"nvramrc", {OF_VAR_TYPE_STR, 0x54}},
{"boot-command", {OF_VAR_TYPE_STR, 0x58}},
};
void OfNvramUtils::printenv()
{
int i;
if (!this->validate()) {
cout << "Invalid Open Firmware partition content!" << endl;
return;
}
uint8_t of_flags = this->buf[12];
cout << endl;
// print flags
for (i = 0; i < 8; i++) {
cout << setw(20) << left << flag_names[i] <<
(((of_flags << i) & 0x80) ? "true" : "false") << endl;
}
// print the remaining variables
for (auto& var : of_vars) {
auto type = std::get<0>(var.second);
auto offset = std::get<1>(var.second);
cout << setw(20) << left << var.first;
switch (type) {
case OF_VAR_TYPE_INT:
cout << hex << READ_DWORD_BE_A(&this->buf[offset]) << endl;
break;
case OF_VAR_TYPE_STR:
uint16_t str_offset = READ_WORD_BE_A(&this->buf[offset]) - OF_NVRAM_OFFSET;
uint16_t str_len = READ_WORD_BE_A(&this->buf[offset+2]);
for (i = 0; i < str_len; i++) {
char ch = *(char *)&(this->buf[str_offset + i]);
if (ch == '\x0D') cout << endl; else cout << ch;
}
cout << endl;
}
}
cout << endl;
}
void OfNvramUtils::setenv(string var_name, string value)
{
int i, flag;
if (!this->validate()) {
cout << "Invalid Open Firmware partition content!" << endl;
return;
}
// check if the user tries to change a flag
for (i = 0; i < 8; i++) {
if (var_name == flag_names[i]) {
if (value == "true")
flag = 1;
else if (value == "false")
flag = 0;
else {
cout << "Invalid property value: " << value << endl;
return;
}
uint8_t flag_bit = 0x80U >> i;
uint8_t of_flags = this->buf[12];
if (flag)
of_flags |= flag_bit;
else
of_flags &= ~flag_bit;
this->buf[12] = of_flags;
this->update_partition();
cout << " ok" << endl; // mimic Forth
return;
}
}
// see if one of the standard properties should be changed
if (of_vars.find(var_name) == of_vars.end()) {
cout << "Attempt to change unknown variable " << var_name << endl;
return;
}
auto type = std::get<0>(of_vars.at(var_name));
auto offset = std::get<1>(of_vars.at(var_name));
if (type == OF_VAR_TYPE_INT) {
uint32_t num;
try {
num = str2env(value);
} catch (invalid_argument& exc) {
cout << exc.what() << endl;
return;
}
WRITE_DWORD_BE_A(&this->buf[offset], num);
this->update_partition();
cout << " ok" << endl; // mimic Forth
} else {
uint16_t str_offset = READ_WORD_BE_A(&this->buf[offset]);
uint16_t str_len = READ_WORD_BE_A(&this->buf[offset+2]);
OfNvramHdr *hdr = (OfNvramHdr *)&this->buf[0];
uint16_t here = READ_WORD_BE_A(&hdr->here);
uint16_t top = READ_WORD_BE_A(&hdr->top);
// check if there is enough space in the heap for the new string
// the heap is grown down from offset 0x2000 and cannot be lower than here (0x185c)
uint16_t new_top = top + str_len - value.length();
if (new_top < here) {
cout << "No room in the heap!" << endl;
return;
}
// remove the old string
std::memmove(&this->buf[top + str_len - OF_NVRAM_OFFSET], &this->buf[top - OF_NVRAM_OFFSET], str_offset - top);
for (auto& var : of_vars) {
auto type = std::get<0>(var.second);
auto offset = std::get<1>(var.second);
if (type == OF_VAR_TYPE_STR) {
uint16_t i_str_offset = READ_WORD_BE_A(&this->buf[offset]);
if (i_str_offset < str_offset) {
WRITE_WORD_BE_A(&this->buf[offset], i_str_offset + str_len);
}
}
}
top = new_top;
// copy new string into NVRAM buffer char by char
i = 0;
for(char& ch : value) {
this->buf[top + i - OF_NVRAM_OFFSET] = ch == '\x0A' ? '\x0D' : ch;
i++;
}
// stuff new values into the variable state
WRITE_WORD_BE_A(&this->buf[offset+0], top);
WRITE_WORD_BE_A(&this->buf[offset+2], value.length());
// update partition header
WRITE_WORD_BE_A(&hdr->top, top);
// update physical NVRAM
this->update_partition();
cout << " ok" << endl; // mimic Forth
return;
}
}
+67
View File
@@ -0,0 +1,67 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef OF_NVRAM_H
#define OF_NVRAM_H
#include <devices/common/nvram.h>
/** @file Utilities for working with the Apple Open Firmware NVRAM partition. */
#define OF_NVRAM_OFFSET 0x1800
#define OF_NVRAM_SIG 0x1275
// OF Variable types
enum {
OF_VAR_TYPE_INT = 1,
OF_VAR_TYPE_STR = 2,
};
typedef struct {
uint16_t sig; // >nv.1275 partition signature (= 0x1275)
uint8_t version; // >nv.version header version (= 5)
uint8_t num_pages; // >nv.pages number of memory pages (= 8 pages 0x100 bytes each)
uint16_t checksum; // >nv.checksum partition checksum
uint16_t here; // >nv.here offset to the next free byte (offset of after last string length; = 0x185c)
uint16_t top; // >nv.top offset to the last free byte (offset of string with lowest offset; < 0x2000)
} OfNvramHdr;
class OfNvramUtils {
public:
OfNvramUtils() = default;
~OfNvramUtils() = default;
int init();
void printenv();
void setenv(std::string var_name, std::string value);
protected:
bool validate();
uint16_t checksum_partition();
void update_partition();
private:
NVram* nvram_obj = nullptr;
int size = 0;
uint8_t buf[0x800];
};
#endif // OF_NVRAM_H
+405
View File
@@ -0,0 +1,405 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** Bandit ARBus-to-PCI Bridge emulation. */
#include <devices/common/pci/bandit.h>
#include <devices/deviceregistry.h>
#include <devices/memctrl/memctrlbase.h>
#include <endianswap.h>
#include <loguru.hpp>
#include <machines/machinebase.h>
#include <memaccess.h>
#include <cinttypes>
const int MultiplyDeBruijnBitPosition2[] =
{
0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
};
/** finds the position of the bit that is set */
#define WHAT_BIT_SET(val) (MultiplyDeBruijnBitPosition2[(uint32_t)(val * 0x077CB531U) >> 27])
Bandit::Bandit(int bridge_num, std::string name) : PCIHost(), PCIDevice(name)
{
supports_types(HWCompType::PCI_HOST | HWCompType::PCI_DEV);
this->base_addr = 0xF0000000 + ((bridge_num & 3) << 25);
MemCtrlBase *mem_ctrl = dynamic_cast<MemCtrlBase *>
(gMachineObj->get_comp_by_type(HWCompType::MEM_CTRL));
// add memory mapped I/O region for Bandit control registers
// This region has the following layout:
// base_addr + 0x000000 --> I/O space
// base_addr + 0x800000 --> CONFIG_ADDR
// base_addr + 0xC00000 --> CONFIG_DATA
// base_addr + 0x1000000 --> pass-through memory space (not included below)
mem_ctrl->add_mmio_region(base_addr, 0x01000000, this);
// prepare the PCI config header
this->vendor_id = PCI_VENDOR_APPLE;
this->device_id = 0x0001;
this->class_rev = 0x06000003;
this->cache_ln_sz = 8;
this->command = 0x16;
// make several PCI config space registers read-only
this->pci_wr_cmd = [](uint16_t cmd) {}; // command register
this->pci_wr_cache_lnsz = [](uint8_t val) {}; // cache line size register
// set the bits in the fine address space field of the address mask register
// that correspond to the 32MB assigned PCI address space of this Bandit.
// This initialization is implied by the device functionality.
this->addr_mask = 3 << ((bridge_num & 3) * 2);
this->name = name;
}
uint32_t Bandit::pci_cfg_read(uint32_t reg_offs, uint32_t size)
{
if (reg_offs < 64) {
return PCIDevice::pci_cfg_read(reg_offs, size);
}
switch (reg_offs) {
case BANDIT_ADDR_MASK:
return BYTESWAP_32(this->addr_mask);
default:
LOG_F(WARNING, "%s: reading from unimplemented config register at 0x%X",
this->pci_name.c_str(), reg_offs);
}
return 0;
}
void Bandit::pci_cfg_write(uint32_t reg_offs, uint32_t value, uint32_t size)
{
if (reg_offs < 64) {
PCIDevice::pci_cfg_write(reg_offs, value, size);
return;
}
switch (reg_offs) {
case BANDIT_ADDR_MASK:
this->addr_mask = BYTESWAP_32(value);
this->verbose_address_space();
break;
default:
LOG_F(WARNING, "%s: writing to unimplemented config register at 0x%X",
this->pci_name.c_str(), reg_offs);
}
}
uint32_t Bandit::read(uint32_t rgn_start, uint32_t offset, int size)
{
int bus_num, dev_num, fun_num;
uint8_t reg_offs;
uint32_t result, idsel;
if (offset & BANDIT_CONFIG_SPACE) {
if (offset & 0x00400000) {
fun_num = (this->config_addr >> 8) & 7;
reg_offs = this->config_addr & 0xFCU;
// access to the CONFIG_DATA pseudo-register causes a Config Cycle
if (this->config_addr & BANDIT_CAR_TYPE) {
bus_num = (this->config_addr >> 16) & 255;
dev_num = (this->config_addr >> 11) & 31;
LOG_F(
WARNING, "%s: read config cycle type 1 not supported yet %02x:%02x.%x @%02x.%c",
this->name.c_str(), bus_num, dev_num, fun_num, reg_offs + (offset & 3),
size == 4 ? 'l' : size == 2 ? 'w' : size == 1 ? 'b' : '0' + size
);
return 0xFFFFFFFFUL; // PCI spec §6.1
}
idsel = (this->config_addr >> 11) & 0x1FFFFFU;
if (!SINGLE_BIT_SET(idsel)) {
LOG_F(
ERROR, "%s: read invalid IDSEL=0x%X config:0x%X ??:??.%x? @%02x?.%c",
this->name.c_str(), idsel, this->config_addr,
fun_num, reg_offs + (offset & 3),
size == 4 ? 'l' : size == 2 ? 'w' : size == 1 ? 'b' : '0' + size
);
return 0xFFFFFFFFUL; // PCI spec §6.1
}
if (idsel == BANDIT_ID_SEL) { // access to myself
result = this->pci_cfg_read(reg_offs, size);
} else {
if (this->dev_map.count(idsel)) {
result = this->dev_map[idsel]->pci_cfg_read(reg_offs, size);
} else {
dev_num = WHAT_BIT_SET(idsel) + 11;
LOG_F(
ERROR, "%s err: read attempt from non-existing PCI device ??:%02x.%x @%02x.%c",
this->name.c_str(), dev_num, fun_num, reg_offs + (offset & 3),
size == 4 ? 'l' : size == 2 ? 'w' : size == 1 ? 'b' : '0' + size
);
return 0xFFFFFFFFUL; // PCI spec §6.1
}
}
} else {
result = this->config_addr;
}
} else { // I/O space access
// broadcast I/O request to devices that support I/O space
// until a device returns true that means "request accepted"
for (auto& dev : this->io_space_devs) {
if (dev->pci_io_read(offset, size, &result)) {
return result;
}
}
LOG_F(ERROR, "%s: attempt to read from unmapped PCI I/O space, offset=0x%X",
this->name.c_str(), offset);
}
return result;
}
void Bandit::write(uint32_t rgn_start, uint32_t offset, uint32_t value, int size)
{
int bus_num, dev_num, fun_num;
uint8_t reg_offs;
uint32_t idsel;
if (offset & BANDIT_CONFIG_SPACE) {
if (offset & 0x00400000) {
fun_num = (this->config_addr >> 8) & 7;
reg_offs = this->config_addr & 0xFCU;
// access to the CONFIG_DATA pseudo-register causes a Config Cycle
if (this->config_addr & BANDIT_CAR_TYPE) {
bus_num = (this->config_addr >> 16) & 255;
dev_num = (this->config_addr >> 11) & 31;
LOG_F(
WARNING, "%s: write config cycle type 1 not supported yet %02x:%02x.%x @%02x.%c = %0*x",
this->name.c_str(), bus_num, dev_num, fun_num, reg_offs + (offset & 3),
size == 4 ? 'l' : size == 2 ? 'w' : size == 1 ? 'b' : '0' + size, size * 2, value
);
return;
}
idsel = (this->config_addr >> 11) & 0x1FFFFFU;
if (!SINGLE_BIT_SET(idsel)) {
LOG_F(
ERROR, "%s: write invalid IDSEL=0x%X config:0x%X ??:??.%x? @%02x?.%c = %0*x",
this->name.c_str(), idsel, this->config_addr,
fun_num, reg_offs + (offset & 3),
size == 4 ? 'l' : size == 2 ? 'w' : size == 1 ? 'b' : '0' + size, size * 2, value
);
return;
}
if (idsel == BANDIT_ID_SEL) { // access to myself
this->pci_cfg_write(reg_offs, value, size);
return;
}
if (this->dev_map.count(idsel)) {
this->dev_map[idsel]->pci_cfg_write(reg_offs, value, size);
} else {
dev_num = WHAT_BIT_SET(idsel) + 11;
LOG_F(
ERROR, "%s err: write attempt to non-existing PCI device ??:%02x.%x @%02x.%c = %0*x",
this->name.c_str(), dev_num, fun_num, reg_offs + (offset & 3),
size == 4 ? 'l' : size == 2 ? 'w' : size == 1 ? 'b' : '0' + size, size * 2, value
);
}
} else {
this->config_addr = BYTESWAP_32(value);
}
} else { // I/O space access
// broadcast I/O request to devices that support I/O space
// until a device returns true that means "request accepted"
for (auto& dev : this->io_space_devs) {
if (dev->pci_io_write(offset, value, size)) {
return;
}
}
LOG_F(ERROR, "%s: attempt to write to unmapped PCI I/O space, offset=0x%X",
this->name.c_str(), offset);
}
}
void Bandit::verbose_address_space()
{
uint32_t mask;
int bit_pos;
LOG_F(INFO, "%s address spaces:", this->pci_name.c_str());
// verbose coarse aka 256MB memory regions
for (mask = 0x10000, bit_pos = 0; mask != 0x80000000UL; mask <<= 1, bit_pos++) {
if (this->addr_mask & mask) {
uint32_t start_addr = bit_pos << 28;
LOG_F(INFO, "- 0x%X ... 0x%X", start_addr, start_addr + 0x0FFFFFFFU);
}
}
// verbose fine aka 16MB memory regions
for (mask = 0x1, bit_pos = 0; mask != 0x10000UL; mask <<= 1, bit_pos++) {
if (this->addr_mask & mask) {
uint32_t start_addr = (bit_pos << 24) + 0xF0000000UL;
LOG_F(INFO, "- 0x%X ... 0x%X", start_addr, start_addr + 0x00FFFFFFU);
}
}
}
Chaos::Chaos(std::string name) : PCIHost()
{
supports_types(HWCompType::PCI_HOST);
MemCtrlBase *mem_ctrl = dynamic_cast<MemCtrlBase *>
(gMachineObj->get_comp_by_type(HWCompType::MEM_CTRL));
// add memory mapped I/O region for Chaos control registers
// This region has the following layout:
// base_addr + 0x800000 --> CONFIG_ADDR
// base_addr + 0xC00000 --> CONFIG_DATA
mem_ctrl->add_mmio_region(0xF0000000UL, 0x01000000, this);
this->name = name;
}
uint32_t Chaos::read(uint32_t rgn_start, uint32_t offset, int size)
{
int bus_num, dev_num, fun_num;
uint8_t reg_offs;
uint32_t result, idsel;
if (offset & BANDIT_CONFIG_SPACE) {
if (offset & 0x00400000) {
fun_num = (this->config_addr >> 8) & 7;
reg_offs = this->config_addr & 0xFCU;
// access to the CONFIG_DATA pseudo-register causes a Config Cycle
if (this->config_addr & BANDIT_CAR_TYPE) {
bus_num = (this->config_addr >> 16) & 255;
dev_num = (this->config_addr >> 11) & 31;
LOG_F(
WARNING, "%s: read config cycle type 1 not supported yet %02x:%02x.%x @%02x.%c",
this->name.c_str(), bus_num, dev_num, fun_num, reg_offs + (offset & 3),
size == 4 ? 'l' : size == 2 ? 'w' : size == 1 ? 'b' : '0' + size
);
return 0xFFFFFFFFUL; // PCI spec §6.1
}
idsel = (this->config_addr >> 11) & 0x1FFFFFU;
if (!SINGLE_BIT_SET(idsel)) {
LOG_F(
ERROR, "%s: read invalid IDSEL=0x%X config:0x%X ??:??.%x? @%02x?.%c",
this->name.c_str(), idsel, this->config_addr,
fun_num, reg_offs + (offset & 3),
size == 4 ? 'l' : size == 2 ? 'w' : size == 1 ? 'b' : '0' + size
);
return 0xFFFFFFFFUL; // PCI spec §6.1
}
if (this->dev_map.count(idsel)) {
result = this->dev_map[idsel]->pci_cfg_read(reg_offs, size);
} else {
dev_num = WHAT_BIT_SET(idsel) + 11;
LOG_F(
ERROR, "%s err: read attempt from non-existing VCI device ??:%02x.%x @%02x.%c",
this->name.c_str(), dev_num, fun_num, reg_offs + (offset & 3),
size == 4 ? 'l' : size == 2 ? 'w' : size == 1 ? 'b' : '0' + size
);
return 0xFFFFFFFFUL; // PCI spec §6.1
}
} else {
result = this->config_addr;
}
} else { // I/O space access
LOG_F(ERROR, "%s: I/O space not supported", this->name.c_str());
return 0;
}
return result;
}
void Chaos::write(uint32_t rgn_start, uint32_t offset, uint32_t value, int size)
{
int bus_num, dev_num, fun_num;
uint8_t reg_offs;
uint32_t idsel;
if (offset & BANDIT_CONFIG_SPACE) {
if (offset & 0x00400000) {
fun_num = (this->config_addr >> 8) & 7;
reg_offs = this->config_addr & 0xFCU;
// access to the CONFIG_DATA pseudo-register causes a Config Cycle
if (this->config_addr & BANDIT_CAR_TYPE) {
bus_num = (this->config_addr >> 16) & 255;
dev_num = (this->config_addr >> 11) & 31;
LOG_F(
WARNING, "%s: write config cycle type 1 not supported yet %02x:%02x.%x @%02x.%c = %0*x",
this->name.c_str(), bus_num, dev_num, fun_num, reg_offs + (offset & 3),
size == 4 ? 'l' : size == 2 ? 'w' : size == 1 ? 'b' : '0' + size, size * 2, value
);
return;
}
idsel = (this->config_addr >> 11) & 0x1FFFFFU;
if (!SINGLE_BIT_SET(idsel)) {
LOG_F(
ERROR, "%s: write invalid IDSEL=0x%X config:0x%X ??:??.%x? @%02x?.%c = %0*x",
this->name.c_str(), idsel, this->config_addr,
fun_num, reg_offs + (offset & 3),
size == 4 ? 'l' : size == 2 ? 'w' : size == 1 ? 'b' : '0' + size, size * 2, value
);
return;
}
if (this->dev_map.count(idsel)) {
this->dev_map[idsel]->pci_cfg_write(reg_offs, value, size);
} else {
dev_num = WHAT_BIT_SET(idsel) + 11;
LOG_F(
ERROR, "%s err: write attempt to non-existing VCI device ??:%02x.%x @%02x.%c = %0*x",
this->name.c_str(), dev_num, fun_num, reg_offs + (offset & 3),
size == 4 ? 'l' : size == 2 ? 'w' : size == 1 ? 'b' : '0' + size, size * 2, value
);
}
} else {
this->config_addr = BYTESWAP_32(value);
}
} else { // I/O space access
LOG_F(ERROR, "%s: I/O space not supported", this->name.c_str());
}
}
static const DeviceDescription Bandit1_Descriptor = {
Bandit::create_first, {}, {}
};
static const DeviceDescription Chaos_Descriptor = {
Chaos::create, {}, {}
};
REGISTER_DEVICE(Bandit1, Bandit1_Descriptor);
REGISTER_DEVICE(Chaos, Chaos_Descriptor);
+101
View File
@@ -0,0 +1,101 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** Bandit ARBus-to-PCI bridge definitions.
The Bandit ASIC is a custom ARBus-to-PCI bridge used in the second
generation of the Power Macintosh computer equipped with the PCI bus.
*/
#ifndef BANDIT_PCI_H
#define BANDIT_PCI_H
#include <devices/common/hwcomponent.h>
#include <devices/common/mmiodevice.h>
#include <devices/common/pci/pcidevice.h>
#include <devices/common/pci/pcihost.h>
#include <cinttypes>
#include <memory>
#include <string>
#include <unordered_map>
#define BANDIT_ID_SEL (1 << 0) // Bandit's own IDSEL
#define BANDIT_CAR_TYPE (1 << 0) // Bandit config address type bit
#define BANDIT_CONFIG_SPACE 0x00800000 // Bandit Config Space bit
/** Bandit specific configuration registers. */
enum {
BANDIT_ADDR_MASK = 0x48,
};
/** checks if one bit is set at time, return 0 if not */
#define SINGLE_BIT_SET(val) ((val) && !((val) & ((val)-1)))
class Bandit : public PCIHost, public PCIDevice {
public:
Bandit(int bridge_num, std::string name);
~Bandit() = default;
static std::unique_ptr<HWComponent> create_first() {
return std::unique_ptr<Bandit>(new Bandit(1, "Bandit-PCI1"));
};
uint32_t pci_cfg_read(uint32_t reg_offs, uint32_t size);
void pci_cfg_write(uint32_t reg_offs, uint32_t value, uint32_t size);
// MMIODevice methods
uint32_t read(uint32_t rgn_start, uint32_t offset, int size);
void write(uint32_t rgn_start, uint32_t offset, uint32_t value, int size);
protected:
void verbose_address_space();
private:
uint32_t base_addr;
uint32_t config_addr;
uint32_t addr_mask;
};
/** Chaos is a custom ARBus-to-PCI bridge that provides a specialized
PCI-like bus for video called VCI. This 64-bit bus runs at the same
frequency as the CPU bus (40-50 MHz) and provides an interface
between video input/output devices and the CPU bus.
*/
class Chaos : public PCIHost, public MMIODevice {
public:
Chaos(std::string name);
~Chaos() = default;
static std::unique_ptr<HWComponent> create() {
return std::unique_ptr<Chaos>(new Chaos("VCI0"));
};
// MMIODevice methods
uint32_t read(uint32_t rgn_start, uint32_t offset, int size);
void write(uint32_t rgn_start, uint32_t offset, uint32_t value, int size);
private:
std::string name;
uint32_t config_addr;
};
#endif // BANDIT_PCI_H
+267
View File
@@ -0,0 +1,267 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <devices/common/pci/pcidevice.h>
#include <devices/common/viacuda.h>
#include <endianswap.h>
#include <loguru.hpp>
#include <memaccess.h>
#include <cinttypes>
#include <fstream>
#include <string>
PCIDevice::PCIDevice(std::string name)
{
this->pci_name = name;
this->pci_rd_stat = [this]() { return this->status; };
this->pci_rd_cmd = [this]() { return this->command; };
this->pci_rd_bist = []() { return 0; };
this->pci_rd_lat_timer = [this]() { return this->lat_timer; };
this->pci_rd_cache_lnsz = [this]() { return this->cache_ln_sz; };
this->pci_wr_stat = [](uint16_t val) {};
this->pci_wr_cmd = [this](uint16_t cmd) { this->command = cmd; };
this->pci_wr_bist = [](uint8_t val) {};
this->pci_wr_lat_timer = [this](uint8_t val) { this->lat_timer = val; };
this->pci_wr_cache_lnsz = [this](uint8_t val) { this->cache_ln_sz = val; };
this->pci_notify_bar_change = [](int bar_num) {};
};
uint32_t PCIDevice::pci_cfg_read(uint32_t reg_offs, uint32_t size)
{
uint32_t result;
switch (reg_offs) {
case PCI_CFG_DEV_ID:
result = (this->device_id << 16) | (this->vendor_id);
break;
case PCI_CFG_STAT_CMD:
result = (this->pci_rd_stat() << 16) | (this->pci_rd_cmd());
break;
case PCI_CFG_CLASS_REV:
result = this->class_rev;
break;
case PCI_CFG_DWORD_3:
result = (pci_rd_bist() << 24) | (this->hdr_type << 16) |
(pci_rd_lat_timer() << 8) | pci_rd_cache_lnsz();
break;
case PCI_CFG_BAR0:
case PCI_CFG_BAR1:
case PCI_CFG_BAR2:
case PCI_CFG_BAR3:
case PCI_CFG_BAR4:
case PCI_CFG_BAR5:
result = this->bars[(reg_offs - 0x10) >> 2];
break;
case PCI_CFG_SUBSYS_ID:
result = (this->subsys_id << 16) | (this->subsys_vndr);
break;
case PCI_CFG_ROM_BAR:
result = this->exp_rom_bar;
break;
case PCI_CFG_DWORD_15:
result = (max_lat << 24) | (min_gnt << 16) | (irq_pin << 8) | irq_line;
break;
case PCI_CFG_CAP_PTR:
result = cap_ptr;
break;
default:
LOG_F(
WARNING, "%s: attempt to read from reserved/unimplemented register @%02x.%c",
this->pci_name.c_str(), reg_offs,
size == 4 ? 'l' : size == 2 ? 'w' : size == 1 ? 'b' : '0' + size
);
return 0;
}
if (size == 4) {
return BYTESWAP_32(result);
} else {
return read_mem_rev(((uint8_t *)&result) + (reg_offs & 3), size);
}
}
void PCIDevice::pci_cfg_write(uint32_t reg_offs, uint32_t value, uint32_t size)
{
uint32_t data;
if (size == 4) {
data = BYTESWAP_32(value);
} else {
// get current register content as DWORD and update it partially
data = BYTESWAP_32(this->pci_cfg_read(reg_offs, 4));
write_mem_rev(((uint8_t *)&data) + (reg_offs & 3), value, size);
}
switch (reg_offs) {
case PCI_CFG_STAT_CMD:
this->pci_wr_stat(data >> 16);
this->pci_wr_cmd(data & 0xFFFFU);
break;
case PCI_CFG_DWORD_3:
this->pci_wr_bist(data >> 24);
this->pci_wr_lat_timer((data >> 8) & 0xFF);
this->pci_wr_cache_lnsz(data & 0xFF);
break;
case PCI_CFG_BAR0:
case PCI_CFG_BAR1:
case PCI_CFG_BAR2:
case PCI_CFG_BAR3:
case PCI_CFG_BAR4:
case PCI_CFG_BAR5:
if (data == 0xFFFFFFFFUL) {
this->do_bar_sizing((reg_offs - 0x10) >> 2);
} else {
this->set_bar_value((reg_offs - 0x10) >> 2, data);
}
break;
case PCI_CFG_ROM_BAR:
if (data == 0xFFFFF800UL) {
this->exp_rom_bar = this->exp_bar_cfg;
} else {
this->exp_rom_bar = (data & 0xFFFFF801UL);
if (this->exp_rom_bar & 1) {
this->map_exp_rom_mem();
} else {
LOG_F(WARNING, "%s: unmapping of expansion ROM not implemented yet",
this->pci_name.c_str());
}
}
break;
case PCI_CFG_DWORD_15:
this->irq_line = data >> 24;
break;
default:
LOG_F(
WARNING, "%s: attempt to write to reserved/unimplemented register @%02x.%c = %0*x",
this->pci_name.c_str(), reg_offs,
size == 4 ? 'l' : size == 2 ? 'w' : size == 1 ? 'b' : '0' + size, size * 2, value
);
}
}
int PCIDevice::attach_exp_rom_image(const std::string img_path)
{
std::ifstream img_file;
int result = 0;
this->exp_bar_cfg = 0; // tell the world we got no ROM for now
try {
img_file.open(img_path, std::ios::in | std::ios::binary);
if (img_file.fail()) {
throw std::runtime_error("could not open specified ROM dump image");
}
// validate image file
uint8_t buf[4] = { 0 };
img_file.seekg(0, std::ios::beg);
img_file.read((char *)buf, sizeof(buf));
if (buf[0] != 0x55 || buf[1] != 0xAA) {
throw std::runtime_error("invalid expansion ROM signature");
}
// determine image size
img_file.seekg(0, std::ios::end);
uint32_t exp_rom_image_size = img_file.tellg();
// verify PCI struct offset
uint32_t pci_struct_offset = 0;
img_file.seekg(0x18, std::ios::beg);
img_file.read((char *)&pci_struct_offset, sizeof(pci_struct_offset));
if (pci_struct_offset > exp_rom_image_size) {
throw std::runtime_error("invalid PCI structure offset");
}
// verify PCI struct signature
img_file.seekg(pci_struct_offset, std::ios::beg);
img_file.read((char *)buf, sizeof(buf));
if (buf[0] != 'P' || buf[1] != 'C' || buf[2] != 'I' || buf[3] != 'R') {
throw std::runtime_error("unexpected PCI struct signature");
}
// find minimum rom size for the rom file (power of 2 >= 0x800)
for (this->exp_rom_size = 1 << 11; this->exp_rom_size < exp_rom_image_size; this->exp_rom_size <<= 1) {}
// ROM image ok - go ahead and load it
this->exp_rom_data = std::unique_ptr<uint8_t[]> (new uint8_t[this->exp_rom_size]);
img_file.seekg(0, std::ios::beg);
img_file.read((char *)this->exp_rom_data.get(), exp_rom_image_size);
memset(&this->exp_rom_data[exp_rom_image_size], 0xff, this->exp_rom_size - exp_rom_image_size);
if (exp_rom_image_size == this->exp_rom_size) {
LOG_F(INFO, "%s: loaded expansion rom (%d bytes).", this->pci_name.c_str(), this->exp_rom_size);
}
else {
LOG_F(WARNING, "%s: loaded expansion rom (%d bytes adjusted to %d bytes).", this->pci_name.c_str(), exp_rom_image_size, this->exp_rom_size);
}
this->exp_bar_cfg = ~(this->exp_rom_size - 1);
}
catch (const std::exception& exc) {
LOG_F(ERROR, "PCIDevice: %s", exc.what());
result = -1;
}
img_file.close();
return result;
}
void PCIDevice::do_bar_sizing(int bar_num)
{
this->bars[bar_num] = this->bars_cfg[bar_num];
}
void PCIDevice::set_bar_value(int bar_num, uint32_t value)
{
uint32_t bar_cfg = this->bars_cfg[bar_num];
if (bar_cfg & 1) {
this->bars[bar_num] = (value & 0xFFFFFFFCUL) | 1;
} else {
if (bar_cfg & 6) {
ABORT_F("Invalid or unsupported PCI space type: %d", (bar_cfg >> 1) & 3);
}
this->bars[bar_num] = (value & 0xFFFFFFF0UL) | (bar_cfg & 0xF);
}
this->pci_notify_bar_change(bar_num);
}
void PCIDevice::map_exp_rom_mem()
{
uint32_t rom_addr, rom_size;
rom_addr = this->exp_rom_bar & 0xFFFFF800UL;
rom_size = ~this->exp_bar_cfg + 1;
if (!this->exp_rom_addr || this->exp_rom_addr != rom_addr) {
this->host_instance->pci_register_mmio_region(rom_addr, rom_size, this);
this->exp_rom_addr = rom_addr;
}
}
+139
View File
@@ -0,0 +1,139 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef PCI_DEVICE_H
#define PCI_DEVICE_H
#include <devices/common/mmiodevice.h>
#include <devices/common/pci/pcihost.h>
#include <cinttypes>
#include <functional>
#include <memory>
#include <string>
/** PCI configuration space registers offsets */
enum {
PCI_CFG_DEV_ID = 0x00, // device and vendor IDs
PCI_CFG_STAT_CMD = 0x04, // command/status register
PCI_CFG_CLASS_REV = 0x08, // class code and revision ID
PCI_CFG_DWORD_3 = 0x0C, // BIST, HeaderType, Lat_Timer and Cache_Line_Size
PCI_CFG_BAR0 = 0x10, // base address register 0
PCI_CFG_BAR1 = 0x14, // base address register 1
PCI_CFG_BAR2 = 0x18, // base address register 2
PCI_CFG_BAR3 = 0x1C, // base address register 3
PCI_CFG_BAR4 = 0x20, // base address register 4
PCI_CFG_BAR5 = 0x24, // base address register 5
PCI_CFG_CIS_PTR = 0x28, // Cardbus CIS Pointer
PCI_CFG_SUBSYS_ID = 0x2C, // Subsysten IDs
PCI_CFG_ROM_BAR = 0x30, // expansion ROM base address
PCI_CFG_CAP_PTR = 0x34, // capabilities pointer
PCI_CFG_DWORD_14 = 0x38, // reserved
PCI_CFG_DWORD_15 = 0x3C, // Max_Lat, Min_Gnt, Int_Pin and Int_Line registers
};
/** PCI Vendor IDs for devices used in Power Macintosh computers. */
enum {
PCI_VENDOR_ATI = 0x1002,
PCI_VENDOR_MOTOROLA = 0x1057,
PCI_VENDOR_APPLE = 0x106B,
};
class PCIDevice : public MMIODevice {
public:
PCIDevice(std::string name);
virtual ~PCIDevice() = default;
virtual bool supports_io_space() {
return false;
};
/* I/O space access methods */
virtual bool pci_io_read(uint32_t offset, uint32_t size, uint32_t* res) {
return false;
};
virtual bool pci_io_write(uint32_t offset, uint32_t value, uint32_t size) {
return false;
};
// configuration space access methods
virtual uint32_t pci_cfg_read(uint32_t reg_offs, uint32_t size);
virtual void pci_cfg_write(uint32_t reg_offs, uint32_t value, uint32_t size);
// plugin interface for using in the derived classes
std::function<uint16_t()> pci_rd_stat;
std::function<void(uint16_t)> pci_wr_stat;
std::function<uint16_t()> pci_rd_cmd;
std::function<void(uint16_t)> pci_wr_cmd;
std::function<uint8_t()> pci_rd_bist;
std::function<void(uint8_t)> pci_wr_bist;
std::function<uint8_t()> pci_rd_lat_timer;
std::function<void(uint8_t)> pci_wr_lat_timer;
std::function<uint8_t()> pci_rd_cache_lnsz;
std::function<void(uint8_t)> pci_wr_cache_lnsz;
std::function<void(int)> pci_notify_bar_change;
int attach_exp_rom_image(const std::string img_path);
virtual void set_host(PCIHost* host_instance) {
this->host_instance = host_instance;
};
protected:
void do_bar_sizing(int bar_num);
void set_bar_value(int bar_num, uint32_t value);
void map_exp_rom_mem();
std::string pci_name; // human-readable device name
PCIHost* host_instance; // host bridge instance to call back
// PCI configuration space state
uint16_t vendor_id;
uint16_t device_id;
uint32_t class_rev; // class code and revision id
uint16_t status = 0;
uint16_t command = 0;
uint8_t hdr_type = 0; // header type
uint8_t lat_timer = 0; // latency timer
uint8_t cache_ln_sz = 0; // cache line size
uint16_t subsys_id = 0;
uint16_t subsys_vndr = 0;
uint8_t cap_ptr = 0;
uint8_t max_lat = 0;
uint8_t min_gnt = 0;
uint8_t irq_pin = 0;
uint8_t irq_line = 0;
uint32_t bars[6] = { 0 }; // base address registers
uint32_t bars_cfg[6] = { 0 }; // configuration values for base address registers
uint32_t exp_bar_cfg = 0; // expansion ROM configuration
uint32_t exp_rom_bar = 0; // expansion ROM base address register
uint32_t exp_rom_addr = 0; // expansion ROM base address
uint32_t exp_rom_size = 0; // expansion ROM size in bytes
std::unique_ptr<uint8_t[]> exp_rom_data;
};
#endif /* PCI_DEVICE_H */
+77
View File
@@ -0,0 +1,77 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <devices/common/hwcomponent.h>
#include <devices/common/pci/pcidevice.h>
#include <devices/common/pci/pcihost.h>
#include <devices/memctrl/memctrlbase.h>
#include <machines/machinebase.h>
#include <loguru.hpp>
#include <cinttypes>
bool PCIHost::pci_register_device(int dev_num, PCIDevice* dev_instance)
{
// return false if dev_num already registered
if (this->dev_map.count(dev_num))
return false;
this->dev_map[dev_num] = dev_instance;
dev_instance->set_host(this);
if (dev_instance->supports_io_space()) {
this->io_space_devs.push_back(dev_instance);
}
return true;
}
bool PCIHost::pci_register_mmio_region(uint32_t start_addr, uint32_t size, PCIDevice* obj)
{
MemCtrlBase *mem_ctrl = dynamic_cast<MemCtrlBase *>
(gMachineObj->get_comp_by_type(HWCompType::MEM_CTRL));
// FIXME: add sanity checks!
return mem_ctrl->add_mmio_region(start_addr, size, obj);
}
void PCIHost::attach_pci_device(std::string& dev_name, int slot_id)
{
if (!DeviceRegistry::device_registered(dev_name)) {
LOG_F(WARNING, "PCIHost: specified PCI device %s doesn't exist", dev_name.c_str());
return;
}
// attempt to create device object
auto dev_obj = DeviceRegistry::get_descriptor(dev_name).m_create_func();
if (!dev_obj->supports_type(HWCompType::PCI_DEV)) {
LOG_F(WARNING, "PCIHost: cannot attach non-PCI device %s", dev_name.c_str());
return;
}
// add device to the machine object
gMachineObj->add_device(dev_name, std::move(dev_obj));
// register device with the PCI host
this->pci_register_device(
slot_id, dynamic_cast<PCIDevice*>(gMachineObj->get_comp_by_name(dev_name)));
}
@@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@@ -19,5 +19,35 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
void interpreter_update_counters();
void interpreter_main_loop();
#ifndef PCI_HOST_H
#define PCI_HOST_H
#include <devices/deviceregistry.h>
#include <cinttypes>
#include <string>
#include <unordered_map>
#include <vector>
class PCIDevice; // forward declaration to prevent errors
class PCIHost {
public:
PCIHost() {
this->dev_map.clear();
io_space_devs.clear();
};
~PCIHost() = default;
virtual bool pci_register_device(int dev_num, PCIDevice* dev_instance);
virtual bool pci_register_mmio_region(uint32_t start_addr, uint32_t size, PCIDevice* obj);
virtual void attach_pci_device(std::string& dev_name, int slot_id);
protected:
std::unordered_map<int, PCIDevice*> dev_map;
std::vector<PCIDevice*> io_space_devs;
};
#endif /* PCI_HOST_H */
@@ -21,10 +21,14 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
/** @file MESH (Macintosh Enhanced SCSI Hardware) controller emulation. */
#include "mesh.h"
#include <devices/common/scsi/mesh.h>
#include <devices/deviceregistry.h>
#include <cinttypes>
#include <loguru.hpp>
using namespace MeshScsi;
uint8_t MESHController::read(uint8_t reg_offset)
{
switch(reg_offset) {
@@ -35,7 +39,7 @@ uint8_t MESHController::read(uint8_t reg_offset)
LOG_F(INFO, "MESH: read from MeshID register");
return this->chip_id; // tell them who we are
default:
LOG_F(WARNING, "MESH: read from unimplemented register at offset %d", reg_offset);
LOG_F(WARNING, "MESH: read from unimplemented register at offset 0x%x", reg_offset);
}
return 0;
@@ -63,6 +67,12 @@ void MESHController::write(uint8_t reg_offset, uint8_t value)
LOG_F(INFO, "MESH: write 0x%02X to SyncParms register", value);
break;
default:
LOG_F(WARNING, "MESH: write to unimplemented register at offset %d", reg_offset);
LOG_F(WARNING, "MESH: write to unimplemented register at offset 0x%x", reg_offset);
}
}
static const DeviceDescription Mesh_Descriptor = {
MESHController::create, {}, {}
};
REGISTER_DEVICE(Mesh, Mesh_Descriptor);
+16 -2
View File
@@ -24,11 +24,16 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#ifndef MESH_H
#define MESH_H
#include <devices/common/hwcomponent.h>
#include <cinttypes>
#include <memory>
// Chip ID returned by the MESH cell inside the Heathrow ASIC
#define HeathrowMESHID 4
namespace MeshScsi {
// MESH registers offsets
enum MeshReg : uint8_t {
XferCount0 = 0,
@@ -49,11 +54,20 @@ enum MeshReg : uint8_t {
SelTimeOut = 0xF
};
class MESHController {
}; // namespace MeshScsi
class MESHController : public HWComponent {
public:
MESHController(uint8_t mesh_id) { this->chip_id = mesh_id; };
MESHController(uint8_t mesh_id) {
supports_types(HWCompType::SCSI_HOST | HWCompType::SCSI_DEV);
this->chip_id = mesh_id;
};
~MESHController() = default;
static std::unique_ptr<HWComponent> create() {
return std::unique_ptr<MESHController>(new MESHController(HeathrowMESHID));
}
// MESH registers access
uint8_t read(uint8_t reg_offset);
void write(uint8_t reg_offset, uint8_t value);
+367
View File
@@ -0,0 +1,367 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** @file NCR53C94/Am53CF94 SCSI controller emulation. */
#include <core/timermanager.h>
#include <devices/common/hwcomponent.h>
#include <devices/common/hwinterrupt.h>
#include <devices/common/scsi/sc53c94.h>
#include <devices/deviceregistry.h>
#include <loguru.hpp>
#include <machines/machinebase.h>
#include <cinttypes>
Sc53C94::Sc53C94(uint8_t chip_id, uint8_t my_id)
{
this->chip_id = chip_id;
this->my_bus_id = my_id;
supports_types(HWCompType::SCSI_HOST | HWCompType::SCSI_DEV);
reset_device();
}
int Sc53C94::device_postinit()
{
this->bus_obj = dynamic_cast<ScsiBus*>(gMachineObj->get_comp_by_name("SCSI0"));
this->bus_obj->register_device(7, static_cast<ScsiDevice*>(this));
this->int_ctrl = dynamic_cast<InterruptCtrl*>(
gMachineObj->get_comp_by_type(HWCompType::INT_CTRL));
this->irq_id = this->int_ctrl->register_dev_int(IntSrc::SCSI1);
return 0;
}
void Sc53C94::reset_device()
{
// part-unique ID to be read using a magic sequence
this->set_xfer_count = this->chip_id << 16;
this->clk_factor = 2;
this->sel_timeout = 0;
this->is_initiator = true;
// clear command FIFO
this->cmd_fifo_pos = 0;
// clear data FIFO
this->data_fifo_pos = 0;
this->data_fifo[0] = 0;
this->seq_step = 0;
this->status = 0;
}
uint8_t Sc53C94::read(uint8_t reg_offset)
{
uint8_t int_status;
switch (reg_offset) {
case Read::Reg53C94::Command:
return this->cmd_fifo[0];
case Read::Reg53C94::Status:
return this->status;
case Read::Reg53C94::Int_Status:
int_status = this->int_status;
this->seq_step = 0;
this->int_status = 0;
this->update_irq();
return int_status;
case Read::Reg53C94::Seq_Step:
return this->seq_step;
case Read::Reg53C94::FIFO_Flags:
return (this->seq_step << 5) | (this->data_fifo_pos & 0x1F);
case Read::Reg53C94::Config_3:
return this->config3;
case Read::Reg53C94::Xfer_Cnt_Hi:
if (this->config2 & CFG2_ENF) {
return (this->xfer_count >> 16) & 0xFFU;
}
break;
default:
LOG_F(INFO, "SC53C94: reading from register %d", reg_offset);
}
return 0;
}
void Sc53C94::write(uint8_t reg_offset, uint8_t value)
{
switch (reg_offset) {
case Write::Reg53C94::Command:
update_command_reg(value);
break;
case Write::Reg53C94::FIFO:
fifo_push(value);
break;
case Write::Reg53C94::Dest_Bus_ID:
this->target_id = value & 7;
break;
case Write::Reg53C94::Sel_Timeout:
this->sel_timeout = value;
break;
case Write::Reg53C94::Sync_Offset:
this->sync_offset = value;
break;
case Write::Reg53C94::Clock_Factor:
this->clk_factor = value;
break;
case Write::Reg53C94::Config_1:
if ((value & 7) != this->my_bus_id) {
ABORT_F("SC53C94: HBA bus ID mismatch!");
}
this->config1 = value;
break;
case Write::Reg53C94::Config_2:
this->config2 = value;
break;
case Write::Reg53C94::Config_3:
this->config3 = value;
break;
default:
LOG_F(INFO, "SC53C94: writing 0x%X to %d register", value, reg_offset);
}
}
void Sc53C94::update_command_reg(uint8_t cmd)
{
if (this->on_reset && (cmd & 0x7F) != CMD_NOP) {
LOG_F(WARNING, "SC53C94: command register blocked after RESET!");
return;
}
// NOTE: Reset Device (chip), Reset Bus and DMA Stop commands execute
// immediately while all others are placed into the command FIFO
switch (cmd & 0x7F) {
case CMD_RESET_DEVICE:
case CMD_RESET_BUS:
case CMD_DMA_STOP:
this->cmd_fifo_pos = 0; // put them at the bottom of the command FIFO
}
if (this->cmd_fifo_pos < 2) {
// put new command into the command FIFO
this->cmd_fifo[this->cmd_fifo_pos++] = cmd;
if (this->cmd_fifo_pos == 1) {
exec_command();
}
} else {
LOG_F(ERROR, "SC53C94: the top of the command FIFO overwritten!");
this->status |= 0x40; // signal IOE/Gross Error
}
}
void Sc53C94::exec_command()
{
uint8_t cmd = this->cmd_fifo[0] & 0x7F;
bool is_dma_cmd = !!(this->cmd_fifo[0] & 0x80);
if (is_dma_cmd) {
if (this->config2 & CFG2_ENF) { // extended mode: 24-bit
this->xfer_count = this->set_xfer_count & 0xFFFFFFUL;
} else { // standard mode: 16-bit
this->xfer_count = this->set_xfer_count & 0xFFFFUL;
}
}
// simple commands will be executed immediately
// complex commands will be broken into multiple steps
// and handled by the sequencer
switch (cmd) {
case CMD_NOP:
this->on_reset = false; // unblock the command register
exec_next_command();
break;
case CMD_CLEAR_FIFO:
this->data_fifo_pos = 0; // set the bottom of the FIFO to zero
this->data_fifo[0] = 0;
exec_next_command();
break;
case CMD_RESET_DEVICE:
reset_device();
this->on_reset = true; // block the command register
return;
case CMD_RESET_BUS:
LOG_F(INFO, "SC53C94: resetting SCSI bus...");
// assert RST line
this->bus_obj->assert_ctrl_line(this->my_bus_id, SCSI_CTRL_RST);
// release RST line after 25 us
my_timer_id = TimerManager::get_instance()->add_oneshot_timer(
USECS_TO_NSECS(25),
[this]() {
this->bus_obj->release_ctrl_line(this->my_bus_id, SCSI_CTRL_RST);
});
if (!(config1 & 0x40)) {
LOG_F(INFO, "SC53C94: reset interrupt issued");
this->int_status |= INTSTAT_SRST;
}
exec_next_command();
break;
case CMD_SELECT_NO_ATN:
static SeqDesc * sel_no_atn_desc = new SeqDesc[3]{
{SeqState::SEL_BEGIN, 0, INTSTAT_DIS },
{SeqState::CMD_BEGIN, 3, INTSTAT_SR | INTSTAT_SO},
{SeqState::CMD_COMPLETE, 4, INTSTAT_SR | INTSTAT_SO},
};
this->seq_step = 0;
this->cmd_steps = sel_no_atn_desc;
this->cur_state = SeqState::BUS_FREE;
sequencer();
LOG_F(INFO, "SC53C94: SELECT W/O ATN command started");
break;
case CMD_ENA_SEL_RESEL:
LOG_F(INFO, "SC53C94: ENABLE SELECTION/RESELECTION command executed");
exec_next_command();
break;
default:
LOG_F(ERROR, "SC53C94: invalid/unimplemented command 0x%X", cmd);
this->cmd_fifo_pos--; // remove invalid command from FIFO
this->int_status |= INTSTAT_ICMD;
}
}
void Sc53C94::exec_next_command()
{
if (this->cmd_fifo_pos) { // skip empty command FIFO
this->cmd_fifo_pos--; // remove completed command
if (this->cmd_fifo_pos) { // is there another command in the FIFO?
this->cmd_fifo[0] = this->cmd_fifo[1]; // top -> bottom
exec_command(); // execute it
}
}
}
void Sc53C94::fifo_push(const uint8_t data)
{
if (this->data_fifo_pos < 16) {
this->data_fifo[this->data_fifo_pos++] = data;
} else {
LOG_F(ERROR, "SC53C94: data FIFO overflow!");
this->status |= 0x40; // signal IOE/Gross Error
}
}
void Sc53C94::seq_defer_state(uint64_t delay_ns)
{
seq_timer_id = TimerManager::get_instance()->add_oneshot_timer(
delay_ns,
[this]() {
// re-enter the sequencer with the state specified in next_state
this->cur_state = this->next_state;
this->sequencer();
});
}
void Sc53C94::sequencer()
{
switch (this->cur_state) {
case SeqState::IDLE:
break;
case SeqState::BUS_FREE:
if (this->bus_obj->current_phase() == ScsiPhase::BUS_FREE) {
this->next_state = SeqState::ARB_BEGIN;
this->seq_defer_state(BUS_FREE_DELAY + BUS_SETTLE_DELAY);
} else { // continue waiting
this->next_state = SeqState::BUS_FREE;
this->seq_defer_state(BUS_FREE_DELAY);
}
break;
case SeqState::ARB_BEGIN:
if (!this->bus_obj->begin_arbitration(this->my_bus_id)) {
LOG_F(ERROR, "SC53C94: arbitration error, bus not free!");
this->bus_obj->release_ctrl_lines(this->my_bus_id);
this->next_state = SeqState::BUS_FREE;
this->seq_defer_state(BUS_CLEAR_DELAY);
break;
}
this->next_state = SeqState::ARB_END;
this->seq_defer_state(ARB_DELAY);
break;
case SeqState::ARB_END:
if (this->bus_obj->end_arbitration(this->my_bus_id)) { // arbitration won
this->next_state = this->cmd_steps->next_step;
this->seq_defer_state(BUS_CLEAR_DELAY + BUS_SETTLE_DELAY);
} else { // arbitration lost
LOG_F(INFO, "SC53C94: arbitration lost!");
this->bus_obj->release_ctrl_lines(this->my_bus_id);
this->next_state = SeqState::BUS_FREE;
this->seq_defer_state(BUS_CLEAR_DELAY);
}
break;
case SeqState::SEL_BEGIN:
this->is_initiator = true;
this->bus_obj->begin_selection(this->my_bus_id, this->target_id,
this->cur_cmd != CMD_SELECT_NO_ATN);
this->next_state = SeqState::SEL_END;
this->seq_defer_state(SEL_TIME_OUT);
break;
case SeqState::SEL_END:
if (this->bus_obj->end_selection(this->my_bus_id, this->target_id)) {
LOG_F(INFO, "SC53C94: selection completed");
this->cmd_steps++;
} else { // selection timeout
this->seq_step = this->cmd_steps->step_num;
this->int_status |= this->cmd_steps->status;
this->bus_obj->disconnect(this->my_bus_id);
this->cur_state = SeqState::IDLE;
this->update_irq();
exec_next_command();
}
break;
default:
ABORT_F("SC53C94: unimplemented sequencer state %d", this->cur_state);
}
}
void Sc53C94::update_irq()
{
uint8_t new_irq = !!(this->int_status != 0);
if (new_irq != this->irq) {
this->irq = new_irq;
this->status = (this->status & 0x7F) | (new_irq << 7);
this->int_ctrl->ack_int(this->irq_id, new_irq);
}
}
void Sc53C94::notify(ScsiMsg msg_type, int param)
{
switch (msg_type) {
case ScsiMsg::CONFIRM_SEL:
if (this->target_id == param) {
// cancel selection timeout timer
TimerManager::get_instance()->cancel_timer(this->seq_timer_id);
this->cur_state = SeqState::SEL_END;
sequencer();
} else {
LOG_F(WARNING, "SC53C94: ignore invalid selection confirmation message");
}
break;
default:
LOG_F(WARNING, "SC53C94: ignore notification message, type: %d", msg_type);
}
}
static const DeviceDescription Sc53C94_Descriptor = {
Sc53C94::create, {}, {}
};
REGISTER_DEVICE(Sc53C94, Sc53C94_Descriptor);
+196
View File
@@ -0,0 +1,196 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** @file NCR53C94/Am53CF94 SCSI controller definitions. */
/* NOTE: Power Macintosh computers don't have a real NCR 53C94 chip.
The corresponding functionality is provided by the 53CF94 compatible
cell in the custom Curio IC (Am79C950).
*/
#ifndef SC_53C94_H
#define SC_53C94_H
#include <devices/common/scsi/scsi.h>
#include <devices/common/hwinterrupt.h>
#include <cinttypes>
#include <memory>
/** 53C94 read registers */
namespace Read {
enum Reg53C94 : uint8_t {
Xfer_Cnt_LSB = 0,
Xfer_Cnt_MSB = 1,
FIFO = 2,
Command = 3,
Status = 4,
Int_Status = 5,
Seq_Step = 6,
FIFO_Flags = 7,
Config_1 = 8,
Config_2 = 0xB,
Config_3 = 0xC,
Config_4 = 0xD, // Am53CF94 extension
Xfer_Cnt_Hi = 0xE, // Am53CF94 extension
};
};
/** 53C94 write registers */
namespace Write {
enum Reg53C94 : uint8_t {
Xfer_Cnt_LSB = 0,
Xfer_Cnt_MSB = 1,
FIFO = 2,
Command = 3,
Dest_Bus_ID = 4,
Sel_Timeout = 5,
Sync_Period = 6,
Sync_Offset = 7,
Config_1 = 8,
Clock_Factor = 9,
Test_Mode = 0xA,
Config_2 = 0xB,
Config_3 = 0xC,
Config_4 = 0xD, // Am53CF94 extension
Xfer_Cnt_Hi = 0xE, // Am53CF94 extension
Data_Align = 0xF
};
};
/** NCR53C94/Am53CF94 commands. */
enum {
CMD_NOP = 0,
CMD_CLEAR_FIFO = 1,
CMD_RESET_DEVICE = 2,
CMD_RESET_BUS = 3,
CMD_DMA_STOP = 4,
CMD_SELECT_NO_ATN = 0x41,
CMD_ENA_SEL_RESEL = 0x44,
};
/** Interrupt status register bits. */
enum {
INTSTAT_SRST = 0x80, // bus reset
INTSTAT_ICMD = 0x40, // invalid command
INTSTAT_DIS = 0x20, // disconnected
INTSTAT_SR = 0x10, // service request
INTSTAT_SO = 0x08, // successful operation
INTSTAT_RESEL = 0x04, // reselected
INTSTAT_SELA = 0x02, // selected as a target with attention
INTSTAT_SEL = 0x01, // selected as a target without attention
};
enum {
CFG2_ENF = 0x40, // Am53CF94: enable features (ENF) bit
};
/** Sequencer states. */
namespace SeqState {
enum {
IDLE = 0,
BUS_FREE,
ARB_BEGIN,
ARB_END,
SEL_BEGIN,
SEL_END,
CMD_BEGIN,
CMD_COMPLETE,
};
};
/** Sequence descriptor for sequencer commands. */
typedef struct {
int next_step;
int step_num;
int status;
} SeqDesc;
class Sc53C94 : public ScsiDevice {
public:
Sc53C94(uint8_t chip_id=12, uint8_t my_id=7);
~Sc53C94() = default;
static std::unique_ptr<HWComponent> create() {
return std::unique_ptr<Sc53C94>(new Sc53C94());
}
// HWComponent methods
int device_postinit();
// 53C94 registers access
uint8_t read(uint8_t reg_offset);
void write(uint8_t reg_offset, uint8_t value);
// ScsiDevice methods
void notify(ScsiMsg msg_type, int param);
protected:
void reset_device();
void update_command_reg(uint8_t cmd);
void exec_command();
void exec_next_command();
void fifo_push(const uint8_t data);
void sequencer();
void seq_defer_state(uint64_t delay_ns);
void update_irq();
private:
uint8_t chip_id;
uint8_t my_bus_id;
ScsiBus* bus_obj;
uint32_t my_timer_id;
uint8_t cmd_fifo[2];
uint8_t data_fifo[16];
int cmd_fifo_pos;
int data_fifo_pos;
bool on_reset = false;
uint32_t xfer_count;
uint32_t set_xfer_count;
uint8_t status;
uint8_t target_id;
uint8_t int_status;
uint8_t seq_step;
uint8_t sel_timeout;
uint8_t sync_offset;
uint8_t clk_factor;
uint8_t config1;
uint8_t config2;
uint8_t config3;
// sequencer state
uint32_t seq_timer_id;
uint32_t cur_state;
uint32_t next_state;
SeqDesc* cmd_steps;
bool is_initiator;
uint8_t cur_cmd;
// interrupt related stuff
InterruptCtrl* int_ctrl = nullptr;
uint32_t irq_id = 0;
uint8_t irq = 0;
};
#endif // SC_53C94_H
+123
View File
@@ -0,0 +1,123 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** @file SCSI bus definitions. */
#ifndef SCSI_H
#define SCSI_H
#include <devices/common/hwcomponent.h>
#include <array>
#include <cinttypes>
#include <string>
/** SCSI control signals.
Bit positions follow the MESH controller convention for easier mapping.
*/
enum {
SCSI_CTRL_IO = 1 << 0,
SCSI_CTRL_CD = 1 << 1,
SCSI_CTRL_MSG = 1 << 2,
SCSI_CTRL_ATN = 1 << 3,
SCSI_CTRL_ACK = 1 << 4,
SCSI_CTRL_REQ = 1 << 5,
SCSI_CTRL_SEL = 1 << 6,
SCSI_CTRL_BSY = 1 << 7,
SCSI_CTRL_RST = 1 << 8,
};
enum ScsiPhase : int {
BUS_FREE = 0,
ARBITRATION,
SELECTION,
RESELECTION,
RESET,
};
enum ScsiMsg : int {
CONFIRM_SEL = 1,
BUS_PHASE_CHANGE,
};
/** Standard SCSI bus timing values measured in ns. */
#define BUS_SETTLE_DELAY 400
#define BUS_FREE_DELAY 800
#define BUS_CLEAR_DELAY 800
#define ARB_DELAY 2400
#define SEL_ABORT_TIME 200000
#define SEL_TIME_OUT 250000000
#define SCSI_MAX_DEVS 8
class ScsiDevice : public HWComponent {
public:
ScsiDevice() = default;
~ScsiDevice() = default;
virtual void notify(ScsiMsg msg_type, int param) = 0;
private:
int scsi_id;
};
/** This class provides a higher level abstraction for the SCSI bus. */
class ScsiBus : public HWComponent {
public:
ScsiBus();
~ScsiBus() = default;
// low-level control/status
void register_device(int id, ScsiDevice* dev_obj);
void assert_ctrl_line(int id, uint16_t mask);
void release_ctrl_line(int id, uint16_t mask);
void release_ctrl_lines(int id);
int current_phase() { return this->cur_phase; };
// high-level control/status
bool begin_arbitration(int id);
bool end_arbitration(int id);
bool begin_selection(int initiator_id, int target_id, bool atn);
void confirm_selection(int target_id);
bool end_selection(int initiator_id, int target_id);
void disconnect(int dev_id);
protected:
void change_bus_phase(int initiator_id);
private:
std::string name;
// SCSI devices registered with this bus
std::array<ScsiDevice*, SCSI_MAX_DEVS> devices;
// per-device state of the control lines
uint16_t dev_ctrl_lines[SCSI_MAX_DEVS];
uint16_t ctrl_lines;
int cur_phase;
int arb_winner_id;
int initiator_id;
int target_id;
uint8_t data_lines;
};
#endif // SCSI_H
+181
View File
@@ -0,0 +1,181 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** @file SCSI bus emulation. */
#include <devices/common/hwcomponent.h>
#include <devices/common/scsi/scsi.h>
#include <loguru.hpp>
#include <cinttypes>
ScsiBus::ScsiBus()
{
supports_types(HWCompType::SCSI_BUS);
for(int i = 0; i < SCSI_MAX_DEVS; i++) {
this->devices[i] = nullptr;
this->dev_ctrl_lines[i] = 0;
}
this->ctrl_lines = 0; // all control lines released
this->data_lines = 0; // data lines released
this->arb_winner_id = -1;
this->initiator_id = -1;
this->target_id = -1;
}
void ScsiBus::register_device(int id, ScsiDevice* dev_obj)
{
if (this->devices[id] != nullptr) {
ABORT_F("ScsiBus: device with ID %d already registered", id);
}
this->devices[id] = dev_obj;
}
void ScsiBus::change_bus_phase(int initiator_id)
{
for (int i = 0; i < SCSI_MAX_DEVS; i++) {
if (i == initiator_id)
continue; // don't notify the initiator
if (this->devices[i] != nullptr) {
this->devices[i]->notify(ScsiMsg::BUS_PHASE_CHANGE, this->cur_phase);
}
}
}
void ScsiBus::assert_ctrl_line(int initiator_id, uint16_t mask)
{
uint16_t new_state = 0xFFFFU & mask;
this->dev_ctrl_lines[initiator_id] = new_state;
if (new_state == this->ctrl_lines) {
return;
}
this->ctrl_lines = new_state;
if (new_state & SCSI_CTRL_RST) {
this->cur_phase = ScsiPhase::RESET;
change_bus_phase(initiator_id);
}
}
void ScsiBus::release_ctrl_line(int id, uint16_t mask)
{
uint16_t new_state = 0;
this->dev_ctrl_lines[id] &= ~mask;
// OR control lines of all devices together
for (int i = 0; i < SCSI_MAX_DEVS; i++) {
new_state |= this->dev_ctrl_lines[i];
}
if (this->ctrl_lines & SCSI_CTRL_RST) {
if (!(new_state & SCSI_CTRL_RST)) {
this->ctrl_lines = new_state;
this->cur_phase = ScsiPhase::BUS_FREE;
change_bus_phase(id);
}
}
}
void ScsiBus::release_ctrl_lines(int id)
{
this->release_ctrl_line(id, 0xFFFFUL);
}
bool ScsiBus::begin_arbitration(int initiator_id)
{
if (this->cur_phase == ScsiPhase::BUS_FREE) {
this->data_lines |= 1 << initiator_id;
this->cur_phase = ScsiPhase::ARBITRATION;
change_bus_phase(initiator_id);
return true;
} else {
return false;
}
}
bool ScsiBus::end_arbitration(int initiator_id)
{
int highest_id = -1;
// find the highest ID bit on the data lines
for (int id = 7; id >= 0; id--) {
if (this->data_lines & (1 << id)) {
highest_id = id;
break;
}
}
if (highest_id >= 0) {
this->arb_winner_id = highest_id;
}
return highest_id == initiator_id;
}
bool ScsiBus::begin_selection(int initiator_id, int target_id, bool atn)
{
// perform bus integrity checks
if (this->cur_phase != ScsiPhase::ARBITRATION || this->arb_winner_id != initiator_id)
return false;
this->data_lines |= (1 << initiator_id) | (1 << target_id);
if (atn) {
assert_ctrl_line(initiator_id, SCSI_CTRL_ATN);
}
this->initiator_id = initiator_id;
this->cur_phase = ScsiPhase::SELECTION;
change_bus_phase(initiator_id);
return true;
}
void ScsiBus::confirm_selection(int target_id)
{
this->target_id = target_id;
// notify initiator about selection confirmation from target
if (this->initiator_id >= 0) {
this->devices[this->initiator_id]->notify(ScsiMsg::CONFIRM_SEL, target_id);
}
}
bool ScsiBus::end_selection(int initiator_id, int target_id)
{
// check for selection confirmation from target
return this->target_id == target_id;
}
void ScsiBus::disconnect(int dev_id)
{
this->release_ctrl_lines(dev_id);
if (!(this->ctrl_lines & SCSI_CTRL_BSY) && !(this->ctrl_lines & SCSI_CTRL_SEL)) {
this->cur_phase = ScsiPhase::BUS_FREE;
change_bus_phase(dev_id);
}
}
+727
View File
@@ -0,0 +1,727 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** High-level VIA-CUDA combo device emulation.
*/
#include <core/timermanager.h>
#include <devices/common/adb/adb.h>
#include <devices/common/hwinterrupt.h>
#include <devices/common/viacuda.h>
#include <devices/deviceregistry.h>
#include <loguru.hpp>
#include <machines/machinebase.h>
#include <memaccess.h>
#include <cinttypes>
#include <memory>
using namespace std;
ViaCuda::ViaCuda() {
this->name = "ViaCuda";
supports_types(HWCompType::ADB_HOST | HWCompType::I2C_HOST);
// VIA reset clears all internal registers to logic 0
// except timers/counters and the shift register
// as stated in the 6522 datasheet
this->via_regs[VIA_A] = 0;
this->via_regs[VIA_B] = 0;
this->via_regs[VIA_DIRB] = 0;
this->via_regs[VIA_DIRA] = 0;
this->via_regs[VIA_IER] = 0;
this->via_regs[VIA_ACR] = 0;
this->via_regs[VIA_PCR] = 0;
this->via_regs[VIA_IFR] = 0;
// load maximum value into the timer registers for safety
// (not prescribed in the 6522 datasheet)
this->via_regs[VIA_T1LL] = 0xFF;
this->via_regs[VIA_T1LH] = 0xFF;
this->via_regs[VIA_T2CL] = 0xFF;
this->via_regs[VIA_T2CH] = 0xFF;
this->_via_ifr = 0; // all flags cleared
this->_via_ier = 0; // all interrupts disabled
// intialize counters/timers
this->t1_counter = 0xFFFF;
this->t1_active = false;
this->t2_counter = 0xFFFF;
this->t2_active = false;
// calculate VIA clock duration in ns
this->via_clk_dur = 1.0f / VIA_CLOCK_HZ * NS_PER_SEC;
// PRAM is part of Cuda
this->pram_obj = std::unique_ptr<NVram> (new NVram("pram.bin", 256));
// ADB bus is driven by Cuda
this->adb_bus = std::unique_ptr<ADB_Bus> (new ADB_Bus());
this->cuda_init();
this->int_ctrl = nullptr;
}
int ViaCuda::device_postinit()
{
this->int_ctrl = dynamic_cast<InterruptCtrl*>(
gMachineObj->get_comp_by_type(HWCompType::INT_CTRL));
this->irq_id = this->int_ctrl->register_dev_int(IntSrc::VIA_CUDA);
return 0;
}
void ViaCuda::cuda_init() {
this->old_tip = 0;
this->old_byteack = 0;
this->treq = 1;
this->in_count = 0;
this->out_count = 0;
this->poll_rate = 11;
}
uint8_t ViaCuda::read(int reg) {
/* reading from some VIA registers triggers special actions */
switch (reg & 0xF) {
case VIA_B:
return (this->via_regs[VIA_B]);
case VIA_A:
case VIA_ANH:
LOG_F(WARNING, "Attempted read from VIA Port A!");
break;
case VIA_IER:
return (this->_via_ier | 0x80); // bit 7 always reads as "1"
case VIA_IFR:
return this->_via_ifr;
case VIA_T1CL:
this->_via_ifr &= ~VIA_IF_T1;
update_irq();
return this->calc_counter_val(this->t1_counter, this->t1_start_time) & 0xFFU;
case VIA_T1CH:
return this->calc_counter_val(this->t1_counter, this->t1_start_time) >> 8;
case VIA_T2CL:
this->_via_ifr &= ~VIA_IF_T2;
update_irq();
return this->calc_counter_val(this->t2_counter, this->t2_start_time) & 0xFFU;
case VIA_T2CH:
return this->calc_counter_val(this->t2_counter, this->t2_start_time) >> 8;
case VIA_SR:
this->_via_ifr &= ~VIA_IF_SR;
update_irq();
break;
}
return (this->via_regs[reg & 0xF]);
}
void ViaCuda::write(int reg, uint8_t value) {
this->via_regs[reg & 0xF] = value;
switch (reg & 0xF) {
case VIA_B:
write(value);
break;
case VIA_A:
case VIA_ANH:
LOG_F(WARNING, "Attempted write to VIA Port A!");
break;
case VIA_DIRB:
LOG_F(9, "VIA_DIRB = 0x%X", value);
break;
case VIA_DIRA:
LOG_F(9, "VIA_DIRA = 0x%X", value);
break;
case VIA_PCR:
LOG_F(9, "VIA_PCR = 0x%X", value);
break;
case VIA_ACR:
LOG_F(9, "VIA_ACR = 0x%X", value);
break;
case VIA_IFR:
// for each "1" in value clear the corresponding flags
this->_via_ifr &= ~value;
update_irq();
break;
case VIA_IER:
if (value & 0x80) {
this->_via_ier |= value & 0x7F;
} else {
this->_via_ier &= ~value;
}
update_irq();
print_enabled_ints();
break;
case VIA_T1CH:
if (this->via_regs[VIA_ACR] & 0xC0) {
ABORT_F("Unsupported VIA T1 mode, ACR=0x%X", this->via_regs[VIA_ACR]);
}
// cancel active T1 timer task
if (this->t1_active) {
TimerManager::get_instance()->cancel_timer(this->t1_timer_id);
this->t1_active = false;
}
// clear T1 flag in IFR
this->_via_ifr &= ~VIA_IF_T1;
update_irq();
// load initial value into counter 1
this->t1_counter = (value << 8) | this->via_regs[VIA_T1CL];
// TODO: delay for one phase 2 clock
// sample current vCPU time and remember it
this->t1_start_time = TimerManager::get_instance()->current_time_ns();
// set up timout timer for T1
this->t1_timer_id = TimerManager::get_instance()->add_oneshot_timer(
static_cast<uint64_t>(this->via_clk_dur * (this->t1_counter + 3) + 0.5f),
[this]() {
this->assert_t1_int();
}
);
this->t1_active = true;
break;
case VIA_T2CH:
if (this->via_regs[VIA_ACR] & 0x20) {
ABORT_F("VIA T2 pulse count mode not supported!");
}
// cancel active T2 timer task
if (this->t2_active) {
TimerManager::get_instance()->cancel_timer(this->t2_timer_id);
this->t2_active = false;
}
// clear T2 flag in IFR
this->_via_ifr &= ~VIA_IF_T2;
update_irq();
// load initial value into counter 2
this->t2_counter = (value << 8) | this->via_regs[VIA_T2CL];
// TODO: delay for one phase 2 clock
// sample current vCPU time and remember it
this->t2_start_time = TimerManager::get_instance()->current_time_ns();
// set up timeout timer for T2
this->t2_timer_id = TimerManager::get_instance()->add_oneshot_timer(
static_cast<uint64_t>(this->via_clk_dur * (this->t2_counter + 3) + 0.5f),
[this]() {
this->assert_t2_int();
}
);
this->t2_active = true;
break;
case VIA_SR:
this->_via_ifr &= ~VIA_IF_SR;
update_irq();
break;
}
}
uint16_t ViaCuda::calc_counter_val(const uint16_t last_val, const uint64_t& last_time)
{
// calcualte current counter value based on elapsed time and timer frequency
uint64_t cur_time = TimerManager::get_instance()->current_time_ns();
uint32_t diff = (cur_time - last_time) / this->via_clk_dur;
return last_val - diff;
}
void ViaCuda::print_enabled_ints() {
const char* via_int_src[] = {"CA2", "CA1", "SR", "CB2", "CB1", "T2", "T1"};
for (int i = 0; i < 7; i++) {
if (this->_via_ier & (1 << i))
LOG_F(INFO, "VIA %s interrupt enabled", via_int_src[i]);
}
}
void ViaCuda::update_irq()
{
uint8_t active_ints = this->_via_ifr & this->_via_ier & 0x7F;
if (active_ints != this->old_ifr) {
uint8_t new_irq = !!active_ints;
this->_via_ifr = (active_ints) | (new_irq << 7);
this->old_ifr = active_ints;
LOG_F(6, "VIA: assert IRQ, IFR=0x%02X", this->_via_ifr);
this->int_ctrl->ack_int(this->irq_id, new_irq);
}
}
inline bool ViaCuda::ready() {
return ((this->via_regs[VIA_DIRB] & 0x38) == 0x30);
}
void ViaCuda::assert_sr_int() {
this->sr_timer_on = false;
this->_via_ifr |= VIA_IF_SR;
update_irq();
}
void ViaCuda::assert_t1_int() {
this->_via_ifr |= VIA_IF_T1;
this->t1_active = false;
update_irq();
}
void ViaCuda::assert_t2_int() {
this->_via_ifr |= VIA_IF_T2;
this->t2_active = false;
update_irq();
}
#ifdef DEBUG_CPU_INT
void ViaCuda::assert_int(uint8_t flags) {
this->_via_ifr |= (flags & 0x7F);
update_irq();
}
#endif
void ViaCuda::assert_ctrl_line(ViaLine line)
{
switch (line) {
case ViaLine::CA1:
this->_via_ifr |= VIA_IF_CA1;
break;
case ViaLine::CA2:
this->_via_ifr |= VIA_IF_CA2;
break;
case ViaLine::CB1:
this->_via_ifr |= VIA_IF_CB1;
break;
case ViaLine::CB2:
this->_via_ifr |= VIA_IF_CB1;
break;
default:
ABORT_F("Assertion of unknown VIA line requested!");
}
update_irq();
}
void ViaCuda::schedule_sr_int(uint64_t timeout_ns) {
if (this->sr_timer_on) {
TimerManager::get_instance()->cancel_timer(this->sr_timer_id);
this->sr_timer_on = false;
}
this->sr_timer_id = TimerManager::get_instance()->add_oneshot_timer(
timeout_ns,
[this]() { this->assert_sr_int(); });
this->sr_timer_on = true;
}
void ViaCuda::write(uint8_t new_state) {
if (!ready()) {
LOG_F(WARNING, "Cuda not ready!");
return;
}
int new_tip = !!(new_state & CUDA_TIP);
int new_byteack = !!(new_state & CUDA_BYTEACK);
/* return if there is no state change */
if (new_tip == this->old_tip && new_byteack == this->old_byteack)
return;
LOG_F(9, "Cuda state changed!");
this->old_tip = new_tip;
this->old_byteack = new_byteack;
if (new_tip) {
if (new_byteack) {
this->via_regs[VIA_B] |= CUDA_TREQ; /* negate TREQ */
this->treq = 1;
if (this->in_count) {
process_packet();
/* start response transaction */
this->via_regs[VIA_B] &= ~CUDA_TREQ; /* assert TREQ */
this->treq = 0;
}
this->in_count = 0;
} else {
LOG_F(9, "Cuda: enter sync state");
this->via_regs[VIA_B] &= ~CUDA_TREQ; /* assert TREQ */
this->treq = 0;
this->in_count = 0;
this->out_count = 0;
}
// send dummy byte as idle acknowledge or attention
//assert_sr_int();
schedule_sr_int(USECS_TO_NSECS(61));
} else {
if (this->via_regs[VIA_ACR] & 0x10) { /* data transfer: Host --> Cuda */
if (this->in_count < sizeof(this->in_buf)) {
this->in_buf[this->in_count++] = this->via_regs[VIA_SR];
// tell the system we've read the byte after 71 usecs
schedule_sr_int(USECS_TO_NSECS(71));
//assert_sr_int();
} else {
LOG_F(WARNING, "Cuda input buffer too small. Truncating data!");
}
} else { /* data transfer: Cuda --> Host */
(this->*out_handler)();
//assert_sr_int();
// tell the system we've written next byte after 88 usecs
schedule_sr_int(USECS_TO_NSECS(88));
}
}
}
/* sends zeros to host ad infinitum */
void ViaCuda::null_out_handler() {
this->via_regs[VIA_SR] = 0;
}
/* sends PRAM content to host ad infinitum */
void ViaCuda::pram_out_handler()
{
this->via_regs[VIA_SR] = this->pram_obj->read_byte(this->cur_pram_addr++);
}
/* sends data from out_buf until exhausted, then switches to next_out_handler */
void ViaCuda::out_buf_handler() {
if (this->out_pos < this->out_count) {
LOG_F(9, "OutBufHandler: sending next byte 0x%X", this->out_buf[this->out_pos]);
this->via_regs[VIA_SR] = this->out_buf[this->out_pos++];
} else if (this->is_open_ended) {
LOG_F(9, "OutBufHandler: switching to next handler");
this->out_handler = this->next_out_handler;
this->next_out_handler = &ViaCuda::null_out_handler;
(this->*out_handler)();
} else {
LOG_F(9, "Sending last byte");
this->out_count = 0;
this->via_regs[VIA_B] |= CUDA_TREQ; /* negate TREQ */
this->treq = 1;
}
}
void ViaCuda::response_header(uint32_t pkt_type, uint32_t pkt_flag) {
this->out_buf[0] = pkt_type;
this->out_buf[1] = pkt_flag;
this->out_buf[2] = this->in_buf[1]; /* copy original cmd */
this->out_count = 3;
this->out_pos = 0;
this->out_handler = &ViaCuda::out_buf_handler;
this->next_out_handler = &ViaCuda::null_out_handler;
this->is_open_ended = false;
}
void ViaCuda::error_response(uint32_t error) {
this->out_buf[0] = CUDA_PKT_ERROR;
this->out_buf[1] = error;
this->out_buf[2] = this->in_buf[0];
this->out_buf[3] = this->in_buf[1]; /* copy original cmd */
this->out_count = 4;
this->out_pos = 0;
this->out_handler = &ViaCuda::out_buf_handler;
this->next_out_handler = &ViaCuda::null_out_handler;
this->is_open_ended = false;
}
void ViaCuda::process_packet() {
if (this->in_count < 2) {
LOG_F(ERROR, "Cuda: invalid packet (too few data)!");
error_response(CUDA_ERR_BAD_SIZE);
return;
}
switch (this->in_buf[0]) {
case CUDA_PKT_ADB:
LOG_F(9, "Cuda: ADB packet received");
process_adb_command(this->in_buf[1], this->in_count - 2);
break;
case CUDA_PKT_PSEUDO:
LOG_F(9, "Cuda: pseudo command packet received");
LOG_F(9, "Command: 0x%X", this->in_buf[1]);
LOG_F(9, "Data count: %d", this->in_count);
for (int i = 0; i < this->in_count; i++) {
LOG_F(9, "0x%X ,", this->in_buf[i]);
}
pseudo_command(this->in_buf[1], this->in_count - 2);
break;
default:
LOG_F(ERROR, "Cuda: unsupported packet type = %d", this->in_buf[0]);
error_response(CUDA_ERR_BAD_PKT);
}
}
void ViaCuda::process_adb_command(uint8_t cmd_byte, int data_count) {
int adb_dev = cmd_byte >> 4; // 2 for keyboard, 3 for mouse
int cmd = cmd_byte & 0xF;
if (!cmd) {
LOG_F(9, "Cuda: ADB SendReset command requested");
response_header(CUDA_PKT_ADB, 0);
} else if (cmd == 1) {
LOG_F(9, "Cuda: ADB Flush command requested");
response_header(CUDA_PKT_ADB, 0);
} else if ((cmd & 0xC) == 8) {
LOG_F(9, "Cuda: ADB Listen command requested");
int adb_reg = cmd_byte & 0x3;
if (adb_bus->listen(adb_dev, adb_reg)) {
response_header(CUDA_PKT_ADB, 0);
for (int data_ptr = 0; data_ptr < adb_bus->get_output_len(); data_ptr++) {
this->in_buf[(2 + data_ptr)] = adb_bus->get_output_byte(data_ptr);
}
} else {
response_header(CUDA_PKT_ADB, 2);
}
} else if ((cmd & 0xC) == 0xC) {
LOG_F(9, "Cuda: ADB Talk command requested");
response_header(CUDA_PKT_ADB, 0);
int adb_reg = cmd_byte & 0x3;
if (adb_bus->talk(adb_dev, adb_reg, this->in_buf[2])) {
response_header(CUDA_PKT_ADB, 0);
} else {
response_header(CUDA_PKT_ADB, 2);
}
} else {
LOG_F(ERROR, "Cuda: unsupported ADB command 0x%X", cmd);
error_response(CUDA_ERR_BAD_CMD);
}
}
void ViaCuda::pseudo_command(int cmd, int data_count) {
uint16_t addr;
int i;
switch (cmd) {
case CUDA_START_STOP_AUTOPOLL:
if (this->in_buf[2]) {
LOG_F(INFO, "Cuda: autopoll started, rate: %d ms", this->poll_rate);
} else {
LOG_F(INFO, "Cuda: autopoll stopped");
}
response_header(CUDA_PKT_PSEUDO, 0);
break;
case CUDA_READ_MCU_MEM:
addr = READ_WORD_BE_A(&this->in_buf[2]);
response_header(CUDA_PKT_PSEUDO, 0);
// if starting address is within PRAM region
// prepare to transfer PRAM content, othwesise we will send zeroes
if (addr >= CUDA_PRAM_START && addr <= CUDA_PRAM_END) {
this->cur_pram_addr = addr - CUDA_PRAM_START;
this->next_out_handler = &ViaCuda::pram_out_handler;
} else if (addr >= CUDA_ROM_START) {
// HACK: Cuda ROM dump requsted so let's partially fake it
this->out_buf[3] = 0; // empty copyright string
WRITE_WORD_BE_A(&this->out_buf[4], 0x0019U);
WRITE_WORD_BE_A(&this->out_buf[6], CUDA_FW_VERSION_MAJOR);
WRITE_WORD_BE_A(&this->out_buf[8], CUDA_FW_VERSION_MINOR);
this->out_count += 7;
}
this->is_open_ended = true;
break;
case CUDA_GET_REAL_TIME:
response_header(CUDA_PKT_PSEUDO, 0);
this->out_buf[2] = (uint8_t)((this->real_time >> 24) & 0xFF);
this->out_buf[3] = (uint8_t)((this->real_time >> 16) & 0xFF);
this->out_buf[4] = (uint8_t)((this->real_time >> 8) & 0xFF);
this->out_buf[5] = (uint8_t)((this->real_time) & 0xFF);
break;
case CUDA_WRITE_MCU_MEM:
addr = READ_WORD_BE_A(&this->in_buf[2]);
// if addr is inside PRAM, update PRAM with data from in_buf
// otherwise, ignore data in in_buf
if (addr >= CUDA_PRAM_START && addr <= CUDA_PRAM_END) {
for (i = 0; i < this->in_count - 4; i++) {
this->pram_obj->write_byte((addr - CUDA_PRAM_START + i) & 0xFF,
this->in_buf[4+i]);
}
}
response_header(CUDA_PKT_PSEUDO, 0);
break;
case CUDA_READ_PRAM:
addr = READ_WORD_BE_A(&this->in_buf[2]);
if (addr <= 0xFF) {
response_header(CUDA_PKT_PSEUDO, 0);
// this command is open-ended so set up the corresponding context
this->cur_pram_addr = addr;
this->next_out_handler = &ViaCuda::pram_out_handler;
this->is_open_ended = true;
} else {
error_response(CUDA_ERR_BAD_PAR);
}
break;
case CUDA_SET_REAL_TIME:
response_header(CUDA_PKT_PSEUDO, 0);
this->real_time = ((uint32_t)in_buf[2]) >> 24;
this->real_time += ((uint32_t)in_buf[3]) >> 16;
this->real_time += ((uint32_t)in_buf[4]) >> 8;
this->real_time += ((uint32_t)in_buf[5]);
break;
case CUDA_WRITE_PRAM:
addr = READ_WORD_BE_A(&this->in_buf[2]);
if (addr <= 0xFF) {
// transfer data from in_buf to PRAM
for (i = 0; i < this->in_count - 4; i++) {
this->pram_obj->write_byte((addr + i) & 0xFF, this->in_buf[4+i]);
}
response_header(CUDA_PKT_PSEUDO, 0);
} else {
error_response(CUDA_ERR_BAD_PAR);
}
break;
case CUDA_FILE_SERVER_FLAG:
response_header(CUDA_PKT_PSEUDO, 0);
if (this->in_buf[2]) {
LOG_F(INFO, "Cuda: File server flag on");
this->file_server = true;
} else {
LOG_F(INFO, "Cuda: File server flag off");
this->file_server = false;
}
break;
case CUDA_SET_AUTOPOLL_RATE:
this->poll_rate = this->in_buf[2];
LOG_F(INFO, "Cuda: autopoll rate set to %d ms", this->poll_rate);
response_header(CUDA_PKT_PSEUDO, 0);
break;
case CUDA_GET_AUTOPOLL_RATE:
response_header(CUDA_PKT_PSEUDO, 0);
this->out_buf[3] = this->poll_rate;
this->out_count++;
break;
case CUDA_SET_DEVICE_LIST:
response_header(CUDA_PKT_PSEUDO, 0);
this->device_mask = ((uint16_t)in_buf[2]) >> 8;
this->device_mask += ((uint16_t)in_buf[3]);
break;
case CUDA_GET_DEVICE_LIST:
response_header(CUDA_PKT_PSEUDO, 0);
this->out_buf[2] = (uint8_t)((this->device_mask >> 8) & 0xFF);
this->out_buf[3] = (uint8_t)((this->device_mask) & 0xFF);
break;
case CUDA_ONE_SECOND_MODE:
LOG_F(INFO, "Cuda: One Second Interrupt - Byte Sent: %d", this->in_buf[2]);
response_header(CUDA_PKT_PSEUDO, 0);
break;
case CUDA_READ_WRITE_I2C:
response_header(CUDA_PKT_PSEUDO, 0);
i2c_simple_transaction(this->in_buf[2], &this->in_buf[3], this->in_count - 3);
break;
case CUDA_COMB_FMT_I2C:
response_header(CUDA_PKT_PSEUDO, 0);
if (this->in_count >= 5) {
i2c_comb_transaction(
this->in_buf[2], this->in_buf[3], this->in_buf[4], &this->in_buf[5], this->in_count - 5);
}
break;
case CUDA_OUT_PB0: /* undocumented call! */
LOG_F(INFO, "Cuda: send %d to PB0", (int)(this->in_buf[2]));
response_header(CUDA_PKT_PSEUDO, 0);
break;
case CUDA_WARM_START:
case CUDA_POWER_DOWN:
case CUDA_MONO_STABLE_RESET:
case CUDA_RESTART_SYSTEM:
/* really kludge temp code */
LOG_F(INFO, "Cuda: Restart/Shutdown signal sent with command 0x%x!", cmd);
//exit(0);
break;
default:
LOG_F(ERROR, "Cuda: unsupported pseudo command 0x%X", cmd);
error_response(CUDA_ERR_BAD_CMD);
}
}
/* sends data from the current I2C to host ad infinitum */
void ViaCuda::i2c_handler() {
this->receive_byte(this->curr_i2c_addr, &this->via_regs[VIA_SR]);
}
void ViaCuda::i2c_simple_transaction(uint8_t dev_addr, const uint8_t* in_buf, int in_bytes) {
int op_type = dev_addr & 1; /* 0 - write to device, 1 - read from device */
dev_addr >>= 1; /* strip RD/WR bit */
if (!this->start_transaction(dev_addr)) {
LOG_F(WARNING, "Unsupported I2C device 0x%X", dev_addr);
error_response(CUDA_ERR_I2C);
return;
}
/* send data to the target I2C device until there is no more data to send
or the target device doesn't acknowledge that indicates an error */
for (int i = 0; i < in_bytes; i++) {
if (!this->send_byte(dev_addr, in_buf[i])) {
LOG_F(WARNING, "NO_ACK during sending, device 0x%X", dev_addr);
error_response(CUDA_ERR_I2C);
return;
}
}
if (op_type) { /* read request initiate an open ended transaction */
this->curr_i2c_addr = dev_addr;
this->out_handler = &ViaCuda::out_buf_handler;
this->next_out_handler = &ViaCuda::i2c_handler;
this->is_open_ended = true;
}
}
void ViaCuda::i2c_comb_transaction(
uint8_t dev_addr, uint8_t sub_addr, uint8_t dev_addr1, const uint8_t* in_buf, int in_bytes) {
int op_type = dev_addr1 & 1; /* 0 - write to device, 1 - read from device */
if ((dev_addr & 0xFE) != (dev_addr1 & 0xFE)) {
LOG_F(ERROR, "Combined I2C: dev_addr mismatch!");
error_response(CUDA_ERR_I2C);
return;
}
dev_addr >>= 1; /* strip RD/WR bit */
if (!this->start_transaction(dev_addr)) {
LOG_F(WARNING, "Unsupported I2C device 0x%X", dev_addr);
error_response(CUDA_ERR_I2C);
return;
}
if (!this->send_subaddress(dev_addr, sub_addr)) {
LOG_F(WARNING, "NO_ACK while sending subaddress, device 0x%X", dev_addr);
error_response(CUDA_ERR_I2C);
return;
}
/* send data to the target I2C device until there is no more data to send
or the target device doesn't acknowledge that indicates an error */
for (int i = 0; i < in_bytes; i++) {
if (!this->send_byte(dev_addr, in_buf[i])) {
LOG_F(WARNING, "NO_ACK during sending, device 0x%X", dev_addr);
error_response(CUDA_ERR_I2C);
return;
}
}
if (!op_type) { /* return dummy response for writes */
LOG_F(WARNING, "Combined I2C - write request!");
} else {
this->curr_i2c_addr = dev_addr;
this->out_handler = &ViaCuda::out_buf_handler;
this->next_out_handler = &ViaCuda::i2c_handler;
this->is_open_ended = true;
}
}
static const DeviceDescription ViaCuda_Descriptor = {
ViaCuda::create, {}, {}
};
REGISTER_DEVICE(ViaCuda, ViaCuda_Descriptor);
+123 -32
View File
@@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@@ -21,12 +21,10 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
/** VIA-CUDA combo device emulation.
Author: Max Poliakovski 2019
Versatile interface adapter (VIA) is an old I/O controller that can be found
in nearly every Macintosh computer. In the 68k era, VIA was used to control
various peripherial devices. In a Power Macintosh, its function is limited
to the I/O interface for the Cuda MCU. I therefore decided to put VIA
various peripheral devices. In a Power Macintosh, its function is limited
to the I/O interface with the Cuda MCU. I therefore decided to put VIA
emulation code here.
Cuda MCU is a multipurpose IC built around a custom version of the Motorola
@@ -45,10 +43,15 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#ifndef VIACUDA_H
#define VIACUDA_H
#include "adb.h"
#include "hwcomponent.h"
#include "i2c.h"
#include "nvram.h"
#include <devices/common/adb/adb.h>
#include <devices/common/hwcomponent.h>
#include <devices/common/hwinterrupt.h>
#include <devices/common/i2c/i2c.h>
#include <devices/common/nvram.h>
#include <memory>
#define VIA_CLOCK_HZ 783360
/** VIA register offsets. */
enum {
@@ -70,6 +73,24 @@ enum {
VIA_ANH = 0x0F, /* input/output register A, no handshake */
};
/** VIA interrupt flags */
enum {
VIA_IF_CA2 = 1 << 0,
VIA_IF_CA1 = 1 << 1,
VIA_IF_SR = 1 << 2,
VIA_IF_CB2 = 1 << 3,
VIA_IF_CB1 = 1 << 4,
VIA_IF_T2 = 1 << 5,
VIA_IF_T1 = 1 << 6
};
enum class ViaLine {
CA1,
CA2,
CB1,
CB2
};
/** Cuda communication signals. */
enum {
CUDA_TIP = 0x20, /* transaction in progress: 0 - true, 1 - false */
@@ -88,11 +109,23 @@ enum {
/** Cuda pseudo commands. */
enum {
CUDA_WARM_START = 0x00, /* warm start */
CUDA_START_STOP_AUTOPOLL = 0x01, /* start/stop device auto-polling */
CUDA_READ_MCU_MEM = 0x02, /* read internal Cuda memory */
CUDA_GET_REAL_TIME = 0x03, /* get real time */
CUDA_READ_PRAM = 0x07, /* read parameter RAM */
CUDA_WRITE_MCU_MEM = 0x08, /* write internal Cuda memory */
CUDA_SET_REAL_TIME = 0x09, /* set real time */
CUDA_POWER_DOWN = 0x0A, /* power down system */
CUDA_WRITE_PRAM = 0x0C, /* write parameter RAM */
CUDA_MONO_STABLE_RESET = 0x0D, /* mono stable reset */
CUDA_RESTART_SYSTEM = 0x11, /* restart system */
CUDA_FILE_SERVER_FLAG = 0x13, /* set file server flag */
CUDA_SET_AUTOPOLL_RATE = 0x14, /* set auto-polling rate */
CUDA_GET_AUTOPOLL_RATE = 0x16, /* get auto-polling rate */
CUDA_SET_DEVICE_LIST = 0x19, /* set device list */
CUDA_GET_DEVICE_LIST = 0x1A, /* get device list */
CUDA_ONE_SECOND_MODE = 0x1B, /* one second interrupt mode */
CUDA_READ_WRITE_I2C = 0x22, /* read/write I2C device */
CUDA_COMB_FMT_I2C = 0x25, /* combined format I2C transaction */
CUDA_OUT_PB0 = 0x26, /* output one bit to Cuda's PB0 line */
@@ -103,59 +136,117 @@ enum {
CUDA_ERR_BAD_PKT = 1, /* invalid packet type */
CUDA_ERR_BAD_CMD = 2, /* invalid pseudo command */
CUDA_ERR_BAD_SIZE = 3, /* invalid packet size */
CUDA_ERR_BAD_PAR = 4, /* invalid parameter */
CUDA_ERR_I2C = 5 /* invalid I2C data or no acknowledge */
};
#define CUDA_IN_BUF_SIZE 256
/** PRAM addresses within Cuda's internal memory */
#define CUDA_PRAM_START 0x100 // starting address of PRAM
#define CUDA_PRAM_END 0x1FF // last byte of PRAM
#define CUDA_ROM_START 0xF00 // starting address of ROM containing Cuda FW
/** Latest Cuda Firmware version. */
#define CUDA_FW_VERSION_MAJOR 0x0002
#define CUDA_FW_VERSION_MINOR 0x0029
class ViaCuda : public HWComponent, public I2CBus {
public:
ViaCuda();
~ViaCuda();
~ViaCuda() = default;
bool supports_type(HWCompType type) {
return (type == HWCompType::ADB_HOST || type == HWCompType::I2C_HOST);
};
static std::unique_ptr<HWComponent> create() {
return std::unique_ptr<ViaCuda>(new ViaCuda());
}
// HWComponent methods
int device_postinit();
uint8_t read(int reg);
void write(int reg, uint8_t value);
void assert_ctrl_line(ViaLine line);
#ifdef DEBUG_CPU_INT
void assert_int(uint8_t flags);
#endif
private:
uint8_t via_regs[16]; /* VIA virtual registers */
// VIA virtual HW registers
uint8_t via_regs[16];
float via_clk_dur; // one VIA clock duration = 1,27655 us
// VIA internal state
uint32_t sr_timer_id = 0;
bool sr_timer_on = false;
// timer 1 state
bool t1_active;
uint16_t t1_counter;
uint32_t t1_timer_id = 0;
uint64_t t1_start_time = 0;
// timer 2 state
bool t2_active;
uint16_t t2_counter;
uint32_t t2_timer_id = 0;
uint64_t t2_start_time = 0;
// VIA interrupt related stuff
InterruptCtrl* int_ctrl;
uint32_t irq_id;
uint8_t _via_ifr;
uint8_t _via_ier;
uint8_t old_ifr = 0;
/* Cuda state. */
uint8_t old_tip;
uint8_t old_byteack;
uint8_t treq;
uint8_t in_buf[16];
int32_t in_count;
uint8_t out_buf[16];
int32_t out_count;
int32_t out_pos;
uint8_t poll_rate;
uint8_t old_tip;
uint8_t old_byteack;
uint8_t treq;
uint8_t in_buf[CUDA_IN_BUF_SIZE];
int32_t in_count;
uint8_t out_buf[16];
int32_t out_count;
int32_t out_pos;
uint8_t poll_rate;
int32_t real_time = 0;
bool file_server;
uint16_t device_mask = 0;
bool is_open_ended;
uint8_t curr_i2c_addr;
bool is_open_ended; // true if current transaction is open-ended
uint8_t curr_i2c_addr; // current I2C address
uint8_t cur_pram_addr; // current PRAM address, range 0...FF
void (ViaCuda::*out_handler)(void);
void (ViaCuda::*next_out_handler)(void);
NVram* pram_obj;
ADB_Bus* adb_obj;
std::unique_ptr<NVram> pram_obj;
std::unique_ptr<ADB_Bus> adb_bus;
void print_enabled_ints(); /* print enabled VIA interrupts and their sources */
void init();
bool ready();
// VIA methods
void print_enabled_ints(); // print enabled VIA interrupts and their sources
void init_ints();
void update_irq();
void assert_sr_int();
void assert_t1_int();
void assert_t2_int();
void schedule_sr_int(uint64_t timeout_ns);
uint16_t calc_counter_val(const uint16_t last_val, const uint64_t& last_time);
// CUDA methods
void cuda_init();
bool ready();
void write(uint8_t new_state);
void response_header(uint32_t pkt_type, uint32_t pkt_flag);
// void cuda_response_packet();
void error_response(uint32_t error);
void process_packet();
void process_adb_command(uint8_t cmd_byte, int data_count);
void pseudo_command(int cmd, int data_count);
void null_out_handler(void);
void pram_out_handler(void);
void out_buf_handler(void);
void i2c_handler(void);
+69
View File
@@ -0,0 +1,69 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** @file Device registry implementation. */
#include <devices/deviceregistry.h>
#include <devices/common/hwcomponent.h>
#include <memory>
#include <string>
bool DeviceRegistry::add(const std::string name, DeviceDescription desc)
{
if (device_registered(name)) {
return false;
}
get_registry()[name] = desc;
return true;
}
bool DeviceRegistry::device_registered(const string dev_name)
{
if (get_registry().find(dev_name) != get_registry().end()) {
return true;
}
return false;
}
unique_ptr<HWComponent> DeviceRegistry::create(const std::string& name)
{
auto it = get_registry().find(name);
if (it != get_registry().end()) {
return it->second.m_create_func();
}
return nullptr;
}
DeviceDescription& DeviceRegistry::get_descriptor(const std::string& name)
{
auto it = get_registry().find(name);
if (it != get_registry().end()) {
return it->second;
}
static DeviceDescription empty_descriptor = {};
return empty_descriptor;
}
+67
View File
@@ -0,0 +1,67 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** @file Device registry definitions. */
#ifndef DEVICE_REGISTRY_H
#define DEVICE_REGISTRY_H
#include <devices/common/hwcomponent.h>
#include <machines/machineproperties.h>
#include <functional>
#include <memory>
#include <string>
#include <map>
#include <vector>
typedef std::function<unique_ptr<HWComponent> ()> CreateFunc;
struct DeviceDescription {
CreateFunc m_create_func;
vector<string> subdev_list;
PropMap properties;
};
class DeviceRegistry
{
public:
DeviceRegistry() = delete;
static bool add(const string name, DeviceDescription desc);
static bool device_registered(const string dev_name);
static unique_ptr<HWComponent> create(const string& name);
static DeviceDescription& get_descriptor(const std::string& name);
private:
static map<string, DeviceDescription> & get_registry() {
static map<string, DeviceDescription> device_registry;
return device_registry;
}
};
#define REGISTER_DEVICE(dev_name, dev_desc) \
static bool dev_name ## _registered = DeviceRegistry::add(#dev_name, (dev_desc))
#endif // DEVICE_REGISTRY_H
+64
View File
@@ -0,0 +1,64 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** @file Media Access Controller for Ethernet (MACE) emulation. */
#include <devices/deviceregistry.h>
#include <devices/ethernet/mace.h>
#include <loguru.hpp>
#include <cinttypes>
using namespace MaceEnet;
uint8_t MaceController::read(uint8_t reg_offset)
{
switch(reg_offset) {
case MaceReg::Interrupt:
LOG_F(INFO, "MACE: all interrupt flags cleared");
return 0;
default:
LOG_F(INFO, "Reading MACE register %d", reg_offset);
}
return 0;
}
void MaceController::write(uint8_t reg_offset, uint8_t value)
{
switch(reg_offset) {
case MaceReg::BIU_Config_Ctrl:
if (value & 1) {
LOG_F(INFO, "MACE Reset issued");
} else {
LOG_F(INFO, "MACE BIU Config set to 0x%X", value);
}
break;
default:
LOG_F(INFO, "Writing 0x%X to MACE register %d", value, reg_offset);
}
}
static const DeviceDescription Mace_Descriptor = {
MaceController::create, {}, {}
};
REGISTER_DEVICE(Mace, Mace_Descriptor);
+89
View File
@@ -0,0 +1,89 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** @file Media Access Controller for Ethernet (MACE) definitions. */
#ifndef MACE_H
#define MACE_H
#include <devices/common/hwcomponent.h>
#include <cinttypes>
#include <memory>
// MACE Chip ID from AMD datasheet
// TODO: compare with real HW
#define MACE_ID 0x3940
// MACE registers offsets
// Refer to the Am79C940 datasheet for details
namespace MaceEnet {
enum MaceReg : uint8_t {
Rcv_FIFO = 0,
Xmit_FIFO = 1,
Xmit_Frame_Ctrl = 2,
Xmit_Frame_Stat = 3,
Xmit_Retry_Cnt = 4,
Rcv_Frame_Ctrl = 5,
Rcv_Frame_Stat = 6,
FIFO_Frame_Cnt = 7,
Interrupt = 8,
Interrupt_Mask = 9,
Poll = 0x0A,
BIU_Config_Ctrl = 0x0B,
FIFO_Config = 0x0C,
MAC_Config_Ctrl = 0x0D,
PLS_Config_Ctrl = 0x0E,
PHY_Config_Ctrl = 0x0F,
Chip_ID_Lo = 0x10,
Chip_ID_Hi = 0x11,
Addr_Config = 0x12,
Log_Addr_Flt = 0x14,
Phys_Addr = 0x15,
Missed_Pkt_Cnt = 0x18,
Runt_Pkt_Cnt = 0x1A, // not used in Macintosh?
Rcv_Collis_Cnt = 0x1B, // not used in Macintosh?
User_Test = 0x1D,
Rsrvd_Test_1 = 0x1E, // not used in Macintosh?
Rsrvd_Test_2 = 0x1F, // not used in Macintosh?
};
}; // namespace MaceEnet
class MaceController : public HWComponent {
public:
MaceController(uint16_t id) { this->chip_id = id; };
~MaceController() = default;
static std::unique_ptr<HWComponent> create() {
return std::unique_ptr<MaceController>(new MaceController(MACE_ID));
}
// MACE registers access
uint8_t read(uint8_t reg_offset);
void write(uint8_t reg_offset, uint8_t value);
private:
uint16_t chip_id; // per-instance MACE Chip ID
};
#endif // MACE_H
+258
View File
@@ -0,0 +1,258 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-21 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** @file Support for reading and writing of various floppy images. */
#include "floppyimg.h"
#include <loguru.hpp>
#include <memaccess.h>
#include <cinttypes>
#include <cstring>
#include <fstream>
#include <string>
static FlopImgType identify_image(std::ifstream& img_file)
{
// WOZ images identification strings
static uint8_t WOZ1_SIG[] = {0x57, 0x4F, 0x5A, 0x31, 0xFF, 0x0A, 0x0D, 0x0A};
static uint8_t WOZ2_SIG[] = {0x57, 0x4F, 0x5A, 0x32, 0xFF, 0x0A, 0x0D, 0x0A};
uint8_t buf[8] = { 0 };
img_file.seekg(0, std::ios::beg);
img_file.read((char *)buf, sizeof(buf));
// WOZ files are easily identified
if (!std::memcmp(buf, WOZ1_SIG, sizeof(buf))) {
return FlopImgType::WOZ1;
} else if (!std::memcmp(buf, WOZ2_SIG, sizeof(buf))) {
return FlopImgType::WOZ2;
} else {
for (int offset = 0; offset <=84; offset += 84) {
// rewind to logical block 2
img_file.seekg(2*BLOCK_SIZE + offset, std::ios::beg);
img_file.read((char *)buf, sizeof(buf));
// check for HFS/MFS signature at the start of the logical block 2
if ((buf[0] == 0x42 && buf[1] == 0x44) ||
(buf[0] == 0xD2 && buf[1] == 0xD7)) {
if (offset) {
return FlopImgType::DC42;
} else {
return FlopImgType::RAW;
}
}
}
}
return FlopImgType::UNKNOWN;
}
static int64_t get_hfs_vol_size(const uint8_t *mdb_data)
{
uint16_t drNmAlBlks = READ_WORD_BE_A(&mdb_data[18]);
uint32_t drAlBlkSiz = READ_DWORD_BE_A(&mdb_data[20]);
// calculate size of the volume bitmap
uint32_t vol_bmp_size = (((drNmAlBlks + 8) >> 3) + 512) & 0xFFFFFE00UL;
return (drNmAlBlks * drAlBlkSiz + vol_bmp_size + 3*BLOCK_SIZE);
}
//======================= RAW IMAGE CONVERTER ============================
RawFloppyImg::RawFloppyImg(std::string& file_path) : FloppyImgConverter()
{
this->img_path = file_path;
}
/** For raw images, we're going to ensure that the data fits into
one of the supported floppy disk sizes as well as image size
matches the size of the embedded HFS/MFS volume.
Then we'll attempt to guess disk format based on image size.
*/
int RawFloppyImg::calc_phys_params()
{
std::ifstream img_file;
img_file.open(img_path, std::ios::in | std::ios::binary);
if (img_file.fail()) {
img_file.close();
LOG_F(ERROR, "RawFloppyImg: Could not open specified floppy image!");
return -1;
}
// determine image size
img_file.seekg(0, img_file.end);
this->img_size = img_file.tellg();
img_file.seekg(0, img_file.beg);
// verify image size
if (this->img_size < 5*BLOCK_SIZE) {
img_file.close();
LOG_F(ERROR, "RawFloppyImg: image too short!");
return -1;
}
if (this->img_size > MFM_HD_SIZE) {
img_file.close();
LOG_F(ERROR, "RawFloppyImg: image too big!");
return -1;
}
// read Master Directory Block from logical block 2
uint8_t buf[512] = { 0 };
img_file.seekg(2*BLOCK_SIZE, img_file.beg);
img_file.read((char *)buf, sizeof(buf));
img_file.close();
uint64_t vol_size = 0;
if (buf[0] == 0x42 && buf[1] == 0x44) {
// check HFS volume size
vol_size = get_hfs_vol_size(buf);
} else if (buf[0] == 0xD2 && buf[1] == 0xD7) {
// check MFS volume size
} else {
LOG_F(ERROR, "RawFloppyImg: unknown volume type!");
return -1;
}
if (vol_size > this->img_size) {
LOG_F(INFO, "RawFloppyImg: volume size > image size!");
LOG_F(INFO, "Volume size: %llu, Image size: %d", vol_size, this->img_size);
return -1;
}
// raw images don't include anything than raw disk data
this->data_size = this->img_size;
// guess disk format from image file size
static struct {
int capacity;
int rec_method;
int num_tracks;
int num_sectors;
int num_sides;
int density;
} size_to_params[] = {
{ 409600, 0, 80, 800, 1, 0}, // 400K GCR
{ 819200, 0, 80, 800, 2, 0}, // 800K GCR
{ 737280, 1, 80, 1440, 2, 0}, // 720K MFM
{1474560, 1, 80, 2880, 2, 1}, // 1440K MFM
};
this->rec_method = -1;
for (int i = 0; i < 4; i++) {
if (this->img_size == size_to_params[i].capacity) {
this->rec_method = size_to_params[i].rec_method;
this->num_tracks = size_to_params[i].num_tracks;
this->num_sectors = size_to_params[i].num_sectors;
this->num_sides = size_to_params[i].num_sides;
this->density = size_to_params[i].density;
// fake format byte for GCR disks
if (!this->rec_method) {
this->format_byte = (this->num_sides == 2) ? 0x22 : 0x2;
} else {
// For MFM disks this byte indicates sector size in blocks
this->format_byte = 2;
}
break;
}
}
if (this->rec_method == -1) {
LOG_F(ERROR, "RawFloppyImg: could't determine disk format from image size!");
return -1;
}
return 0;
}
/** Retrieve raw disk data. */
int RawFloppyImg::get_raw_disk_data(char* buf)
{
std::ifstream img_file;
img_file.open(img_path, std::ios::in | std::ios::binary);
if (img_file.fail()) {
img_file.close();
LOG_F(ERROR, "RawFloppyImg: Could not open specified floppy image!");
return -1;
}
img_file.seekg(0, img_file.beg);
img_file.read(buf, this->data_size);
img_file.close();
return 0;
}
/** Convert low-level disk data to high-level image data. */
int RawFloppyImg::export_data()
{
return 0;
}
FloppyImgConverter* open_floppy_image(std::string& img_path)
{
FloppyImgConverter *fconv;
std::ifstream img_file;
img_file.open(img_path, std::ios::in | std::ios::binary);
if (img_file.fail()) {
img_file.close();
LOG_F(ERROR, "Could not open specified floppy image!");
return nullptr;
}
FlopImgType itype = identify_image(img_file);
img_file.close();
switch(itype) {
case FlopImgType::RAW:
LOG_F(INFO, "Raw floppy image");
fconv = new RawFloppyImg(img_path);
break;
case FlopImgType::DC42:
LOG_F(INFO, "Disk Copy 4.2 image");
break;
case FlopImgType::WOZ1:
case FlopImgType::WOZ2:
LOG_F(INFO, "WOZ v%s image", (itype == FlopImgType::WOZ2) ? "2" : "1");
break;
default:
LOG_F(ERROR, "Unknown/unsupported image format!");
return nullptr;
}
if (fconv->calc_phys_params()) {
delete fconv;
return nullptr;
}
return fconv;
}
+87
View File
@@ -0,0 +1,87 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-21 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** @file Definitions for working with various floppy images. */
#ifndef FLOPPY_IMG_H
#define FLOPPY_IMG_H
#include <cinttypes>
#include <string>
#define BLOCK_SIZE 512 // size in bytes of a logical block
#define MFM_HD_SIZE (BLOCK_SIZE*2880) // maximal size of a high-density floppy
/** Floppy image types. */
enum class FlopImgType {
RAW, // raw image without metadata or low-level formatting
DC42, // Disk Copy 4.2 image
WOZ1, // WOZ v1 image with metadata and low-level data retained
WOZ2, // WOZ v2 image with metadata and low-level data retained
UNKNOWN, // invalid/unknown image format
};
/** Interface for floppy image converters. */
class FloppyImgConverter {
public:
FloppyImgConverter() = default;
virtual ~FloppyImgConverter() = default;
virtual int calc_phys_params(void) = 0;
virtual int get_raw_disk_data(char* buf) = 0;
virtual int export_data(void) = 0;
int get_data_size() { return this->data_size; };
int get_disk_rec_method() { return this->rec_method; };
int get_number_of_tracks() { return this->num_tracks; };
int get_number_of_sides() { return this->num_sides; };
int get_sectors_per_side() { return this->num_sectors; };
int get_rec_density() { return this->density; };
uint8_t get_format_byte() { return this->format_byte; };
protected:
std::string img_path;
int img_size;
int data_size; // disk data size without image format specific stuff
int rec_method;
int num_tracks;
int num_sides;
int num_sectors;
int density;
uint8_t format_byte; // GCR format byte from sector header
};
/** Converter for raw floppy images. */
class RawFloppyImg : public FloppyImgConverter {
public:
RawFloppyImg(std::string& file_path);
~RawFloppyImg() = default;
int calc_phys_params(void);
int get_raw_disk_data(char* buf);
int export_data(void);
};
extern FloppyImgConverter* open_floppy_image(std::string& img_path);
#endif // FLOPPY_IMG_H
+256
View File
@@ -0,0 +1,256 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** @file Macintosh Superdrive emulation. */
#include <devices/floppy/floppyimg.h>
#include <devices/floppy/superdrive.h>
#include <loguru.hpp>
#include <cinttypes>
#include <memory>
using namespace MacSuperdrive;
MacSuperDrive::MacSuperDrive()
{
this->name = "Superdrive";
this->supported_types = HWCompType::FLOPPY_DRV;
this->media_kind = MediaKind::high_density;
this->has_disk = 0; // drive is empty
this->eject_latch = 0; // eject latch is off
this->drive_mode = RecMethod::MFM; // assume MFM mode by default
this->motor_stat = 0; // spindle motor is off
this->cur_track = 0; // current head position
this->is_ready = 0; // drive not ready
}
void MacSuperDrive::command(uint8_t addr, uint8_t value)
{
uint8_t new_motor_stat;
LOG_F(9, "Superdrive: command addr=0x%X, value=%d", addr, value);
switch(addr) {
case CommandAddr::Step_Direction:
this->step_dir = value ? -1 : 1;
break;
case CommandAddr::Do_Step:
if (!value) {
this->cur_track += this->step_dir;
if (this->cur_track < 0)
this->cur_track = 0;
this->track_zero = this->cur_track == 0;
}
break;
case CommandAddr::Motor_On_Off:
new_motor_stat = value ^ 1;
if (this->motor_stat != new_motor_stat) {
this->motor_stat = new_motor_stat;
if (new_motor_stat) {
this->is_ready = 1;
LOG_F(INFO, "Superdrive: turn spindle motor on");
} else {
LOG_F(INFO, "Superdrive: turn spindle motor off");
}
}
break;
case CommandAddr::Reset_Eject_Latch:
if (value) {
this->eject_latch = 0;
}
break;
case CommandAddr::Switch_Drive_Mode:
if (this->drive_mode != (value ^ 1)) {
switch_drive_mode(value ^ 1); // reverse logic
this->is_ready = 1;
}
break;
default:
LOG_F(WARNING, "Superdrive: unimplemented command, addr=0x%X", addr);
}
}
uint8_t MacSuperDrive::status(uint8_t addr)
{
LOG_F(9, "Superdrive: status request, addr = 0x%X", addr);
switch(addr) {
case StatusAddr::Step_Status:
return 1; // not sure what should be returned here
case StatusAddr::Motor_Status:
return this->motor_stat ^ 1; // reverse logic
case StatusAddr::Eject_Latch:
return this->eject_latch;
case StatusAddr::Select_Head_0:
this->cur_head = 0;
return 1; // not sure what should be returned here
case StatusAddr::MFM_Support:
return 1; // Superdrive does support MFM encoding scheme
case StatusAddr::Double_Sided:
return 1; // yes, Superdrive is double sided
case StatusAddr::Drive_Exists:
return 0; // tell the world I'm here
case StatusAddr::Disk_In_Drive:
return this->has_disk ^ 1; // reverse logic (active low)!
case StatusAddr::Write_Protect:
return this->wr_protect ^ 1; // reverse logic
case StatusAddr::Track_Zero:
return this->track_zero ^ 1; // reverse logic
case StatusAddr::Select_Head_1:
this->cur_head = 1;
return 1; // not sure what should be returned here
case StatusAddr::Drive_Mode:
return this->drive_mode;
case StatusAddr::Drive_Ready:
return this->is_ready ^ 1; // reverse logic
case StatusAddr::Media_Kind:
return this->media_kind ^ 1; // reverse logic!
default:
LOG_F(WARNING, "Superdrive: unimplemented status request, addr=0x%X", addr);
return 0;
}
}
int MacSuperDrive::insert_disk(std::string& img_path, int write_flag = 0)
{
if (this->has_disk) {
LOG_F(ERROR, "Superdrive: drive is not empty!");
return -1;
}
FloppyImgConverter* img_conv = open_floppy_image(img_path);
if (img_conv != nullptr) {
this->img_conv = std::unique_ptr<FloppyImgConverter>(img_conv);
set_disk_phys_params();
// allocate memory for raw disk data
this->disk_data = std::unique_ptr<char[]>(new char[this->img_conv->get_data_size()]);
// swallow all raw disk data at once!
this->img_conv->get_raw_disk_data(this->disk_data.get());
// disk is write-enabled by default
this->wr_protect = write_flag;
// everything is set up, let's say we got a disk
this->has_disk = 1;
} else {
this->has_disk = 0;
}
return 0;
}
void MacSuperDrive::set_disk_phys_params()
{
this->rec_method = this->img_conv->get_disk_rec_method();
this->num_tracks = this->img_conv->get_number_of_tracks();
this->num_sides = this->img_conv->get_number_of_sides();
this->media_kind = this->img_conv->get_rec_density();
this->format_byte = this->img_conv->get_format_byte();
switch_drive_mode(this->rec_method);
}
void MacSuperDrive::switch_drive_mode(int mode)
{
if (mode == RecMethod::GCR) {
// Apple GCR speeds per group of 16 tracks
static int gcr_rpm_per_group[5] = {394, 429, 472, 525, 590};
// initialize three lookup tables:
// sectors per track
// rpm per track
// logical block number of the first sector in each track
// for easier navigation
for (int grp = 0, blk_num = 0; grp < 5; grp++) {
for (int trk = 0; trk < 16; trk++) {
this->sectors_per_track[grp * 16 + trk] = 12 - grp;
this->rpm_per_track[grp * 16 + trk] = gcr_rpm_per_group[grp];
this->track2lblk[grp * 16 + trk] = blk_num;
blk_num += (12 - grp) * this->num_sides;
}
}
this->drive_mode = RecMethod::GCR;
} else {
int sectors_per_track = this->media_kind ? 18 : 9;
// MFM disks use constant number of sectors per track
// and the fixed rotational speed of 300 RPM
for (int trk = 0; trk < 80; trk++) {
this->sectors_per_track[trk] = sectors_per_track;
this->rpm_per_track[trk] = 300;
this->track2lblk[trk] = trk * sectors_per_track * 2;
}
this->drive_mode = RecMethod::MFM;
}
}
double MacSuperDrive::get_current_track_delay()
{
return (1.0f / (this->rpm_per_track[this->cur_track] / 60));
}
double MacSuperDrive::get_sector_delay()
{
return this->get_current_track_delay() / this->sectors_per_track[this->cur_track];
}
void MacSuperDrive::init_track_search(int pos)
{
if (pos == -1) {
// pick random sector number
this->cur_sector = this->sectors_per_track[this->cur_track] / 2;
} else {
this->cur_sector = pos;
}
}
SectorHdr MacSuperDrive::next_sector_header()
{
this->cur_sector++;
if (this->cur_sector >= this->sectors_per_track[this->cur_track]) {
this->cur_sector = 0;
}
// MFM sector numbering is 1-based so we need to bump sector number
int sector_num = this->cur_sector + ((this->rec_method == RecMethod::MFM) ? 1 : 0);
return SectorHdr {
this->cur_track,
this->cur_head,
sector_num,
this->format_byte
};
}
char* MacSuperDrive::get_sector_data_ptr(int sector_num)
{
return this->disk_data.get() +
((this->track2lblk[this->cur_track] +
(this->cur_head * this->sectors_per_track[this->cur_track]) +
sector_num - 1) * 512
);
}
+130
View File
@@ -0,0 +1,130 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** @file Macintosh Superdrive definitions. */
#ifndef MAC_SUPERDRIVE_H
#define MAC_SUPERDRIVE_H
#include <devices/common/hwcomponent.h>
#include <devices/floppy/floppyimg.h>
#include <cinttypes>
#include <memory>
#include <string>
namespace MacSuperdrive {
/** Apple Drive status request addresses. */
enum StatusAddr : uint8_t {
Step_Status = 1,
Motor_Status = 2,
Eject_Latch = 3,
Select_Head_0 = 4,
MFM_Support = 5,
Double_Sided = 6,
Drive_Exists = 7,
Disk_In_Drive = 8,
Write_Protect = 9,
Track_Zero = 0xA,
Select_Head_1 = 0xC,
Drive_Mode = 0xD,
Drive_Ready = 0xE,
Media_Kind = 0xF
};
/** Apple Drive command addresses. */
enum CommandAddr : uint8_t {
Step_Direction = 0,
Do_Step = 1,
Motor_On_Off = 2,
Reset_Eject_Latch = 4,
Switch_Drive_Mode = 5,
};
/** Type of media currently in the drive. */
enum MediaKind : uint8_t {
low_density = 0,
high_density = 1, // 1 or 2 MB disk
};
/** Disk recording method. */
enum RecMethod : int {
GCR = 0,
MFM = 1
};
typedef struct SectorHdr {
int track;
int side;
int sector;
int format;
} SectorHdr;
class MacSuperDrive : public HWComponent {
public:
MacSuperDrive();
~MacSuperDrive() = default;
void command(uint8_t addr, uint8_t value);
uint8_t status(uint8_t addr);
int insert_disk(std::string& img_path, int write_flag);
double get_current_track_delay();
double get_sector_delay();
void init_track_search(int pos);
SectorHdr next_sector_header();
char* get_sector_data_ptr(int sector_num);
protected:
void set_disk_phys_params();
void switch_drive_mode(int mode);
private:
uint8_t has_disk;
uint8_t eject_latch;
uint8_t motor_stat; // spindle motor status: 1 - on, 0 - off
uint8_t drive_mode; // drive mode: 0 - GCR, 1 - MFM
uint8_t is_ready;
uint8_t track_zero; // 1 - if head is at track zero
int step_dir; // step direction -1/+1
int cur_track; // track number the head is currently at
int cur_head; // current head number: 1 - upper, 0 - lower
int cur_sector; // current sector number
// physical parameters of the currently inserted disk
uint8_t media_kind;
uint8_t wr_protect;
uint8_t format_byte;
int rec_method;
int num_tracks;
int num_sides;
int sectors_per_track[80];
int rpm_per_track[80];
int track2lblk[80]; // convert track number to first logical block number
std::unique_ptr<FloppyImgConverter> img_conv;
std::unique_ptr<char[]> disk_data;
};
}; // namespace MacSuperdrive
#endif // MAC_SUPERDRIVE_H
+349
View File
@@ -0,0 +1,349 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** @file Sander-Wozniak Machine 3 (SWIM3) emulation. */
#include <core/timermanager.h>
#include <devices/deviceregistry.h>
#include <devices/common/hwinterrupt.h>
#include <devices/floppy/superdrive.h>
#include <devices/floppy/swim3.h>
#include <loguru.hpp>
#include <machines/machinebase.h>
#include <machines/machineproperties.h>
#include <cinttypes>
#include <memory>
#include <string>
using namespace Swim3;
Swim3Ctrl::Swim3Ctrl()
{
this->name = "SWIM3";
this->supported_types = HWCompType::FLOPPY_CTRL;
this->setup_reg = 0;
this->mode_reg = 0;
this->int_reg = 0;
this->int_flags = 0;
this->int_mask = 0;
this->error = 0;
this->xfer_cnt = 0;
this->first_sec = 0xFF;
// Attach virtual Superdrive to the internal drive connector
// TODO: make SWIM3/drive wiring user selectable
this->int_drive = std::unique_ptr<MacSuperdrive::MacSuperDrive>
(new MacSuperdrive::MacSuperDrive());
}
int Swim3Ctrl::device_postinit()
{
this->int_ctrl = dynamic_cast<InterruptCtrl*>(
gMachineObj->get_comp_by_type(HWCompType::INT_CTRL));
this->irq_id = this->int_ctrl->register_dev_int(IntSrc::SWIM3);
// if a floppy image was given "insert" it into the virtual superdrive
std::string fd_image_path = GET_STR_PROP("fdd_img");
int fd_write_prot = GET_BIN_PROP("fdd_wr_prot");
if (!fd_image_path.empty()) {
this->int_drive->insert_disk(fd_image_path, fd_write_prot);
}
return 0;
};
uint8_t Swim3Ctrl::read(uint8_t reg_offset)
{
uint8_t status_addr, status_val, old_int_flags, old_error;
switch(reg_offset) {
case Swim3Reg::Error:
old_error = this->error;
this->error = 0;
return old_error;
case Swim3Reg::Phase:
return this->phase_lines;
case Swim3Reg::Setup:
return this->setup_reg;
case Swim3Reg::Handshake_Mode1:
if (this->mode_reg & 2) { // internal drive?
status_addr = ((this->mode_reg & 0x20) >> 2) | (this->phase_lines & 7);
status_val = this->int_drive->status(status_addr) & 1;
// transfer status_val to both bit 2 (RDDATA) and bit 3 (SENSE)
// because those signals seem to be historically wired together
return ((status_val << 2) | (status_val << 3));
}
return 4;
case Swim3Reg::Interrupt_Flags:
old_int_flags = this->int_flags;
this->int_flags = 0; // read from this register clears all flags
update_irq();
return old_int_flags;
case Swim3Reg::Current_Track:
return this->cur_track;
case Swim3Reg::Current_Sector:
return this->cur_sector;
case Swim3Reg::Gap_Format:
return this->format;
case Swim3Reg::First_Sector:
return this->first_sec;
case Swim3Reg::Sectors_To_Xfer:
return this->xfer_cnt;
case Swim3Reg::Interrupt_Mask:
return this->int_mask;
default:
LOG_F(INFO, "SWIM3: reading from 0x%X register", reg_offset);
}
return 0;
}
void Swim3Ctrl::write(uint8_t reg_offset, uint8_t value)
{
uint8_t old_mode_reg;
switch(reg_offset) {
case Swim3Reg::Param_Data:
this->pram = value;
break;
case Swim3Reg::Phase:
this->phase_lines = value & 0xF;
if (value & 8) {
if (this->mode_reg & 2) { // internal drive?
this->int_drive->command(
((this->mode_reg & 0x20) >> 3) | (this->phase_lines & 3),
(value >> 2) & 1
);
}
}
break;
case Swim3Reg::Setup:
this->setup_reg = value;
break;
case Swim3Reg::Status_Mode0:
// ones in value clear the corresponding bits in the mode register
if ((this->mode_reg & value) & (SWIM3_GO | SWIM3_GO_STEP)) {
if (value & SWIM3_GO_STEP) {
stop_stepping();
} else {
stop_disk_access();
}
}
this->mode_reg &= ~value;
break;
case Swim3Reg::Handshake_Mode1:
// ones in value set the corresponding bits in the mode register
if ((this->mode_reg ^ value) & (SWIM3_GO | SWIM3_GO_STEP)) {
if (value & SWIM3_GO_STEP) {
start_stepping();
} else {
start_disk_access();
}
}
this->mode_reg |= value;
break;
case Swim3Reg::Step:
this->step_count = value;
break;
case Swim3Reg::Gap_Format:
this->gap_size = value;
break;
case Swim3Reg::First_Sector:
this->first_sec = value;
break;
case Swim3Reg::Sectors_To_Xfer:
this->xfer_cnt = value;
break;
case Swim3Reg::Interrupt_Mask:
this->int_mask = value;
break;
default:
LOG_F(INFO, "SWIM3: writing 0x%X to register 0x%X", value, reg_offset);
}
}
void Swim3Ctrl::update_irq()
{
if (this->mode_reg & SWIM3_INT_ENA) {
uint8_t new_irq = !!(this->int_flags & this->int_mask);
if (new_irq != this->irq) {
this->irq = new_irq;
this->int_ctrl->ack_int(this->irq_id, new_irq);
}
}
}
void Swim3Ctrl::do_step()
{
if (this->mode_reg & SWIM3_GO_STEP && this->step_count) { // are we still stepping?
// instruct the drive to perform single step in current direction
this->int_drive->command(MacSuperdrive::CommandAddr::Do_Step, 0);
if (--this->step_count == 0) {
if (this->step_timer_id) {
this->stop_stepping();
}
this->int_flags |= INT_STEP_DONE;
update_irq();
}
}
}
void Swim3Ctrl::start_stepping()
{
if (!this->step_count) {
LOG_F(WARNING, "SWIM3: step_count is zero while go_step is active!");
return;
}
if (this->mode_reg & SWIM3_GO_STEP || this->step_timer_id) {
LOG_F(ERROR, "SWIM3: another stepping action is running!");
return;
}
if (this->mode_reg & SWIM3_GO || this->access_timer_id) {
LOG_F(ERROR, "SWIM3: stepping attempt while disk access is in progress!");
return;
}
if ((((this->mode_reg & 0x20) >> 3) | (this->phase_lines & 3))
!= MacSuperdrive::CommandAddr::Do_Step) {
LOG_F(WARNING, "SWIM3: invalid command address on the phase lines!");
return;
}
this->mode_reg |= SWIM3_GO_STEP;
// step count > 1 requires periodic task
if (this->step_count > 1) {
this->step_timer_id = TimerManager::get_instance()->add_cyclic_timer(
USECS_TO_NSECS(80),
[this]() {
this->do_step();
}
);
}
// perform the first step immediately
do_step();
}
void Swim3Ctrl::stop_stepping()
{
// cancel stepping task
if (this->step_timer_id) {
TimerManager::get_instance()->cancel_timer(this->step_timer_id);
}
this->step_timer_id = 0;
this->step_count = 0; // not sure this one is required
}
void Swim3Ctrl::start_disk_access()
{
if (this->mode_reg & SWIM3_GO || this->access_timer_id) {
LOG_F(ERROR, "SWIM3: another disk access is running!");
return;
}
if (this->mode_reg & SWIM3_GO_STEP || this->step_timer_id) {
LOG_F(ERROR, "SWIM3: disk access attempt while stepping is in progress!");
return;
}
if (this->mode_reg & SWIM3_WR_MODE) {
LOG_F(ERROR, "SWIM3: writing not implemented yet");
return;
}
this->mode_reg |= SWIM3_GO;
LOG_F(9, "SWIM3: disk access started!");
if (this->first_sec == 0xFF) {
// $FF means no sector to match ->
// generate ID_read interrups as long as the GO bit is set
this->int_drive->init_track_search(-1); // start at random sector
} else {
this->cur_sector = this->first_sec;
}
// HACK: figure out from bits in int_mask register which kind of disk access is requested
if (this->int_mask & INT_ID_READ) { // read address header
this->access_timer_id = TimerManager::get_instance()->add_cyclic_timer(
static_cast<uint64_t>(this->int_drive->get_sector_delay() * NS_PER_SEC + 0.5f),
[this]() {
// get next sector's address field
MacSuperdrive::SectorHdr addr = this->int_drive->next_sector_header();
// set up the corresponding SWIM3 registers
this->cur_track = ((addr.side & 1) << 7) | (addr.track & 0x7F);
this->cur_sector = 0x80 /* CRC/checksum valid */ | (addr.sector & 0x7F);
this->format = addr.format;
// generate ID_read interrupt
this->int_flags |= INT_ID_READ;
update_irq();
}
);
} else { // otherwise, read sector data
this->access_timer_id = TimerManager::get_instance()->add_cyclic_timer(
static_cast<uint64_t>(this->int_drive->get_sector_delay() * NS_PER_SEC + 0.5f),
[this]() {
// transfer sector data over DMA
this->dma_ch->push_data(this->int_drive->get_sector_data_ptr(this->cur_sector), 512);
// get next address field
MacSuperdrive::SectorHdr addr = this->int_drive->next_sector_header();
// set up the corresponding SWIM3 registers
this->cur_track = ((addr.side & 1) << 7) | (addr.track & 0x7F);
this->cur_sector = 0x80 /* CRC/checksum valid */ | (addr.sector & 0x7F);
this->format = addr.format;
if (--this->xfer_cnt == 0) {
this->stop_disk_access();
// generate sector_done interrupt
this->int_flags |= INT_SECT_DONE;
update_irq();
}
}
);
}
}
void Swim3Ctrl::stop_disk_access()
{
// cancel disk access timer
if (this->access_timer_id) {
TimerManager::get_instance()->cancel_timer(this->access_timer_id);
}
this->access_timer_id = 0;
}
static const PropMap Swim3_Properties = {
{"fdd_img",
new StrProperty("")},
{"fdd_wr_prot",
new BinProperty(0)},
};
static const DeviceDescription Swim3_Descriptor = {
Swim3Ctrl::create, {}, Swim3_Properties
};
REGISTER_DEVICE(Swim3, Swim3_Descriptor);
+131
View File
@@ -0,0 +1,131 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-21 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** @file Sander-Wozniak Machine 3 (SWIM3) definitions. */
#ifndef SWIM3_H
#define SWIM3_H
#include <devices/common/dmacore.h>
#include <devices/common/hwcomponent.h>
#include <devices/common/hwinterrupt.h>
#include <devices/floppy/superdrive.h>
#include <cinttypes>
#include <memory>
/** SWIM3 registers offsets. */
namespace Swim3 {
enum Swim3Reg : uint8_t {
Data = 0,
Timer = 1,
Error = 2,
Param_Data = 3,
Phase = 4,
Setup = 5,
Status_Mode0 = 6, // read: Status, write: zeroes to the mode register
Handshake_Mode1 = 7, // read: Handshake, write: ones to the mode register
Interrupt_Flags = 8,
Step = 9,
Current_Track = 10,
Current_Sector = 11,
Gap_Format = 12,
First_Sector = 13,
Sectors_To_Xfer = 14,
Interrupt_Mask = 15
};
/** Mode register bits. */
enum {
SWIM3_INT_ENA = 0x01,
SWIM3_GO = 0x08,
SWIM3_WR_MODE = 0x10,
SWIM3_GO_STEP = 0x80,
};
/** Interrupt flags. */
enum {
INT_STEP_DONE = 0x02,
INT_ID_READ = 0x04,
INT_SECT_DONE = 0x08,
};
class Swim3Ctrl : public HWComponent {
public:
Swim3Ctrl();
~Swim3Ctrl() = default;
static std::unique_ptr<HWComponent> create() {
return std::unique_ptr<Swim3Ctrl>(new Swim3Ctrl());
}
int device_postinit();
// SWIM3 registers access
uint8_t read(uint8_t reg_offset);
void write(uint8_t reg_offset, uint8_t value);
void set_dma_channel(DmaBidirChannel *dma_ch) {
this->dma_ch = dma_ch;
};
protected:
void update_irq();
void start_stepping();
void do_step();
void stop_stepping();
void start_disk_access();
void stop_disk_access();
private:
std::unique_ptr<MacSuperdrive::MacSuperDrive> int_drive;
DmaBidirChannel* dma_ch;
uint8_t setup_reg;
uint8_t mode_reg;
uint8_t error;
uint8_t phase_lines;
uint8_t int_reg;
uint8_t int_flags; // interrupt flags
uint8_t int_mask;
uint8_t pram; // parameter RAM: two nibbles = {late_time, early_time}
uint8_t step_count;
uint8_t cur_track;
uint8_t cur_sector;
uint8_t format; // format byte from the last GCR/MFM address field
uint8_t first_sec;
uint8_t xfer_cnt;
uint8_t gap_size;
int step_timer_id = 0;
int access_timer_id = 0;
// Interrupt related stuff
InterruptCtrl* int_ctrl = nullptr;
uint32_t irq_id = 0;
uint8_t irq = 0;
};
}; // namespace Swim3
#endif // SWIM3_H
-273
View File
@@ -1,273 +0,0 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-21 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "awacs.h"
#include "dbdma.h"
#include "machines/machinebase.h"
#include "macio.h"
#include "viacuda.h"
#include <cinttypes>
#include <functional>
#include <iostream>
#include <loguru.hpp>
#include <cpu/ppc/ppcemu.h>
/** Heathrow Mac I/O device emulation.
Author: Max Poliakovski
*/
using namespace std;
HeathrowIC::HeathrowIC() : PCIDevice("mac-io/heathrow") {
this->nvram = new NVram();
this->viacuda = new ViaCuda();
gMachineObj->add_subdevice("ViaCuda", this->viacuda);
// initialize sound chip and its DMA output channel, then wire them together
this->screamer = new AwacsScreamer();
this->snd_out_dma = new DMAChannel();
this->screamer->set_dma_out(this->snd_out_dma);
this->snd_out_dma->set_callbacks(
std::bind(&AwacsScreamer::dma_start, this->screamer),
std::bind(&AwacsScreamer::dma_end, this->screamer)
);
this->mesh = new MESHController(HeathrowMESHID);
}
HeathrowIC::~HeathrowIC() {
if (this->nvram)
delete (this->nvram);
if (this->viacuda)
delete (this->viacuda);
if (this->mesh)
delete (this->mesh);
}
uint32_t HeathrowIC::pci_cfg_read(uint32_t reg_offs, uint32_t size) {
return this->pci_cfg_hdr[reg_offs & 0xFF];
}
void HeathrowIC::pci_cfg_write(uint32_t reg_offs, uint32_t value, uint32_t size) {
switch (reg_offs) {
case CFG_REG_BAR0: // base address register
value = LE2BE(value);
if (value == 0xFFFFFFFF) {
LOG_F(
ERROR,
"%s err: BAR0 block size determination not \
implemented yet \n",
this->name.c_str());
} else if (value & 1) {
LOG_F(ERROR, "%s err: BAR0 I/O space not supported! \n", this->name.c_str());
} else if (value & 0x06) {
LOG_F(ERROR, "%s err: BAR0 64-bit I/O space not supported! \n", this->name.c_str());
} else {
this->base_addr = value & 0xFFF80000;
this->host_instance->pci_register_mmio_region(this->base_addr, 0x80000, this);
LOG_F(INFO, "%s base address set to %x \n", this->name.c_str(), this->base_addr);
}
break;
}
}
uint32_t HeathrowIC::dma_read(uint32_t offset, int size) {
uint32_t res = 0;
switch (offset >> 8) {
case 8:
res = this->snd_out_dma->reg_read(offset & 0xFF, size);
break;
default:
LOG_F(WARNING, "Unsupported DMA channel read, offset=0x%X", offset);
}
return res;
}
void HeathrowIC::dma_write(uint32_t offset, uint32_t value, int size) {
switch (offset >> 8) {
case 8:
this->snd_out_dma->reg_write(offset & 0xFF, value, size);
break;
default:
LOG_F(WARNING, "Unsupported DMA channel write, offset=0x%X, val=0x%X", offset, value);
}
}
uint32_t HeathrowIC::read(uint32_t reg_start, uint32_t offset, int size) {
uint32_t res = 0;
LOG_F(9, "%s: reading from offset %x \n", this->name.c_str(), offset);
unsigned sub_addr = (offset >> 12) & 0x7F;
switch (sub_addr) {
case 0:
res = mio_ctrl_read(offset, size);
break;
case 8:
res = dma_read(offset - 0x8000, size);
break;
case 0x10:
res = this->mesh->read((offset >> 4) & 0xF);
break;
case 0x14:
res = this->screamer->snd_ctrl_read(offset - 0x14000, size);
break;
case 0x16:
case 0x17:
res = this->viacuda->read((offset - 0x16000) >> 9);
break;
default:
if (sub_addr >= 0x60) {
res = this->nvram->read_byte((offset - 0x60000) >> 4);
} else {
LOG_F(WARNING, "Attempting to read unmapped I/O space: %x \n", offset);
}
}
return res;
}
void HeathrowIC::write(uint32_t reg_start, uint32_t offset, uint32_t value, int size) {
LOG_F(9, "%s: writing to offset %x \n", this->name.c_str(), offset);
unsigned sub_addr = (offset >> 12) & 0x7F;
switch (sub_addr) {
case 0:
mio_ctrl_write(offset, value, size);
break;
case 8:
dma_write(offset - 0x8000, value, size);
break;
case 0x10:
this->mesh->write((offset >> 4) & 0xF, value);
break;
case 0x14:
this->screamer->snd_ctrl_write(offset - 0x14000, value, size);
break;
case 0x16:
case 0x17:
this->viacuda->write((offset - 0x16000) >> 9, value);
break;
default:
if (sub_addr >= 0x60) {
this->nvram->write_byte((offset - 0x60000) >> 4, value);
} else {
LOG_F(WARNING, "Attempting to write to unmapped I/O space: %x \n", offset);
}
}
}
uint32_t HeathrowIC::mio_ctrl_read(uint32_t offset, int size) {
uint32_t res = 0;
switch (offset & 0xFC) {
case 0x14:
LOG_F(9, "read from MIO:Int_Mask2 register \n");
res = this->int_mask2;
break;
case 0x18:
LOG_F(9, "read from MIO:Int_Clear2 register \n");
res = this->int_clear2;
break;
case 0x1C:
LOG_F(9, "read from MIO:Int_Levels2 register \n");
res = this->int_levels2;
break;
case 0x24:
LOG_F(9, "read from MIO:Int_Mask1 register \n");
res = this->int_mask1;
break;
case 0x28:
LOG_F(9, "read from MIO:Int_Clear1 register \n");
res = this->int_clear1;
break;
case 0x2C:
LOG_F(9, "read from MIO:Int_Levels1 register \n");
res = this->int_levels1;
break;
case 0x34: /* heathrowIDs / HEATHROW_MBCR (Linux): media bay config reg? */
LOG_F(9, "read from MIO:ID register at Address %x \n", ppc_state.pc);
res = this->macio_id;
break;
case 0x38:
LOG_F(9, "read from MIO:Feat_Ctrl register \n");
res = this->feat_ctrl;
break;
default:
LOG_F(WARNING, "read from unknown MIO register at %x \n", offset);
break;
}
return res;
}
void HeathrowIC::mio_ctrl_write(uint32_t offset, uint32_t value, int size) {
switch (offset & 0xFC) {
case 0x14:
LOG_F(9, "write %x to MIO:Int_Mask2 register \n", value);
this->int_mask2 = value;
break;
case 0x18:
LOG_F(9, "write %x to MIO:Int_Clear2 register \n", value);
this->int_clear2 = value;
break;
case 0x1C:
LOG_F(9, "write %x to MIO:Int_Levels2 register \n", value);
this->int_levels2 = value;
break;
case 0x24:
LOG_F(9, "write %x to MIO:Int_Mask1 register \n", value);
this->int_mask1 = value;
break;
case 0x28:
LOG_F(9, "write %x to MIO:Int_Clear1 register \n", value);
this->int_clear1 = value;
break;
case 0x2C:
LOG_F(9, "write %x to MIO:Int_Levels1 register \n", value);
this->int_levels1 = value;
break;
case 0x34:
LOG_F(WARNING, "Attempted to write %x to MIO:ID at %x; Address : %x \n", value, offset, ppc_state.pc);
break;
case 0x38:
LOG_F(9, "write %x to MIO:Feat_Ctrl register \n", value);
this->feat_ctrl = value;
break;
case 0x3C:
LOG_F(9, "write %x to MIO:Aux_Ctrl register \n", value);
this->aux_ctrl = value;
break;
default:
LOG_F(WARNING, "write %x to unknown MIO register at %x \n", value, offset);
break;
}
}
-62
View File
@@ -1,62 +0,0 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef HW_COMPONENT_H
#define HW_COMPONENT_H
#include <string>
/** types of different HW components */
enum HWCompType : int {
UNKNOWN = 0, /* unknown component type */
MEM_CTRL = 10, /* memory controller */
ROM = 20, /* read-only memory */
RAM = 30, /* random access memory */
MMIO_DEV = 40, /* memory mapped I/O device */
PCI_HOST = 50, /* PCI host */
PCI_DEV = 51, /* PCI device */
I2C_HOST = 60, /* I2C host */
I2C_DEV = 61, /* I2C device */
ADB_HOST = 70, /* ADB host */
ADB_DEV = 71, /* ADB device */
SND_SERVER = 100, /* host sound server */
};
/** Abstract base class for HW components. */
class HWComponent {
public:
virtual ~HWComponent() = default;
virtual std::string get_name(void) {
return this->name;
};
virtual void set_name(std::string name) {
this->name = name;
};
virtual bool supports_type(HWCompType type) = 0;
protected:
std::string name;
};
#endif /* HW_COMPONENT_H */
+555
View File
@@ -0,0 +1,555 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** Apple memory-mapped I/O controller emulation.
Author: Max Poliakovski
*/
#include <core/timermanager.h>
#include <cpu/ppc/ppcemu.h>
#include <cpu/ppc/ppcmmu.h>
#include <devices/deviceregistry.h>
#include <devices/common/hwcomponent.h>
#include <devices/common/scsi/sc53c94.h>
#include <devices/common/viacuda.h>
#include <devices/ethernet/mace.h>
#include <devices/floppy/swim3.h>
#include <devices/ioctrl/amic.h>
#include <devices/serial/escc.h>
#include <machines/machinebase.h>
#include <devices/memctrl/memctrlbase.h>
#include <devices/video/displayid.h>
#include <devices/video/pdmonboard.h>
#include <algorithm>
#include <cinttypes>
#include <loguru.hpp>
#include <memory>
AMIC::AMIC() : MMIODevice()
{
this->name = "Apple Memory-mapped I/O Controller";
supports_types(HWCompType::MMIO_DEV | HWCompType::INT_CTRL);
// connect internal SCSI controller
this->scsi = dynamic_cast<Sc53C94*>(gMachineObj->get_comp_by_name("Sc53C94"));
// connect serial HW
this->escc = dynamic_cast<EsccController*>(gMachineObj->get_comp_by_name("Escc"));
// connect Ethernet HW
this->mace = dynamic_cast<MaceController*>(gMachineObj->get_comp_by_name("Mace"));
// connect Cuda
this->viacuda = dynamic_cast<ViaCuda*>(gMachineObj->get_comp_by_name("ViaCuda"));
// initialize sound HW
this->snd_out_dma = std::unique_ptr<AmicSndOutDma> (new AmicSndOutDma());
this->awacs = std::unique_ptr<AwacDevicePdm> (new AwacDevicePdm());
this->awacs->set_dma_out(this->snd_out_dma.get());
// initialize on-board video
this->disp_id = std::unique_ptr<DisplayID> (new DisplayID());
this->def_vid = std::unique_ptr<PdmOnboardVideo> (new PdmOnboardVideo());
// initialize floppy disk HW
this->swim3 = dynamic_cast<Swim3::Swim3Ctrl*>(gMachineObj->get_comp_by_name("Swim3"));
this->floppy_dma = std::unique_ptr<AmicFloppyDma> (new AmicFloppyDma());
this->swim3->set_dma_channel(this->floppy_dma.get());
}
int AMIC::device_postinit()
{
MemCtrlBase *mem_ctrl = dynamic_cast<MemCtrlBase *>
(gMachineObj->get_comp_by_type(HWCompType::MEM_CTRL));
// add memory mapped I/O region for the AMIC control registers
if (!mem_ctrl->add_mmio_region(0x50F00000, 0x00040000, this)) {
LOG_F(ERROR, "Couldn't register AMIC registers!");
}
// AMIC drives the VIA CA1 internally to generate 60.15 Hz interrupts
this->pseudo_vbl_tid = TimerManager::get_instance()->add_cyclic_timer(
static_cast<uint64_t>((1.0f/60.15) * NS_PER_SEC + 0.5f),
[this]() {
this->viacuda->assert_ctrl_line(ViaLine::CA1);
});
// set EMMO pin status (active low)
this->emmo_pin = GET_BIN_PROP("emmo") ^ 1;
return 0;
}
uint32_t AMIC::read(uint32_t rgn_start, uint32_t offset, int size)
{
uint32_t phase_val;
// subdevices registers
switch(offset >> 12) {
case 0: // VIA1 registers
case 1:
return this->viacuda->read(offset >> 9);
case 4: // SCC registers
return this->escc->read(compat_to_macrisc[(offset >> 1) & 0xF]);
case 0xA: // MACE registers
return this->mace->read((offset >> 4) & 0x1F);
case 0x10: // SCSI registers
return this->scsi->read((offset >> 4) & 0xF);
case 0x14: // Sound registers
switch (offset) {
case AMICReg::Snd_Stat_0:
case AMICReg::Snd_Stat_1:
case AMICReg::Snd_Stat_2:
return (this->awacs->read_stat() >> (offset & 3) * 8) & 0xFF;
case AMICReg::Snd_Phase0:
case AMICReg::Snd_Phase1:
case AMICReg::Snd_Phase2:
// the sound phase register is organized as follows:
// 000000oo oooooooo oopppppp where 'o' is the 12-bit offset
// into the DMA buffer and 'p' is an undocumented prescale value
// HWInit doesn't care about. Let's hope it will be sufficient
// to return 0 for prescale.
phase_val = this->snd_out_dma->get_cur_buf_pos() << 6;
return (phase_val >> ((2 - (offset & 3)) * 8)) & 0xFF;
case AMICReg::Snd_Out_Ctrl:
return this->snd_out_ctrl;
case AMICReg::Snd_Out_DMA:
return this->snd_out_dma->read_stat();
}
break;
case 0x16: // SWIM3 registers
case 0x17:
return this->swim3->read((offset >> 9) & 0xF);
}
switch(offset) {
case AMICReg::Ariel_Config:
return this->def_vid->get_vdac_config();
case AMICReg::VIA2_IFR:
case AMICReg::VIA2_IFR_RBV:
return this->via2_ifr;
case AMICReg::VIA2_IER:
case AMICReg::VIA2_IER_RBV:
return this->via2_ier;
case AMICReg::Video_Mode:
return this->def_vid->get_video_mode();
case AMICReg::Monitor_Id:
return this->mon_id;
case AMICReg::Int_Ctrl:
return (this->int_ctrl & 0xC0) | (this->dev_irq_lines & 0x3F);
case AMICReg::Diag_Reg:
return 0xFE | this->emmo_pin;
case AMICReg::DMA_Base_Addr_0:
case AMICReg::DMA_Base_Addr_1:
case AMICReg::DMA_Base_Addr_2:
case AMICReg::DMA_Base_Addr_3:
return (this->dma_base >> (3 - (offset & 3)) * 8) & 0xFF;
case AMICReg::SCSI_DMA_Ctrl:
return this->scsi_dma_cs;
case AMICReg::Floppy_Addr_Ptr_0:
case AMICReg::Floppy_Addr_Ptr_1:
case AMICReg::Floppy_Addr_Ptr_2:
case AMICReg::Floppy_Addr_Ptr_3:
return (this->floppy_addr_ptr >> (3 - (offset & 3)) * 8) & 0xFF;
case AMICReg::Floppy_DMA_Ctrl:
return this->floppy_dma->read_stat();
default:
LOG_F(WARNING, "Unknown AMIC register read, offset=%x", offset);
}
return 0;
}
void AMIC::write(uint32_t rgn_start, uint32_t offset, uint32_t value, int size)
{
uint32_t mask;
// subdevices registers
switch(offset >> 12) {
case 0: // VIA1 registers
case 1:
this->viacuda->write(offset >> 9, value);
return;
case 4:
this->escc->write(compat_to_macrisc[(offset >> 1) & 0xF], value);
return;
case 0xA: // MACE registers
this->mace->write((offset >> 4) & 0x1F, value);
return;
case 0x10:
this->scsi->write((offset >> 4) & 0xF, value);
return;
case 0x14: // Sound registers
switch(offset) {
case AMICReg::Snd_Ctrl_0:
case AMICReg::Snd_Ctrl_1:
case AMICReg::Snd_Ctrl_2:
// remember values of sound control registers
this->imm_snd_regs[offset & 3] = value;
// transfer control information to the sound codec when ready
if ((this->imm_snd_regs[0] & 0xC0) == PDM_SND_CTRL_VALID) {
this->awacs->write_ctrl(
(this->imm_snd_regs[1] >> 4) | (this->imm_snd_regs[0] & 0x3F),
((this->imm_snd_regs[1] & 0xF) << 8) | this->imm_snd_regs[2]
);
}
return;
case AMICReg::Snd_Buf_Size_Hi:
case AMICReg::Snd_Buf_Size_Lo:
SET_SIZE_BYTE(this->snd_buf_size, offset, value);
this->snd_buf_size &= ~3; // sound buffer size is always a multiple of 4
LOG_F(9, "AMIC: Sound buffer size set to 0x%X", this->snd_buf_size);
return;
case AMICReg::Snd_Out_Ctrl:
LOG_F(9, "AMIC Sound Out Ctrl updated, val=%x", value);
if ((value & 1) != (this->snd_out_ctrl & 1)) {
if (value & 1) {
LOG_F(9, "AMIC Sound Out DMA enabled!");
this->snd_out_dma->init(this->dma_base & ~0x3FFFF,
this->snd_buf_size);
this->snd_out_dma->enable();
this->awacs->set_sample_rate((this->snd_out_ctrl >> 1) & 3);
this->awacs->start_output_dma();
} else {
LOG_F(9, "AMIC Sound Out DMA disabled!");
this->snd_out_dma->disable();
}
}
this->snd_out_ctrl = value;
return;
case AMICReg::Snd_In_Ctrl:
LOG_F(INFO, "AMIC Sound In Ctrl updated, val=%x", value);
return;
case AMICReg::Snd_Out_DMA:
this->snd_out_dma->write_dma_out_ctrl(value);
return;
}
case 0x16: // SWIM3 registers
case 0x17:
this->swim3->write((offset >> 9) & 0xF, value);
return;
}
switch(offset) {
case AMICReg::VIA2_Slot_IER:
LOG_F(INFO, "AMIC VIA2 Slot Interrupt Enable Register updated, val=%x", value);
break;
case AMICReg::VIA2_IER:
if (value & 0x80) {
this->via2_ier |= value & 0x7F;
} else {
this->via2_ier &= ~value;
}
break;
case AMICReg::Ariel_Clut_Index:
this->def_vid->set_clut_index(value);
break;
case AMICReg::Ariel_Clut_Color:
this->def_vid->set_clut_color(value);
break;
case AMICReg::Ariel_Config:
this->def_vid->set_vdac_config(value);
break;
case AMICReg::Video_Mode:
this->def_vid->set_video_mode(value);
break;
case AMICReg::Pixel_Depth:
this->def_vid->set_pixel_depth(value);
break;
case AMICReg::Monitor_Id: {
// extract and convert pin directions (0 - input, 1 - output)
uint8_t dirs = ~value & 7;
if (!dirs && !(value & 8)) {
LOG_F(INFO, "AMIC: Monitor sense lines tristated");
}
// propagate bit 3 to all pins configured as output
// set levels of all intput pins to "1"
uint8_t levels = (7 ^ dirs) | (((value & 8) ? 7 : 0) & dirs);
// read monitor sense lines and store the result in the bits 4-6
this->mon_id = (this->mon_id & 0xF) |
(this->disp_id->read_monitor_sense(levels, dirs) << 4);
}
break;
case AMICReg::Int_Ctrl:
// reset CPU interrupt bit if requested
if (value & AMIC_INT_CLR) {
if (this->int_ctrl & 0x80) {
this->int_ctrl &= ~AMIC_INT_CLR;
ppc_release_int();
LOG_F(5, "AMIC: CPU INT latch cleared");
}
}
// keep interrupt mode bit
// and discard read-only IQR state bits
this->int_ctrl |= value & AMIC_INT_MODE;
break;
case AMICReg::DMA_Base_Addr_0:
case AMICReg::DMA_Base_Addr_1:
case AMICReg::DMA_Base_Addr_2:
case AMICReg::DMA_Base_Addr_3:
SET_ADDR_BYTE(this->dma_base, offset, value);
this->dma_base &= 0xFFFC0000UL;
LOG_F(9, "AMIC: DMA base address set to 0x%X", this->dma_base);
break;
case AMICReg::Enet_DMA_Xmt_Ctrl:
LOG_F(INFO, "AMIC Ethernet Transmit DMA Ctrl updated, val=%x", value);
break;
case AMICReg::SCSI_DMA_Ctrl:
LOG_F(INFO, "AMIC SCSI DMA Ctrl updated, val=%x", value);
this->scsi_dma_cs = value;
break;
case AMICReg::Enet_DMA_Rcv_Ctrl:
LOG_F(INFO, "AMIC Ethernet Receive DMA Ctrl updated, val=%x", value);
break;
case AMICReg::Floppy_Addr_Ptr_2:
case AMICReg::Floppy_Addr_Ptr_3:
SET_ADDR_BYTE(this->floppy_addr_ptr, offset, value);
break;
case AMICReg::Floppy_Byte_Cnt_Hi:
case AMICReg::Floppy_Byte_Cnt_Lo:
SET_SIZE_BYTE(this->floppy_byte_cnt, offset, value);
break;
case AMICReg::Floppy_DMA_Ctrl:
if (value & 1) { // RST bit set?
this->floppy_addr_ptr = this->dma_base + 0x15000;
this->floppy_dma->reset(this->floppy_addr_ptr);
}
if (value & 2) { // RUN bit set?
this->floppy_dma->reinit(this->floppy_addr_ptr, this->floppy_byte_cnt);
}
this->floppy_dma->write_ctrl(value);
break;
case AMICReg::SCC_DMA_Xmt_A_Ctrl:
LOG_F(INFO, "AMIC SCC Transmit Ch A DMA Ctrl updated, val=%x", value);
break;
case AMICReg::SCC_DMA_Rcv_A_Ctrl:
LOG_F(INFO, "AMIC SCC Receive Ch A DMA Ctrl updated, val=%x", value);
break;
case AMICReg::SCC_DMA_Xmt_B_Ctrl:
LOG_F(INFO, "AMIC SCC Transmit Ch B DMA Ctrl updated, val=%x", value);
break;
case AMICReg::SCC_DMA_Rcv_B_Ctrl:
LOG_F(INFO, "AMIC SCC Receive Ch B DMA Ctrl updated, val=%x", value);
break;
default:
LOG_F(WARNING, "Unknown AMIC register write, offset=%x, val=%x",
offset, value);
}
}
// ======================== Interrupt related stuff ==========================
uint32_t AMIC::register_dev_int(IntSrc src_id) {
switch (src_id) {
case IntSrc::VIA_CUDA:
return 1;
case IntSrc::SCSI1:
return 0x800;
case IntSrc::SWIM3:
return 0x2000;
default:
ABORT_F("AMIC: unknown interrupt source %d", src_id);
}
return 0;
}
uint32_t AMIC::register_dma_int(IntSrc src_id) {
ABORT_F("AMIC: register_dma_int() not implemented");
return 0;
}
void AMIC::ack_int(uint32_t irq_id, uint8_t irq_line_state) {
// dispatch AMIC interrupts from various sources
if (irq_id < 0x100) {
this->ack_cpu_int(irq_id, irq_line_state);
} else if (irq_id < 0x10000) {
this->ack_via2_int(irq_id >> 8, irq_line_state);
} else {
ABORT_F("AMIC: unknown interrupt source ID 0x%X", irq_id);
}
}
void AMIC::ack_via2_int(uint32_t irq_id, uint8_t irq_line_state) {
if (irq_line_state) {
this->via2_ifr |= irq_id;
} else {
this->via2_ifr &= ~irq_id;
}
uint8_t new_irq = !!(this->via2_ifr & this->via2_ier & 0x7F);
this->via2_ifr = (this->via2_ifr & 0x7F) | (new_irq << 7);
if (new_irq != this->via2_irq) {
this->via2_irq = new_irq;
this->ack_cpu_int(2, new_irq);
}
}
void AMIC::ack_cpu_int(uint32_t irq_id, uint8_t irq_line_state) {
if (this->int_ctrl & AMIC_INT_MODE) { // 68k interrupt emulation mode?
if (irq_line_state) {
this->dev_irq_lines |= irq_id;
} else {
this->dev_irq_lines &= ~irq_id;
}
if (!(this->int_ctrl & 0x80)) {
this->int_ctrl |= 0x80; // set CPU interrupt bit
ppc_assert_int();
LOG_F(5, "AMIC: CPU INT asserted, source: %d", irq_id);
} else {
LOG_F(5, "AMIC: CPU INT already latched");
}
} else {
ABORT_F("AMIC: interrupt mode 0 not implemented");
}
}
void AMIC::ack_dma_int(uint32_t irq_id, uint8_t irq_line_state) {
ABORT_F("AMIC: ack_dma_int() not implemented");
}
// ============================ Sound DMA stuff ================================
AmicSndOutDma::AmicSndOutDma()
{
this->dma_out_ctrl = 0;
this->enabled = false;
}
void AmicSndOutDma::init(uint32_t buf_base, uint32_t buf_samples)
{
this->out_buf0 = buf_base + AMIC_SND_BUF0_OFFS;
this->out_buf1 = buf_base + AMIC_SND_BUF1_OFFS;
this->out_buf_len = buf_samples * 2 * 2;
this->snd_buf_num = 0;
this->cur_buf_pos = 0;
}
uint8_t AmicSndOutDma::read_stat()
{
return this->dma_out_ctrl;
}
void AmicSndOutDma::write_dma_out_ctrl(uint8_t value)
{
// clear interrupt flags
value &= ~PDM_DMA_INTS_MASK;
this->dma_out_ctrl = value;
LOG_F(9, "AMIC: Sound out DMA control set to 0x%X", value);
}
DmaPullResult AmicSndOutDma::pull_data(uint32_t req_len, uint32_t *avail_len,
uint8_t **p_data)
{
*avail_len = 0;
int rem_len = this->out_buf_len - this->cur_buf_pos;
if (rem_len <= 0) {
if (!this->snd_buf_num) {
// signal buffer 0 drained
this->dma_out_ctrl |= PDM_DMA_IF0;
// TODO: generate IE0 interrupt if enabled
} else {
// signal buffer 1 drained
this->dma_out_ctrl |= PDM_DMA_IF1;
// TODO: generate IE1 interrupt if enabled
}
// check DMA enable flag after buffer 1 was processed
// if it's false stop delivering sound data
// this will effectively stop audio playback
if (this->snd_buf_num && !this->enabled) {
this->cur_buf_pos = 0;
return DmaPullResult::NoMoreData;
}
this->cur_buf_pos = 0; // reset buffer position
this->snd_buf_num ^= 1; // toggle sound buffers
rem_len = this->out_buf_len; // buffer size = full buffer
}
uint32_t len = std::min((uint32_t)rem_len, req_len);
*p_data = mmu_get_dma_mem(
(this->snd_buf_num ? this->out_buf1 : this->out_buf0) + this->cur_buf_pos,
len);
this->cur_buf_pos += len;
*avail_len = len;
return DmaPullResult::MoreData;
}
// ============================ Floppy DMA stuff ===============================
void AmicFloppyDma::reset(const uint32_t addr_ptr)
{
this->stat &= 0x48; // clear interrupt flag, RUN and RST bits
this->addr_ptr = addr_ptr;
this->byte_count = 0;
}
void AmicFloppyDma::reinit(const uint32_t addr_ptr, const uint16_t byte_cnt)
{
this->addr_ptr = addr_ptr;
this->byte_count = byte_cnt;
}
void AmicFloppyDma::write_ctrl(uint8_t value)
{
// copy over DIR, IE and RUN bits
this->stat = (this->stat & 0x81) | (value & 0x4A);
// clear interrupt flag if requested
if (value & 0x80) {
this->stat &= 0x7F;
}
}
int AmicFloppyDma::push_data(const char* src_ptr, int len)
{
len = std::min((int)this->byte_count, len);
uint8_t *p_data = mmu_get_dma_mem(this->addr_ptr, len);
std::memcpy(p_data, src_ptr, len);
this->addr_ptr += len;
this->byte_count -= len;
if (!this->byte_count) {
LOG_F(WARNING, "AMIC: DMA interrupts not implemented yet");
}
return 0;
}
DmaPullResult AmicFloppyDma::pull_data(uint32_t req_len, uint32_t *avail_len,
uint8_t **p_data)
{
return DmaPullResult::NoMoreData;
}
static vector<string> Amic_Subdevices = {
"Sc53C94", "Escc", "Mace", "ViaCuda", "Swim3"
};
static const DeviceDescription Amic_Descriptor = {
AMIC::create, Amic_Subdevices, {}
};
REGISTER_DEVICE(Amic, Amic_Descriptor);
+123 -17
View File
@@ -24,19 +24,30 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#ifndef AMIC_H
#define AMIC_H
#include "awacs.h"
#include "dmacore.h"
#include "mmiodevice.h"
#include "viacuda.h"
#include <devices/common/hwinterrupt.h>
#include <devices/common/mmiodevice.h>
#include <devices/common/scsi/sc53c94.h>
#include <devices/common/viacuda.h>
#include <devices/ethernet/mace.h>
#include <devices/floppy/swim3.h>
#include <devices/serial/escc.h>
#include <devices/sound/awacs.h>
#include <devices/video/displayid.h>
#include <devices/video/pdmonboard.h>
#include <cinttypes>
#include <memory>
/** Interrupt related constants. */
#define AMIC_INT_CLR 0x80 // clears CPU interrupt
#define AMIC_INT_MODE 0x40 // interrupt mode: 0 - native, 1 - 68k-style
/** AMIC sound buffers are located at fixed offsets from DMA base. */
#define AMIC_SND_BUF0_OFFS 0x10000
#define AMIC_SND_BUF1_OFFS 0x12000
// PDM HWInit source defines two constants: kExpBit = 0x80 and kCmdBit = 0x40
// I don't know what they means but it seems that their combination will
// I don't know what they mean but it seems that their combination will
// cause sound control parameters to be transferred to the sound chip.
#define PDM_SND_CTRL_VALID 0xC0
@@ -50,7 +61,6 @@ public:
AmicSndOutDma();
~AmicSndOutDma() = default;
bool is_active();
void init(uint32_t buf_base, uint32_t buf_samples);
void enable() { this->enabled = true; };
void disable() { this->enabled = false; };
@@ -71,6 +81,37 @@ private:
uint32_t cur_buf_pos;
};
/** AMIC-specific floppy DMA implementation. */
class AmicFloppyDma : public DmaBidirChannel {
public:
AmicFloppyDma() = default;
~AmicFloppyDma() = default;
void reinit(const uint32_t addr_ptr, const uint16_t byte_cnt);
void reset(const uint32_t addr_ptr);
void write_ctrl(const uint8_t value);
uint8_t read_stat() { return this->stat; };
int push_data(const char* src_ptr, int len);
DmaPullResult pull_data(uint32_t req_len, uint32_t *avail_len,
uint8_t **p_data);
private:
uint32_t addr_ptr;
uint16_t byte_count;
uint8_t stat;
};
// macro for byte wise updating of AMIC DMA address registers
#define SET_ADDR_BYTE(reg, offset, value) \
mask = 0xFF000000UL >> (8 * ((offset) & 3)); \
(reg) = ((reg) & ~mask) | (((value) & 0xFF) << (8 * (3 - ((offset) & 3))));
// macro for byte wise updating of AMIC DMA size registers
#define SET_SIZE_BYTE(reg, offset, value) \
mask = 0xFF00U >> (8 * ((offset) & 1)); \
(reg) = ((reg) & ~mask) | (((value) & 0xFF) << (8 * (((offset) & 1) ^ 1)));
/* AMIC registers offsets from AMIC base (0x50F00000). */
enum AMICReg : uint32_t {
// Sound control registers
@@ -90,15 +131,29 @@ enum AMICReg : uint32_t {
Snd_In_DMA = 0x14014, // sound input DMA status/control register
Snd_Out_DMA = 0x14018, // sound output DMA status/control register
// Video DAC (Ariel II) control registers
Ariel_Clut_Index = 0x24000,
Ariel_Clut_Color = 0x24001,
Ariel_Config = 0x24002,
// VIA2 registers
VIA2_IFR = 0x26003,
VIA2_Slot_IER = 0x26012,
VIA2_IER = 0x26013,
VIA2_IFR_RBV = 0x27A03, // RBV-compatible mirror for the VIA2_IFR
VIA2_IER_RBV = 0x27C13, // RBV-compatible mirror for the VIA2_IER
// Video control registers
Video_Mode_Reg = 0x28000,
Video_Mode = 0x28000,
Pixel_Depth = 0x28001,
Monitor_Id = 0x28002,
// Interrupt registers
Int_Ctrl = 0x2A000,
// Undocumented diagnostics register
Diag_Reg = 0x2C000,
// DMA control registers
DMA_Base_Addr_0 = 0x31000,
DMA_Base_Addr_1 = 0x31001,
@@ -107,7 +162,16 @@ enum AMICReg : uint32_t {
Enet_DMA_Xmt_Ctrl = 0x31C20,
SCSI_DMA_Ctrl = 0x32008,
Enet_DMA_Rcv_Ctrl = 0x32028,
SWIM3_DMA_Ctrl = 0x32068,
// Floppy (SWIM3) DMA registers
Floppy_Addr_Ptr_0 = 0x32060,
Floppy_Addr_Ptr_1 = 0x32061,
Floppy_Addr_Ptr_2 = 0x32062,
Floppy_Addr_Ptr_3 = 0x32063,
Floppy_Byte_Cnt_Hi = 0x32064,
Floppy_Byte_Cnt_Lo = 0x32065,
Floppy_DMA_Ctrl = 0x32068,
SCC_DMA_Xmt_A_Ctrl = 0x32088,
SCC_DMA_Rcv_A_Ctrl = 0x32098,
SCC_DMA_Xmt_B_Ctrl = 0x320A8,
@@ -115,31 +179,73 @@ enum AMICReg : uint32_t {
};
/** Apple Memory-mapped I/O controller device. */
class AMIC : public MMIODevice {
class AMIC : public MMIODevice, public InterruptCtrl {
public:
AMIC();
~AMIC() = default;
bool supports_type(HWCompType type);
static std::unique_ptr<HWComponent> create() {
return std::unique_ptr<AMIC>(new AMIC());
}
// HWComponent methods
int device_postinit();
/* MMIODevice methods */
uint32_t read(uint32_t reg_start, uint32_t offset, int size);
void write(uint32_t reg_start, uint32_t offset, uint32_t value, int size);
uint32_t read(uint32_t rgn_start, uint32_t offset, int size);
void write(uint32_t rgn_start, uint32_t offset, uint32_t value, int size);
// InterruptCtrl methods
uint32_t register_dev_int(IntSrc src_id);
uint32_t register_dma_int(IntSrc src_id);
void ack_int(uint32_t irq_id, uint8_t irq_line_state);
void ack_dma_int(uint32_t irq_id, uint8_t irq_line_state);
protected:
void dma_reg_write(uint32_t offset, uint32_t value, int size);
void ack_via2_int(uint32_t irq_id, uint8_t irq_line_state);
void ack_cpu_int(uint32_t irq_id, uint8_t irq_line_state);
private:
uint8_t imm_snd_regs[4]; // temporary storage for sound control registers
uint32_t dma_base = 0; // DMA physical base address
uint16_t snd_buf_size = 0; // sound buffer size in bytes
uint8_t emmo_pin; // EMMO aka factory tester pin status, active low
uint32_t dma_base = 0; // DMA physical base address
uint16_t snd_buf_size = 0; // sound buffer size in bytes
uint8_t snd_out_ctrl = 0;
std::unique_ptr<ViaCuda> viacuda;
std::unique_ptr<AwacDevicePdm> awacs;
// floppy DMA state
uint32_t floppy_addr_ptr;
uint16_t floppy_byte_cnt;
uint8_t scsi_dma_cs = 0; // SCSI DMA control/status register value
// interrupt state
uint8_t int_ctrl = 0;
uint8_t dev_irq_lines = 0; // state of the IRQ lines
// pseudo VIA2 state
uint8_t via2_ier = 0;
uint8_t via2_ifr = 0;
uint8_t via2_irq = 0;
uint32_t pseudo_vbl_tid; // ID for the pseudo-VBL timer
// AMIC subdevice instances
Sc53C94* scsi;
EsccController* escc;
MaceController* mace;
ViaCuda* viacuda;
Swim3::Swim3Ctrl* swim3;
std::unique_ptr<AwacDevicePdm> awacs;
std::unique_ptr<AmicSndOutDma> snd_out_dma;
std::unique_ptr<AmicFloppyDma> floppy_dma;
// on-board video
std::unique_ptr<DisplayID> disp_id;
std::unique_ptr<PdmOnboardVideo> def_vid;
uint8_t mon_id;
};
#endif // AMIC_H
+325
View File
@@ -0,0 +1,325 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <cpu/ppc/ppcemu.h>
#include <devices/deviceregistry.h>
#include <devices/common/scsi/sc53c94.h>
#include <devices/ethernet/mace.h>
#include <devices/floppy/swim3.h>
#include <devices/ioctrl/macio.h>
#include <devices/serial/escc.h>
#include <endianswap.h>
#include <loguru.hpp>
#include <machines/machinebase.h>
#include <cinttypes>
#include <memory>
GrandCentral::GrandCentral() : PCIDevice("mac-io/grandcentral"), InterruptCtrl()
{
supports_types(HWCompType::MMIO_DEV | HWCompType::INT_CTRL);
// populate my PCI config header
this->vendor_id = PCI_VENDOR_APPLE;
this->device_id = 0x0002;
this->class_rev = 0xFF000002;
this->cache_ln_sz = 8;
this->bars_cfg[0] = 0xFFFE0000UL; // declare 128Kb of memory-mapped I/O space
this->pci_notify_bar_change = [this](int bar_num) {
this->notify_bar_change(bar_num);
};
// NVRAM connection
this->nvram = dynamic_cast<NVram*>(gMachineObj->get_comp_by_name("NVRAM"));
// connect Cuda
this->viacuda = dynamic_cast<ViaCuda*>(gMachineObj->get_comp_by_name("ViaCuda"));
// initialize sound chip and its DMA output channel, then wire them together
this->awacs = std::unique_ptr<AwacsScreamer> (new AwacsScreamer());
this->snd_out_dma = std::unique_ptr<DMAChannel> (new DMAChannel());
this->awacs->set_dma_out(this->snd_out_dma.get());
this->snd_out_dma->set_callbacks(
std::bind(&AwacsScreamer::dma_start, this->awacs.get()),
std::bind(&AwacsScreamer::dma_end, this->awacs.get())
);
// connect serial HW
this->escc = dynamic_cast<EsccController*>(gMachineObj->get_comp_by_name("Escc"));
// connect SCSI HW
this->scsi_0 = dynamic_cast<Sc53C94*>(gMachineObj->get_comp_by_name("Sc53C94"));
// connect Ethernet HW
this->mace = dynamic_cast<MaceController*>(gMachineObj->get_comp_by_name("Mace"));
// connect floppy disk HW
this->swim3 = dynamic_cast<Swim3::Swim3Ctrl*>(gMachineObj->get_comp_by_name("Swim3"));
// set EMMO pin status (active low)
this->emmo_pin = GET_BIN_PROP("emmo") ^ 1;
}
void GrandCentral::notify_bar_change(int bar_num)
{
if (bar_num) // only BAR0 is supported
return;
if (this->base_addr != (this->bars[bar_num] & 0xFFFFFFF0UL)) {
if (this->base_addr) {
LOG_F(WARNING, "GC: deallocating I/O memory not implemented");
}
this->base_addr = this->bars[0] & 0xFFFFFFF0UL;
this->host_instance->pci_register_mmio_region(this->base_addr, 0x20000, this);
LOG_F(INFO, "%s: base address set to 0x%X", this->pci_name.c_str(), this->base_addr);
}
}
uint32_t GrandCentral::read(uint32_t rgn_start, uint32_t offset, int size)
{
if (offset & 0x10000) { // Device register space
unsigned subdev_num = (offset >> 12) & 0xF;
switch (subdev_num) {
case 0: // Curio SCSI
return this->scsi_0->read((offset >> 4) & 0xF);
case 1: // MACE
return this->mace->read((offset >> 4) & 0x1F);
case 2: // ESCC compatible addressing
if ((offset & 0xFF) < 16) {
return this->escc->read(compat_to_macrisc[(offset >> 1) & 0xF]);
}
// fallthrough
case 3: // ESCC MacRISC addressing
return this->escc->read((offset >> 4) & 0xF);
case 4: // AWACS
return this->awacs->snd_ctrl_read(offset & 0xFF, size);
case 5: // SWIM3
return this->swim3->read((offset >> 4) & 0xF);
case 6:
case 7: // VIA-CUDA
return this->viacuda->read((offset >> 9) & 0xF);
case 0xA: // Board register 1 (IOBus dev #1)
return BYTESWAP_32(this->emmo_pin << 8);
case 0xB: // IOBus dev #2
case 0xC: // IOBus dev #3
case 0xE: // IOBus dev #5
if (this->iobus_devs[subdev_num - 10] != nullptr) {
return BYTESWAP_16(this->iobus_devs[subdev_num - 10]->iodev_read(
(offset >> 4) & 0x1F));
} else {
LOG_F(ERROR, "GC: IOBus device #%d doesn't exist", subdev_num - 9);
return 0;
}
break;
case 0xF: // NVRAM Data (IOBus dev #6)
return this->nvram->read_byte(
(this->nvram_addr_hi << 5) + ((offset >> 4) & 0x1F));
}
} else if (offset & 0x8000) { // DMA register space
unsigned subdev_num = (offset >> 12) & 0xF;
switch (subdev_num) {
case 8:
return this->snd_out_dma->reg_read(offset & 0xFF, size);
default:
LOG_F(WARNING, "GC: unimplemented DMA register at 0x%X",
this->base_addr + offset);
}
} else { // Interrupt related registers
switch (offset) {
case MIO_INT_MASK1:
return BYTESWAP_32(this->int_mask);
case MIO_INT_LEVELS1:
return BYTESWAP_32(this->int_levels);
case MIO_INT_EVENTS1:
return BYTESWAP_32(this->int_events);
}
}
LOG_F(WARNING, "GC: reading from unmapped I/O memory 0x%X", this->base_addr + offset);
return 0;
}
void GrandCentral::write(uint32_t rgn_start, uint32_t offset, uint32_t value, int size)
{
if (offset & 0x10000) { // Device register space
unsigned subdev_num = (offset >> 12) & 0xF;
switch (subdev_num) {
case 0: // Curio SCSI
this->scsi_0->write((offset >> 4) & 0xF, value);
break;
case 1: // MACE registers
this->mace->write((offset >> 4) & 0x1F, value);
break;
case 2: // ESCC compatible addressing
if ((offset & 0xFF) < 16) {
this->escc->write(compat_to_macrisc[(offset >> 1) & 0xF], value);
break;
}
// fallthrough
case 3: // ESCC MacRISC addressing
this->escc->write((offset >> 4) & 0xF, value);
break;
case 4: // AWACS
this->awacs->snd_ctrl_write(offset & 0xFF, value, size);
break;
case 5:
this->swim3->write((offset >> 4) & 0xF, value);
break;
case 6:
case 7: // VIA-CUDA
this->viacuda->write((offset >> 9) & 0xF, value);
break;
case 0xA: // IOBus dev #1
case 0xB: // IOBus dev #2
case 0xC: // IOBus dev #3
case 0xE: // IOBus dev #5
if (this->iobus_devs[subdev_num - 10] != nullptr) {
this->iobus_devs[subdev_num - 10]->iodev_write(
(offset >> 4) & 0x1F, value);
} else {
LOG_F(ERROR, "GC: IOBus device #%d doesn't exist", subdev_num - 9);
}
break;
case 0xD: // NVRAM High Address (IOBus dev #4)
switch (size) {
case 4:
this->nvram_addr_hi = BYTESWAP_32(value);
break;
case 2:
this->nvram_addr_hi = BYTESWAP_16(value);
break;
default:
this->nvram_addr_hi = value;
}
break;
case 0xF: // NVRAM Data (IOBus dev #6)
this->nvram->write_byte(
(this->nvram_addr_hi << 5) + ((offset >> 4) & 0x1F), value);
break;
default:
LOG_F(WARNING, "GC: writing to unmapped I/O memory 0x%X",
this->base_addr + offset);
}
} else if (offset & 0x8000) { // DMA register space
unsigned subdev_num = (offset >> 12) & 0xF;
switch (subdev_num) {
case 8:
this->snd_out_dma->reg_write(offset & 0xFF, value, size);
break;
default:
LOG_F(WARNING, "GC: unimplemented DMA register at 0x%X",
this->base_addr + offset);
}
} else { // Interrupt related registers
switch (offset) {
case MIO_INT_MASK1:
this->int_mask = BYTESWAP_32(value);
break;
case MIO_INT_CLEAR1:
if (value & MACIO_INT_CLR) {
this->int_events = 0;
this->cpu_int_latch = false;
ppc_release_int();
LOG_F(5, "GC: CPU INT latch cleared");
} else {
this->int_events &= BYTESWAP_32(value);
}
break;
default:
LOG_F(WARNING, "GC: writing to unmapped I/O memory 0x%X",
this->base_addr + offset);
}
}
}
void GrandCentral::attach_iodevice(int dev_num, IobusDevice* dev_obj)
{
if (dev_num >= 0 && dev_num < 6) {
this->iobus_devs[dev_num] = dev_obj;
}
}
uint32_t GrandCentral::register_dev_int(IntSrc src_id)
{
switch (src_id) {
case IntSrc::VIA_CUDA:
return 1 << 18;
case IntSrc::SCSI1:
return 1 << 12;
case IntSrc::SWIM3:
return 1 << 19;
default:
ABORT_F("GC: unknown interrupt source %d", src_id);
}
return 0;
}
uint32_t GrandCentral::register_dma_int(IntSrc src_id)
{
ABORT_F("GC: register_dma_int() not implemened");
return 0;
}
void GrandCentral::ack_int(uint32_t irq_id, uint8_t irq_line_state)
{
if (this->int_mask & MACIO_INT_MODE) { // 68k interrupt emulation mode?
this->int_events |= irq_id; // signal IRQ line change
this->int_events &= this->int_mask;
// update IRQ line state
if (irq_line_state) {
this->int_levels |= irq_id;
} else {
this->int_levels &= ~irq_id;
}
// signal CPU interrupt
if (this->int_events) {
if (!this->cpu_int_latch) {
this->cpu_int_latch = true;
ppc_assert_int();
LOG_F(5, "GC: CPU INT asserted, source: %d", irq_id);
} else {
LOG_F(5, "GC: CPU INT already latched");
}
}
} else {
ABORT_F("GC: native interrupt mode not implemented");
}
}
void GrandCentral::ack_dma_int(uint32_t irq_id, uint8_t irq_line_state)
{
ABORT_F("GC: ack_dma_int() not implemened");
}
static const vector<string> GCSubdevices = {
"NVRAM", "ViaCuda", "Escc", "Sc53C94", "Mace", "Swim3"
};
static const DeviceDescription GC_Descriptor = {
GrandCentral::create, GCSubdevices, {}
};
REGISTER_DEVICE(GrandCentral, GC_Descriptor);
+392
View File
@@ -0,0 +1,392 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <cpu/ppc/ppcemu.h>
#include <devices/deviceregistry.h>
#include <devices/common/dbdma.h>
#include <devices/common/hwcomponent.h>
#include <devices/common/viacuda.h>
#include <devices/floppy/swim3.h>
#include <devices/ioctrl/macio.h>
#include <devices/serial/escc.h>
#include <devices/sound/awacs.h>
#include <endianswap.h>
#include <loguru.hpp>
#include <machines/machinebase.h>
#include <cinttypes>
#include <functional>
#include <iostream>
#include <memory>
/** Heathrow Mac I/O device emulation.
Author: Max Poliakovski
*/
using namespace std;
HeathrowIC::HeathrowIC() : PCIDevice("mac-io/heathrow"), InterruptCtrl()
{
supports_types(HWCompType::MMIO_DEV | HWCompType::INT_CTRL);
// populate my PCI config header
this->vendor_id = PCI_VENDOR_APPLE;
this->device_id = 0x0010;
this->class_rev = 0xFF000001;
this->cache_ln_sz = 8;
this->lat_timer = 0x40;
this->bars_cfg[0] = 0xFFF80000UL; // declare 512Kb of memory-mapped I/O space
this->pci_notify_bar_change = [this](int bar_num) {
this->notify_bar_change(bar_num);
};
// NVRAM connection
this->nvram = dynamic_cast<NVram*>(gMachineObj->get_comp_by_name("NVRAM"));
// connect Cuda
this->viacuda = dynamic_cast<ViaCuda*>(gMachineObj->get_comp_by_name("ViaCuda"));
// initialize sound chip and its DMA output channel, then wire them together
this->screamer = std::unique_ptr<AwacsScreamer> (new AwacsScreamer());
this->snd_out_dma = std::unique_ptr<DMAChannel> (new DMAChannel());
this->screamer->set_dma_out(this->snd_out_dma.get());
this->snd_out_dma->set_callbacks(
std::bind(&AwacsScreamer::dma_start, this->screamer.get()),
std::bind(&AwacsScreamer::dma_end, this->screamer.get())
);
// connect SCSI HW
this->mesh = dynamic_cast<MESHController*>(gMachineObj->get_comp_by_name("Mesh"));
// connect serial HW
this->escc = dynamic_cast<EsccController*>(gMachineObj->get_comp_by_name("Escc"));
// connect floppy disk HW
this->swim3 = dynamic_cast<Swim3::Swim3Ctrl*>(gMachineObj->get_comp_by_name("Swim3"));
// set EMMO pin status (active low)
this->emmo_pin = GET_BIN_PROP("emmo") ^ 1;
}
void HeathrowIC::notify_bar_change(int bar_num)
{
if (bar_num) // only BAR0 is supported
return;
if (this->base_addr != (this->bars[bar_num] & 0xFFFFFFF0UL)) {
if (this->base_addr) {
LOG_F(WARNING, "Heathrow: deallocating I/O memory not implemented");
}
this->base_addr = this->bars[0] & 0xFFFFFFF0UL;
this->host_instance->pci_register_mmio_region(this->base_addr, 0x80000, this);
LOG_F(INFO, "%s: base address set to 0x%X", this->pci_name.c_str(), this->base_addr);
}
}
uint32_t HeathrowIC::dma_read(uint32_t offset, int size) {
uint32_t res = 0;
switch (offset >> 8) {
case 8:
res = this->snd_out_dma->reg_read(offset & 0xFF, size);
break;
default:
LOG_F(WARNING, "Unsupported DMA channel read, offset=0x%X", offset);
}
return res;
}
void HeathrowIC::dma_write(uint32_t offset, uint32_t value, int size) {
switch (offset >> 8) {
case 8:
this->snd_out_dma->reg_write(offset & 0xFF, value, size);
break;
default:
LOG_F(WARNING, "Unsupported DMA channel write, offset=0x%X, val=0x%X", offset, value);
}
}
uint32_t HeathrowIC::read(uint32_t rgn_start, uint32_t offset, int size) {
uint32_t res = 0;
LOG_F(9, "%s: reading from offset %x", this->name.c_str(), offset);
unsigned sub_addr = (offset >> 12) & 0x7F;
switch (sub_addr) {
case 0:
res = mio_ctrl_read(offset, size);
break;
case 8:
res = dma_read(offset - 0x8000, size);
break;
case 0x10:
res = this->mesh->read((offset >> 4) & 0xF);
break;
case 0x12: // ESCC compatible addressing
if ((offset & 0xFF) < 16) {
return this->escc->read(compat_to_macrisc[(offset >> 1) & 0xF]);
}
// fallthrough
case 0x13: // ESCC MacRISC addressing
return this->escc->read((offset >> 4) & 0xF);
case 0x14:
res = this->screamer->snd_ctrl_read(offset - 0x14000, size);
break;
case 0x15: // SWIM3
return this->swim3->read((offset >> 4 )& 0xF);
case 0x16:
case 0x17:
res = this->viacuda->read((offset - 0x16000) >> 9);
break;
default:
if (sub_addr >= 0x60) {
res = this->nvram->read_byte((offset - 0x60000) >> 4);
} else {
LOG_F(WARNING, "Attempting to read unmapped I/O space: %x", offset);
}
}
return res;
}
void HeathrowIC::write(uint32_t rgn_start, uint32_t offset, uint32_t value, int size) {
LOG_F(9, "%s: writing to offset %x", this->name.c_str(), offset);
unsigned sub_addr = (offset >> 12) & 0x7F;
switch (sub_addr) {
case 0:
mio_ctrl_write(offset, value, size);
break;
case 8:
dma_write(offset - 0x8000, value, size);
break;
case 0x10:
this->mesh->write((offset >> 4) & 0xF, value);
break;
case 0x12: // ESCC compatible addressing
if ((offset & 0xFF) < 16) {
this->escc->write(compat_to_macrisc[(offset >> 1) & 0xF], value);
break;
}
// fallthrough
case 0x13: // ESCC MacRISC addressing
this->escc->write((offset >> 4) & 0xF, value);
break;
case 0x14:
this->screamer->snd_ctrl_write(offset - 0x14000, value, size);
break;
case 0x15: // SWIM3
this->swim3->write((offset >> 4) & 0xF, value);
break;
case 0x16:
case 0x17:
this->viacuda->write((offset - 0x16000) >> 9, value);
break;
default:
if (sub_addr >= 0x60) {
this->nvram->write_byte((offset - 0x60000) >> 4, value);
} else {
LOG_F(WARNING, "Attempting to write to unmapped I/O space: %x", offset);
}
}
}
uint32_t HeathrowIC::mio_ctrl_read(uint32_t offset, int size) {
uint32_t res = 0;
switch (offset & 0xFC) {
case MIO_INT_EVENTS2:
res = this->int_events2;
break;
case MIO_INT_MASK2:
res = this->int_mask2;
break;
case MIO_INT_LEVELS2:
res = this->int_levels2;
break;
case MIO_INT_EVENTS1:
res = this->int_events1;
break;
case MIO_INT_MASK1:
res = this->int_mask1;
break;
case MIO_INT_LEVELS1:
res = this->int_levels1;
break;
case MIO_HEAT_ID:
LOG_F(9, "read from MIO:ID register at Address %x", ppc_state.pc);
res = (this->fp_id << 24) | (this->mon_id << 16) | (this->mb_id << 8) |
(this->cpu_id | (this->emmo_pin << 4));
break;
case MIO_HEAT_FEAT_CTRL:
LOG_F(9, "read from MIO:Feat_Ctrl register");
res = this->feat_ctrl;
break;
default:
LOG_F(WARNING, "read from unknown MIO register at %x", offset);
break;
}
return BYTESWAP_32(res);
}
void HeathrowIC::mio_ctrl_write(uint32_t offset, uint32_t value, int size) {
switch (offset & 0xFC) {
case MIO_INT_MASK2:
this->int_mask2 |= BYTESWAP_32(value) & ~MACIO_INT_MODE;
break;
case MIO_INT_CLEAR2:
if (value & MACIO_INT_CLR) {
this->int_events2 = 0;
clear_cpu_int();
} else {
this->int_events2 &= BYTESWAP_32(value);
}
break;
case MIO_INT_MASK1:
this->int_mask1 = BYTESWAP_32(value);
// copy IntMode bit to InterruptMask2 register
this->int_mask2 = (this->int_mask2 & ~MACIO_INT_MODE) | (this->int_mask1 & MACIO_INT_MODE);
break;
case MIO_INT_CLEAR1:
if (value & MACIO_INT_CLR) {
this->int_events1 = 0;
clear_cpu_int();
} else {
this->int_events1 &= BYTESWAP_32(value);
}
break;
case MIO_HEAT_ID:
LOG_F(WARNING, "Attempted to write %x to MIO:ID at %x; Address : %x", value, offset, ppc_state.pc);
break;
case MIO_HEAT_FEAT_CTRL:
this->feature_control(BYTESWAP_32(value));
break;
case 0x3C:
LOG_F(9, "write %x to MIO:Aux_Ctrl register", value);
this->aux_ctrl = value;
break;
default:
LOG_F(WARNING, "write %x to unknown MIO register at %x", value, offset);
break;
}
}
void HeathrowIC::feature_control(const uint32_t value)
{
LOG_F(9, "write %x to MIO:Feat_Ctrl register", value);
this->feat_ctrl = value;
if (!(this->feat_ctrl & 1)) {
LOG_F(9, "Heathrow: Monitor sense enabled");
} else {
LOG_F(9, "Heathrow: Monitor sense disabled");
}
}
uint32_t HeathrowIC::register_dev_int(IntSrc src_id)
{
switch (src_id) {
case IntSrc::VIA_CUDA:
return 1 << 7;
case IntSrc::SCSI1:
return 1 << 1;
case IntSrc::SWIM3:
return 1 << 8;
default:
ABORT_F("Heathrow: unknown interrupt source %d", src_id);
}
return 0;
}
uint32_t HeathrowIC::register_dma_int(IntSrc src_id)
{
return 0;
}
void HeathrowIC::ack_int(uint32_t irq_id, uint8_t irq_line_state)
{
if (this->int_mask1 & MACIO_INT_MODE) { // 68k interrupt emulation mode?
if (irq_id > 0x200000) {
irq_id >>= 21;
this->int_events2 |= irq_id; // signal IRQ line change
this->int_events2 &= this->int_mask2;
// update IRQ line state
if (irq_line_state) {
this->int_levels2 |= irq_id;
} else {
this->int_levels2 &= ~irq_id;
}
} else {
irq_id <<= 11;
this->int_events1 |= irq_id; // signal IRQ line change
this->int_events1 &= this->int_mask1;
// update IRQ line state
if (irq_line_state) {
this->int_levels1 |= irq_id;
} else {
this->int_levels1 &= ~irq_id;
}
}
// signal CPU interrupt
if (this->int_events1 || this->int_events2) {
if (!this->cpu_int_latch) {
this->cpu_int_latch = true;
ppc_assert_int();
LOG_F(5, "Heathrow: CPU INT asserted, source: %d", irq_id);
} else {
LOG_F(5, "Heathrow: CPU INT already latched");
}
}
} else {
ABORT_F("Heathrow: native interrupt mode not implemented");
}
}
void HeathrowIC::ack_dma_int(uint32_t irq_id, uint8_t irq_line_state)
{
}
void HeathrowIC::clear_cpu_int()
{
if (!this->int_events1 && !this->int_events2) {
this->cpu_int_latch = false;
ppc_release_int();
LOG_F(5, "Heathrow: CPU INT latch cleared");
}
}
static const vector<string> Heathrow_Subdevices = {
"NVRAM", "ViaCuda", "Mesh", "Escc", "Swim3"
};
static const DeviceDescription Heathrow_Descriptor = {
HeathrowIC::create, Heathrow_Subdevices, {}
};
REGISTER_DEVICE(Heathrow, Heathrow_Descriptor);
+237
View File
@@ -0,0 +1,237 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** MacIO device family emulation
Mac I/O (MIO) is a family of ASICs to bring support for Apple legacy
I/O hardware to the PCI-based Power Macintosh. That legacy hardware has
existed long before Power Macintosh was introduced. It includes:
- versatile interface adapter (VIA)
- Sander-Woz integrated machine (SWIM) that is a floppy disk controller
- CUDA MCU for ADB, parameter RAM, realtime clock and power management support
- serial communication controller (SCC)
- Macintosh Enhanced SCSI Hardware (MESH)
In the 68k Macintosh era, all this hardware was implemented using several
custom chips. In a PCI-compatible Power Macintosh, the above devices are part
of the MIO chip itself. MIO's functional blocks implementing virtual devices
are called "cells", i.e. "VIA cell", "SWIM cell" etc.
MIO itself is PCI compliant while the legacy hardware it emulates isn't.
MIO occupies 512Kb of the PCI memory space divided into registers space and
DMA space. Access to emulated legacy devices is accomplished by reading from/
writing to MIO's PCI address space at predefined offsets.
MIO includes a DMA controller that offers 15 DMA channels implementing
Apple's own DMA protocol called descriptor-based DMA (DBDMA).
Official documentation (that is somewhat incomplete and erroneous) can be
found in the second chapter of the book "Macintosh Technology in the Common
Hardware Reference Platform" by Apple Computer, Inc.
*/
#ifndef MACIO_H
#define MACIO_H
#include <devices/common/dbdma.h>
#include <devices/common/mmiodevice.h>
#include <devices/common/nvram.h>
#include <devices/common/pci/pcidevice.h>
#include <devices/common/pci/pcihost.h>
#include <devices/common/scsi/mesh.h>
#include <devices/common/scsi/sc53c94.h>
#include <devices/common/viacuda.h>
#include <devices/ethernet/mace.h>
#include <devices/floppy/swim3.h>
#include <devices/memctrl/memctrlbase.h>
#include <devices/serial/escc.h>
#include <devices/sound/awacs.h>
#include <cinttypes>
#include <memory>
/** Interrupt related constants. */
#define MACIO_INT_CLR 0x80UL // clears bits in the interrupt events registers
#define MACIO_INT_MODE 0x80000000UL // interrupt mode: 0 - native, 1 - 68k-style
/** Offsets to common MacIO interrupt registers. */
enum {
MIO_INT_EVENTS2 = 0x10,
MIO_INT_MASK2 = 0x14,
MIO_INT_CLEAR2 = 0x18,
MIO_INT_LEVELS2 = 0x1C,
MIO_INT_EVENTS1 = 0x20,
MIO_INT_MASK1 = 0x24,
MIO_INT_CLEAR1 = 0x28,
MIO_INT_LEVELS1 = 0x2C
};
class IobusDevice {
public:
virtual uint16_t iodev_read(uint32_t address) = 0;
virtual void iodev_write(uint32_t address, uint16_t value) = 0;
};
class GrandCentral : public PCIDevice, public InterruptCtrl {
public:
GrandCentral();
~GrandCentral() = default;
static std::unique_ptr<HWComponent> create() {
return std::unique_ptr<GrandCentral>(new GrandCentral());
}
// MMIO device methods
uint32_t read(uint32_t rgn_start, uint32_t offset, int size);
void write(uint32_t rgn_start, uint32_t offset, uint32_t value, int size);
// InterruptCtrl methods
uint32_t register_dev_int(IntSrc src_id);
uint32_t register_dma_int(IntSrc src_id);
void ack_int(uint32_t irq_id, uint8_t irq_line_state);
void ack_dma_int(uint32_t irq_id, uint8_t irq_line_state);
void attach_iodevice(int dev_num, IobusDevice* dev_obj);
protected:
void notify_bar_change(int bar_num);
private:
uint32_t base_addr = 0;
uint8_t emmo_pin; // factory tester status, active low
// interrupt state
uint32_t int_mask = 0;
uint32_t int_levels = 0;
uint32_t int_events = 0;
bool cpu_int_latch = false;
uint32_t nvram_addr_hi;
// IOBus devices
IobusDevice* iobus_devs[6] = { nullptr };
// subdevice objects
std::unique_ptr<AwacsScreamer> awacs; // AWACS audio codec instance
NVram* nvram; // NVRAM module
MaceController* mace;
ViaCuda* viacuda; // VIA cell with Cuda MCU attached to it
EsccController* escc; // ESCC serial controller
Sc53C94* scsi_0; // external SCSI
Swim3::Swim3Ctrl* swim3; // floppy disk controller
std::unique_ptr<DMAChannel> snd_out_dma;
};
/**
Heathrow ASIC emulation
Heathrow is a MIO-compliant ASIC used in the Gossamer architecture. It's
hard-wired to PCI device number 16. Its I/O memory (512Kb) will be configured
by the Macintosh firmware to live at 0xF3000000.
Emulated subdevices and their offsets within Heathrow I/O space:
----------------------------------------------------------------
mesh(SCSI) register space: 0x00010000, DMA space: 0x00008000
bmac(ethernet) register space: 0x00011000, DMA space: 0x00008200, 0x00008300
escc(compat) register space: 0x00012000, size: 0x00001000
DMA space: 0x00008400, size: 0x00000400
escc(MacRISC) register space: 0x00013000, size: 0x00001000
DMA space: 0x00008400, size: 0x00000400
escc:ch-a register space: 0x00013020, DMA space: 0x00008400, 0x00008500
escc:ch-b register space: 0x00013000, DMA space: 0x00008600, 0x00008700
davbus(sound) register space: 0x00014000, DMA space: 0x00008800, 0x00008900
SWIM3(floppy) register space: 0x00015000, DMA space: 0x00008100
NVRAM register space: 0x00060000, size: 0x00020000
IDE register space: 0x00020000, DMA space: 0x00008b00
VIA-CUDA register space: 0x00016000, size: 0x00002000
*/
/** Heathrow specific registers. */
enum {
MIO_HEAT_ID = 0x34, // IDs register
MIO_HEAT_FEAT_CTRL = 0x38, // feature control register
};
class HeathrowIC : public PCIDevice, public InterruptCtrl {
public:
HeathrowIC();
~HeathrowIC() = default;
static std::unique_ptr<HWComponent> create() {
return std::unique_ptr<HeathrowIC>(new HeathrowIC());
}
// MMIO device methods
uint32_t read(uint32_t rgn_start, uint32_t offset, int size);
void write(uint32_t rgn_start, uint32_t offset, uint32_t value, int size);
// InterruptCtrl methods
uint32_t register_dev_int(IntSrc src_id);
uint32_t register_dma_int(IntSrc src_id);
void ack_int(uint32_t irq_id, uint8_t irq_line_state);
void ack_dma_int(uint32_t irq_id, uint8_t irq_line_state);
protected:
uint32_t dma_read(uint32_t offset, int size);
void dma_write(uint32_t offset, uint32_t value, int size);
uint32_t mio_ctrl_read(uint32_t offset, int size);
void mio_ctrl_write(uint32_t offset, uint32_t value, int size);
void notify_bar_change(int bar_num);
void feature_control(const uint32_t value);
void clear_cpu_int();
private:
uint32_t base_addr = 0;
uint32_t int_events2 = 0;
uint32_t int_mask2 = 0;
uint32_t int_levels2 = 0;
uint32_t int_events1 = 0;
uint32_t int_mask1 = 0;
uint32_t int_levels1 = 0;
uint32_t feat_ctrl = 0; // features control register
uint32_t aux_ctrl = 0; // aux features control register
bool cpu_int_latch = false;
uint8_t cpu_id = 0xE0; // CPUID field (LSB of the MIO_HEAT_ID)
uint8_t mb_id = 0x70; // Media Bay ID (bits 15:8 of the MIO_HEAT_ID)
uint8_t mon_id = 0x10; // Monitor ID (bits 23:16 of the MIO_HEAT_ID)
uint8_t fp_id = 0x70; // Flat panel ID (MSB of the MIO_HEAT_ID)
uint8_t emmo_pin; // factory tester status, active low
// subdevice objects
std::unique_ptr<AwacsScreamer> screamer; // Screamer audio codec instance
NVram* nvram; // NVRAM
ViaCuda* viacuda; // VIA cell with Cuda MCU attached to it
MESHController* mesh; // MESH SCSI cell instance
EsccController* escc; // ESCC serial controller
Swim3::Swim3Ctrl* swim3; // floppy disk controller
std::unique_ptr<DMAChannel> snd_out_dma;
};
#endif /* MACIO_H */
-158
View File
@@ -1,158 +0,0 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** MacIO device family emulation
Mac I/O (MIO) is a family of ASICs to bring support for Apple legacy
I/O hardware to the PCI-based Power Macintosh. That legacy hardware has
existed long before Power Macintosh was introduced. It includes:
- versatile interface adapter (VIA)
- Sander-Woz integrated machine (SWIM) that is a floppy disk controller
- CUDA MCU for ADB, parameter RAM, realtime clock and power management support
- serial communication controller (SCC)
- Macintosh Enhanced SCSI Hardware (MESH)
In the 68k Macintosh era, all this hardware was implemented using several
custom chips. In a PCI-compatible Power Macintosh, the above devices are part
of the MIO chip itself. MIO's functional blocks implementing virtual devices
are called "cells", i.e. "VIA cell", "SWIM cell" etc.
MIO itself is PCI compliant while the legacy hardware it emulates isn't.
MIO occupies 512Kb of the PCI memory space divided into registers space and
DMA space. Access to emulated legacy devices is accomplished by reading from/
writing to MIO's PCI address space at predefined offsets.
MIO includes a DMA controller that offers 15 DMA channels implementing
Apple's own DMA protocol called descriptor-based DMA (DBDMA).
Official documentation (that is somewhat incomplete and erroneous) can be
found in the second chapter of the book "Macintosh Technology in the Common
Hardware Reference Platform" by Apple Computer, Inc.
*/
#ifndef MACIO_H
#define MACIO_H
#include "awacs.h"
#include "dbdma.h"
#include "hwcomponent.h"
#include "memctrlbase.h"
#include "mesh.h"
#include "mmiodevice.h"
#include "nvram.h"
#include "pcidevice.h"
#include "pcihost.h"
#include "viacuda.h"
#include <cinttypes>
/**
Heathrow ASIC emulation
Author: Max Poliakovski
Heathrow is a MIO-compliant ASIC used in the Gossamer architecture. It's
hard-wired to PCI device number 16. Its I/O memory (512Kb) will be configured
by the Macintosh firmware to live at 0xF3000000.
Emulated subdevices and their offsets within Heathrow I/O space:
----------------------------------------------------------------
mesh(SCSI) register space: 0x00010000, DMA space: 0x00008000
bmac(ethernet) register space: 0x00011000, DMA space: 0x00008200, 0x00008300
escc(serial) register space: 0x00013000, size: 0x00001000
DMA space: 0x00008400, size: 0x00000400
escc:ch-a register space: 0x00013020, DMA space: 0x00008400, 0x00008500
escc:ch-b register space: 0x00013000, DMA space: 0x00008600, 0x00008700
davbus(sound) register space: 0x00014000, DMA space: 0x00008800, 0x00008900
SWIM3(floppy) register space: 0x00015000, DMA space: 0x00008100
NVRAM register space: 0x00060000, size: 0x00020000
IDE register space: 0x00020000, DMA space: 0x00008b00
VIA-CUDA register space: 0x00016000, size: 0x00002000
*/
class HeathrowIC : public PCIDevice {
public:
HeathrowIC();
~HeathrowIC();
bool supports_type(HWCompType type) {
return type == HWCompType::MMIO_DEV;
};
/* PCI device methods */
bool supports_io_space(void) {
return false;
};
uint32_t pci_cfg_read(uint32_t reg_offs, uint32_t size);
void pci_cfg_write(uint32_t reg_offs, uint32_t value, uint32_t size);
/* MMIO device methods */
uint32_t read(uint32_t reg_start, uint32_t offset, int size);
void write(uint32_t reg_start, uint32_t offset, uint32_t value, int size);
protected:
uint32_t dma_read(uint32_t offset, int size);
void dma_write(uint32_t offset, uint32_t value, int size);
uint32_t mio_ctrl_read(uint32_t offset, int size);
void mio_ctrl_write(uint32_t offset, uint32_t value, int size);
private:
uint8_t pci_cfg_hdr[256] = {
0x6B,
0x10, // vendor ID: Apple Computer Inc.
0x10,
0x00, // device ID: Heathrow Mac I/O
0x00,
0x00, // PCI command (set to 0 at power-up?)
0x00,
0x00, // PCI status (set to 0 at power-up?)
0x01, // revision ID
// class code is reported in OF property "class-code" as 0xff0000
0x00, // standard programming
0x00, // subclass code
0xFF, // class code: unassigned
0x00,
0x00, // unknown defaults
0x00,
0x00 // unknown defaults
};
uint32_t int_mask2 = 0;
uint32_t int_clear2 = 0;
uint32_t int_levels2 = 0;
uint32_t int_mask1 = 0;
uint32_t int_clear1 = 0;
uint32_t int_levels1 = 0;
uint32_t macio_id = 0xF0700008UL;
uint32_t feat_ctrl = 0; // features control register
uint32_t aux_ctrl = 0; // aux features control register
/* device cells */
ViaCuda* viacuda; // VIA cell with Cuda MCU attached to it
NVram* nvram; // NVRAM cell
AwacsScreamer *screamer; // Screamer audio codec instance
MESHController *mesh; // MESH SCSI cell instance
DMAChannel* snd_out_dma;
};
#endif /* MACIO_H */
+169
View File
@@ -0,0 +1,169 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** Hammerhead Memory Controller emulation. */
#include <devices/deviceregistry.h>
#include <devices/memctrl/hammerhead.h>
#include <loguru.hpp>
#include <cinttypes>
#include <memory>
using namespace Hammerhead;
HammerheadCtrl::HammerheadCtrl() : MemCtrlBase()
{
this->name = "Hammerhead Memory Controller";
supports_types(HWCompType::MEM_CTRL | HWCompType::MMIO_DEV);
// add MMIO region for the configuration and status registers
add_mmio_region(0xF8000000, 0x500, this);
}
uint32_t HammerheadCtrl::read(uint32_t rgn_start, uint32_t offset, int size)
{
uint32_t result;
if (offset >= HammerheadReg::BANK_0_BASE_MSB &&
offset <= HammerheadReg::BANK_25_BASE_LSB) {
offset = (offset - HammerheadReg::BANK_0_BASE_MSB) >> 4;
if (offset & 1) { // return the LSB part
result = bank_base[offset >> 1] & 0xFFU;
} else { // return the MSB part
result = bank_base[offset >> 1] >> 8;
}
goto finish;
}
switch (offset) {
case HammerheadReg::CPU_ID:
result = HH_CPU_ID_TNT;
break;
case HammerheadReg::MOTHERBOARD_ID:
result = MBID_VCI0_PRESENT << 5;
break;
case HammerheadReg::CPU_SPEED:
result = BUS_SPEED_33_MHZ << 5;
break;
case HammerheadReg::ARBITER_CONFIG:
result = this->arb_config;
break;
case HammerheadReg::WHO_AM_I:
result = BM_PRIMARY_CPU << 3;
break;
case HammerheadReg::L2_CACHE_CONFIG:
result = 0; // say there is no L2 cache
break;
default:
LOG_F(WARNING, "Hammerhead: unknown register read at offset 0x%X", offset);
return 0;
}
// Hammerhead registers are one byte wide so always place
// the result in the MSB of a multibyte read
finish:
return result << ((size - 1) << 3);
}
void HammerheadCtrl::write(uint32_t rgn_start, uint32_t offset, uint32_t value, int size)
{
// extract byte value from the MSB of a multibyte value
value = value >> ((size - 1) << 3);
if (offset >= HammerheadReg::BANK_0_BASE_MSB &&
offset <= HammerheadReg::BANK_25_BASE_LSB) {
offset = (offset - HammerheadReg::BANK_0_BASE_MSB) >> 4;
if (offset & 1) { // update the LSB part
bank_base[offset >> 1] = (bank_base[offset >> 1] & 0xFF00U) | value;
} else { // update the MSB part
bank_base[offset >> 1] = (bank_base[offset >> 1] & 0x00FFU) | (value << 8);
}
LOG_F(INFO, "Hammerhead: bank base #%d set to 0x%X", offset >> 1, bank_base[offset >> 1]);
return;
}
switch (offset) {
case HammerheadReg::MEM_TIMING_0:
LOG_F(9, "Hammerhead: MEM_TIMING_0 set to 0x%X", value);
break;
case HammerheadReg::MEM_TIMING_1:
LOG_F(9, "Hammerhead: MEM_TIMING_1 set to 0x%X", value);
break;
case HammerheadReg::REFRESH_TIMING:
LOG_F(9, "Hammerhead: REFRESH_TIMING set to 0x%X", value);
break;
case HammerheadReg::ROM_TIMING:
LOG_F(9, "Hammerhead: ROM_TIMING set to 0x%X", value);
break;
case HammerheadReg::ARBITER_CONFIG:
this->arb_config = value;
break;
default:
LOG_F(WARNING, "Hammerhead: unknown register write at offset 0x%X", offset);
}
}
void HammerheadCtrl::insert_ram_dimm(int slot_num, uint32_t capacity)
{
if (slot_num < 0 || slot_num >= 26) {
ABORT_F("Hammerhead: invalid DIMM slot number %d", slot_num);
}
switch (capacity) {
case DRAM_CAP_2MB:
case DRAM_CAP_4MB:
case DRAM_CAP_8MB:
case DRAM_CAP_16MB:
case DRAM_CAP_32MB:
case DRAM_CAP_64MB:
this->bank_size[slot_num * 2 + 0] = capacity;
break;
case DRAM_CAP_128MB:
this->bank_size[slot_num * 2 + 0] = DRAM_CAP_64MB;
this->bank_size[slot_num * 2 + 1] = DRAM_CAP_64MB;
break;
default:
ABORT_F("Hammerhead: unsupported DRAM capacity %d", capacity);
}
}
void HammerheadCtrl::map_phys_ram()
{
uint32_t total_ram = 0;
for (int i = 0; i < 26; i++) {
total_ram += this->bank_size[i];
}
LOG_F(INFO, "Hammerhead: total RAM size = %d bytes", total_ram);
if (!add_ram_region(0x00000000, total_ram)) {
ABORT_F("Hammerhead: could not allocate physical RAM storage");
}
}
static const DeviceDescription Hammerhead_Descriptor = {
HammerheadCtrl::create, {}, {}
};
REGISTER_DEVICE(Hammerhead, Hammerhead_Descriptor);
+113
View File
@@ -0,0 +1,113 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** Hammerhead Memory Controller definitions.
Author: Max Poliakovski
Hammerhead is a custom memory controller and ARBus arbiter
designed especially for the TNT family of Power Macintosh computers.
*/
#ifndef HAMMERHEAD_MEMCTRL_H
#define HAMMERHEAD_MEMCTRL_H
#include <devices/common/hwcomponent.h>
#include <devices/common/mmiodevice.h>
#include <devices/memctrl/memctrlbase.h>
#include <cinttypes>
#include <memory>
namespace Hammerhead {
#define RISC_MACHINE 0x3
#define MACH_TYPE_TNT 0x1
#define HH_CPU_ID_TNT ((RISC_MACHINE << 4) | (1 << 3) | MACH_TYPE_TNT)
// contants for the MBID field of the MOTHERBOARD_ID register
enum {
MBID_VCI0_PRESENT = 4,
MBID_PCI2_PRESENT = 2,
};
// ARBus speed constants for the CPU_SPEED register
enum {
BUS_SPEED_40_MHZ = 0,
BUS_SPEED_33_MHZ = 1,
BUS_SPEED_44_MHZ = 2,
BUS_SPEED_50_MHZ = 3
};
// bus master IDs for the WHO_AM_I register
enum {
BM_VIDEO_BRIDGE = 1 << 4,
BM_PCI_BRIDGE_1 = 1 << 3,
BM_PCI_BRIDGE_2 = 1 << 2,
BM_PRIMARY_CPU = 1 << 1,
BM_SECONDARY_CPU = 1 << 0
};
// Configuration and status registers.
enum HammerheadReg : uint16_t {
CPU_ID = 0x00,
ASIC_REVISION = 0x10,
MOTHERBOARD_ID = 0x20,
CPU_SPEED = 0x30,
MB_DRAM_CONFIG = 0x40,
MEM_TIMING_0 = 0x50,
MEM_TIMING_1 = 0x60,
REFRESH_TIMING = 0x70,
ROM_TIMING = 0x80,
ARBITER_CONFIG = 0x90,
ARBUS_TIMEOUT = 0xA0,
WHO_AM_I = 0xB0,
L2_CACHE_CONFIG = 0xE0,
BANK_0_BASE_MSB = 0x1C0,
BANK_25_BASE_LSB = 0x4F0,
};
}; // namespace Hammerhead
class HammerheadCtrl : public MemCtrlBase, public MMIODevice {
public:
HammerheadCtrl();
~HammerheadCtrl() = default;
static std::unique_ptr<HWComponent> create() {
return std::unique_ptr<HammerheadCtrl>(new HammerheadCtrl());
}
// MMIODevice methods
uint32_t read(uint32_t rgn_start, uint32_t offset, int size);
void write(uint32_t rgn_start, uint32_t offset, uint32_t value, int size);
void insert_ram_dimm(int slot_num, uint32_t capacity);
void map_phys_ram();
private:
uint8_t arb_config = 0;
uint16_t bank_base[26];
uint32_t bank_size[26] = { 0 };
};
#endif // HAMMERHEAD_MEMCTRL_H
+18 -16
View File
@@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
Copyright (C) 2018-21 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@@ -24,47 +24,49 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
Author: Max Poliakovski
*/
#include "hmc.h"
#include <devices/deviceregistry.h>
#include <devices/common/hwcomponent.h>
#include <devices/memctrl/hmc.h>
HMC::HMC() : MemCtrlBase()
{
this->name = "Highspeed Memory Controller";
supports_types(HWCompType::MEM_CTRL | HWCompType::MMIO_DEV);
/* add memory mapped I/O region for the HMC control register */
add_mmio_region(0x50F40000, 0x10000, this);
this->config_reg = 0ULL;
this->ctrl_reg = 0ULL;
this->bit_pos = 0;
}
bool HMC::supports_type(HWCompType type) {
if (type == HWCompType::MEM_CTRL || type == HWCompType::MMIO_DEV) {
return true;
} else {
return false;
}
}
uint32_t HMC::read(uint32_t reg_start, uint32_t offset, int size)
uint32_t HMC::read(uint32_t rgn_start, uint32_t offset, int size)
{
if (!offset)
return !!(this->config_reg & (1ULL << this->bit_pos++));
return !!(this->ctrl_reg & (1ULL << this->bit_pos++));
else
return 0; /* FIXME: what should be returned for invalid offsets? */
}
void HMC::write(uint32_t reg_start, uint32_t offset, uint32_t value, int size)
void HMC::write(uint32_t rgn_start, uint32_t offset, uint32_t value, int size)
{
uint64_t bit;
switch(offset) {
case 0:
bit = 1ULL << this->bit_pos++;
this->config_reg = (value & 1) ? this->config_reg | bit :
this->config_reg & ~bit;
this->ctrl_reg = (value & 1) ? this->ctrl_reg | bit :
this->ctrl_reg & ~bit;
break;
case 8: /* writing to HMCBase + 8 resets internal bit position */
this->bit_pos = 0;
break;
}
}
static const DeviceDescription Hmc_Descriptor = {
HMC::create, {}, {}
};
REGISTER_DEVICE(HMC, Hmc_Descriptor);
+19 -8
View File
@@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
Copyright (C) 2018-21 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@@ -31,25 +31,36 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#ifndef HMC_H
#define HMC_H
#include "hwcomponent.h"
#include "memctrlbase.h"
#include "mmiodevice.h"
#include <devices/common/mmiodevice.h>
#include <devices/memctrl/memctrlbase.h>
#include <cinttypes>
#include <memory>
/** Bit in the HMC control register telling us where the frame bufffer
for the on-board video is located. */
#define HMC_VBASE_BIT 33
class HMC : public MemCtrlBase, public MMIODevice {
public:
HMC();
~HMC() = default;
bool supports_type(HWCompType type);
static std::unique_ptr<HWComponent> create() {
return std::unique_ptr<HMC>(new HMC());
}
/* MMIODevice methods */
uint32_t read(uint32_t reg_start, uint32_t offset, int size);
void write(uint32_t reg_start, uint32_t offset, uint32_t value, int size);
uint32_t read(uint32_t rgn_start, uint32_t offset, int size);
void write(uint32_t rgn_start, uint32_t offset, uint32_t value, int size);
uint64_t get_control_reg(void) {
return this->ctrl_reg;
};
private:
int bit_pos;
uint64_t config_reg;
uint64_t ctrl_reg;
};
#endif // HMC_H
@@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
Copyright (C) 2018-21 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@@ -19,13 +19,13 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <devices/memctrl/memctrlbase.h>
#include <algorithm> // to shut up MSVC errors (:
#include <cstring>
#include <string>
#include <vector>
#include "memctrlbase.h"
MemCtrlBase::~MemCtrlBase() {
for (auto& entry : address_map) {
@@ -48,7 +48,7 @@ AddressMapEntry* MemCtrlBase::find_range(uint32_t addr) {
return entry;
}
return 0;
return nullptr;
}
@@ -146,3 +146,14 @@ bool MemCtrlBase::add_mmio_region(uint32_t start_addr, uint32_t size, MMIODevice
return true;
}
AddressMapEntry* MemCtrlBase::find_rom_region()
{
for (auto& entry : address_map) {
if (entry->type == RT_ROM) {
return entry;
}
}
return nullptr;
}
@@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@@ -22,11 +22,23 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#ifndef MEMORY_CONTROLLER_BASE_H
#define MEMORY_CONTROLLER_BASE_H
#include "mmiodevice.h"
#include <devices/common/mmiodevice.h>
#include <cinttypes>
#include <string>
#include <vector>
// Common DRAM capacities.
enum {
DRAM_CAP_2MB = (1 << 21),
DRAM_CAP_4MB = (1 << 22),
DRAM_CAP_8MB = (1 << 23),
DRAM_CAP_16MB = (1 << 24),
DRAM_CAP_32MB = (1 << 25),
DRAM_CAP_64MB = (1 << 26),
DRAM_CAP_128MB = (1 << 27),
};
enum RangeType {
RT_ROM = 1, /* read-only memory */
RT_RAM = 2, /* random access memory */
@@ -60,6 +72,7 @@ public:
virtual bool set_data(uint32_t reg_addr, const uint8_t* data, uint32_t size);
AddressMapEntry* find_range(uint32_t addr);
AddressMapEntry* find_rom_region();
protected:
bool add_mem_region(
+111 -89
View File
@@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@@ -19,55 +19,67 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** MPC106 (Grackle) emulation
/** MPC106 (Grackle) emulation. */
Author: Max Poliakovski
*/
#include <devices/common/hwcomponent.h>
#include <devices/common/mmiodevice.h>
#include <devices/deviceregistry.h>
#include <devices/memctrl/memctrlbase.h>
#include <devices/memctrl/mpc106.h>
#include <memaccess.h>
#include <cinttypes>
#include <cstring>
#include <iostream>
#include <loguru.hpp>
#include <string>
#include <vector>
#include "hwcomponent.h"
#include "memctrlbase.h"
#include "memaccess.h"
#include "mmiodevice.h"
#include "mpc106.h"
MPC106::MPC106() : MemCtrlBase(), PCIDevice("Grackle PCI host bridge") {
MPC106::MPC106() : MemCtrlBase(), PCIDevice("Grackle"), PCIHost()
{
this->name = "Grackle";
/* add PCI/ISA I/O space, 64K for now */
supports_types(HWCompType::MEM_CTRL | HWCompType::MMIO_DEV |
HWCompType::PCI_HOST | HWCompType::PCI_DEV);
// populate PCI config header
this->vendor_id = PCI_VENDOR_MOTOROLA;
this->device_id = 0x0002;
this->class_rev = 0x06000040;
this->cache_ln_sz = 8;
this->command = 6;
this->status = 0x80;
// add PCI/ISA I/O space, 64K for now
add_mmio_region(0xFE000000, 0x10000, this);
/* add memory mapped I/O region for MPC106 registers */
// add memory mapped I/O region for MPC106 registers
add_mmio_region(0xFEC00000, 0x300000, this);
this->pci_0_bus.clear();
this->io_space_devs.clear();
}
MPC106::~MPC106() {
this->pci_0_bus.clear();
}
int MPC106::device_postinit()
{
std::string pci_dev_name;
bool MPC106::supports_type(HWCompType type) {
if (type == HWCompType::MEM_CTRL || type == HWCompType::MMIO_DEV ||
type == HWCompType::PCI_HOST || type == HWCompType::PCI_DEV) {
return true;
} else {
return false;
static const std::map<std::string, int> pci_slots = {
{"pci_A1", 0xD}, {"pci_B1", 0xE}, {"pci_C1", 0xF}
};
for (auto& slot : pci_slots) {
pci_dev_name = GET_STR_PROP(slot.first);
if (!pci_dev_name.empty()) {
this->attach_pci_device(pci_dev_name, slot.second);
}
}
return 0;
}
uint32_t MPC106::read(uint32_t reg_start, uint32_t offset, int size) {
uint32_t MPC106::read(uint32_t rgn_start, uint32_t offset, int size) {
uint32_t result;
if (reg_start == 0xFE000000) {
/* broadcast I/O request to devices that support I/O space
until a device returns true that means "request accepted" */
if (rgn_start == 0xFE000000) {
// broadcast I/O request to devices that support I/O space
// until a device returns true that means "request accepted"
for (auto& dev : this->io_space_devs) {
if (dev->pci_io_read(offset, size, &result)) {
return result;
@@ -81,15 +93,15 @@ uint32_t MPC106::read(uint32_t reg_start, uint32_t offset, int size) {
}
}
/* FIXME: reading from CONFIG_ADDR is ignored for now */
// FIXME: reading from CONFIG_ADDR is ignored for now
return 0;
}
void MPC106::write(uint32_t reg_start, uint32_t offset, uint32_t value, int size) {
if (reg_start == 0xFE000000) {
/* broadcast I/O request to devices that support I/O space
until a device returns true that means "request accepted" */
void MPC106::write(uint32_t rgn_start, uint32_t offset, uint32_t value, int size) {
if (rgn_start == 0xFE000000) {
// broadcast I/O request to devices that support I/O space
// until a device returns true that means "request accepted"
for (auto& dev : this->io_space_devs) {
if (dev->pci_io_write(offset, value, size)) {
return;
@@ -110,31 +122,33 @@ uint32_t MPC106::pci_read(uint32_t size) {
int bus_num, dev_num, fun_num, reg_offs;
bus_num = (this->config_addr >> 8) & 0xFF;
if (bus_num) {
LOG_F(
ERROR,
"%s err: read attempt from non-local PCI bus, config_addr = %x \n",
this->name.c_str(),
this->config_addr);
return 0;
}
dev_num = (this->config_addr >> 19) & 0x1F;
fun_num = (this->config_addr >> 16) & 0x07;
reg_offs = (this->config_addr >> 24) & 0xFC;
if (bus_num) {
LOG_F(
ERROR,
"%s err: read attempt from non-local PCI bus, config_addr = %x %02x:%02x.%x @%02x.%c",
this->name.c_str(), this->config_addr, bus_num, dev_num, fun_num, reg_offs,
size == 4 ? 'l' : size == 2 ? 'w' : size == 1 ? 'b' : '0' + size
);
return 0xFFFFFFFFUL; // PCI spec §6.1
}
if (dev_num == 0 && fun_num == 0) { // dev_num 0 is assigned to myself
return this->pci_cfg_read(reg_offs, size);
} else {
if (this->pci_0_bus.count(dev_num)) {
return this->pci_0_bus[dev_num]->pci_cfg_read(reg_offs, size);
if (this->dev_map.count(dev_num)) {
return this->dev_map[dev_num]->pci_cfg_read(reg_offs, size);
} else {
LOG_F(
ERROR,
"%s err: read attempt from non-existing PCI device %d \n",
this->name.c_str(),
dev_num);
return 0;
"%s err: read attempt from non-existing PCI device %02x:%02x.%x @%02x.%c",
this->name.c_str(), bus_num, dev_num, fun_num, reg_offs,
size == 4 ? 'l' : size == 2 ? 'w' : size == 1 ? 'b' : '0' + size
);
return 0xFFFFFFFFUL; // PCI spec §6.1
}
}
@@ -145,79 +159,72 @@ void MPC106::pci_write(uint32_t value, uint32_t size) {
int bus_num, dev_num, fun_num, reg_offs;
bus_num = (this->config_addr >> 8) & 0xFF;
if (bus_num) {
LOG_F(
ERROR,
"%s err: write attempt to non-local PCI bus, config_addr = %x \n",
this->name.c_str(),
this->config_addr);
return;
}
dev_num = (this->config_addr >> 19) & 0x1F;
fun_num = (this->config_addr >> 16) & 0x07;
reg_offs = (this->config_addr >> 24) & 0xFC;
if (bus_num) {
LOG_F(
ERROR,
"%s err: write attempt to non-local PCI bus, config_addr = %x %02x:%02x.%x @%02x.%c = %0*x",
this->name.c_str(), this->config_addr, bus_num, dev_num, fun_num, reg_offs,
size == 4 ? 'l' : size == 2 ? 'w' : size == 1 ? 'b' : '0' + size,
size * 2, value
);
return;
}
if (dev_num == 0 && fun_num == 0) { // dev_num 0 is assigned to myself
this->pci_cfg_write(reg_offs, value, size);
} else {
if (this->pci_0_bus.count(dev_num)) {
this->pci_0_bus[dev_num]->pci_cfg_write(reg_offs, value, size);
if (this->dev_map.count(dev_num)) {
this->dev_map[dev_num]->pci_cfg_write(reg_offs, value, size);
} else {
LOG_F(
ERROR,
"%s err: write attempt to non-existing PCI device %d \n",
this->name.c_str(),
dev_num);
"%s err: write attempt to non-existing PCI device %02x:%02x.%x @%02x.%c = %0*x",
this->name.c_str(), bus_num, dev_num, fun_num, reg_offs,
size == 4 ? 'l' : size == 2 ? 'w' : size == 1 ? 'b' : '0' + size,
size * 2, value
);
}
}
}
uint32_t MPC106::pci_cfg_read(uint32_t reg_offs, uint32_t size) {
#ifdef MPC106_DEBUG
LOG_F(9, "read from Grackle register %08X\n", reg_offs);
LOG_F(9, "read from Grackle register %08X", reg_offs);
#endif
if (reg_offs < 64) {
return PCIDevice::pci_cfg_read(reg_offs, size);
}
return read_mem(&this->my_pci_cfg_hdr[reg_offs], size);
}
void MPC106::pci_cfg_write(uint32_t reg_offs, uint32_t value, uint32_t size) {
#ifdef MPC106_DEBUG
LOG_F(9, "write %08X to Grackle register %08X\n", value, reg_offs);
LOG_F(9, "write %08X to Grackle register %08X", value, reg_offs);
#endif
if (reg_offs < 64) {
PCIDevice::pci_cfg_write(reg_offs, value, size);
return;
}
// FIXME: implement write-protection for read-only registers
write_mem(&this->my_pci_cfg_hdr[reg_offs], value, size);
if (this->my_pci_cfg_hdr[0xF2] & 8) {
#ifdef MPC106_DEBUG
LOG_F(9, "MPC106: MCCR1[MEMGO] was set! \n");
LOG_F(9, "MPC106: MCCR1[MEMGO] was set!");
#endif
setup_ram();
}
}
bool MPC106::pci_register_device(int dev_num, PCIDevice* dev_instance) {
if (this->pci_0_bus.count(dev_num)) // is dev_num already registered?
return false;
this->pci_0_bus[dev_num] = dev_instance;
dev_instance->set_host(this);
if (dev_instance->supports_io_space()) {
this->io_space_devs.push_back(dev_instance);
}
return true;
}
bool MPC106::pci_register_mmio_region(uint32_t start_addr, uint32_t size, PCIDevice* obj) {
// FIXME: add sanity checks!
return this->add_mmio_region(start_addr, size, obj);
}
void MPC106::setup_ram() {
uint32_t mem_start, mem_end, ext_mem_start, ext_mem_end, bank_start, bank_end;
uint32_t ram_size = 0;
@@ -242,12 +249,27 @@ void MPC106::setup_ram() {
bank_end = (((ext_mem_end >> bank * 8) & 3) << 30) |
(((mem_end >> bank * 8) & 0xFF) << 20) | 0xFFFFFUL;
if (bank && bank_start != ram_size)
LOG_F(WARNING, "MPC106: RAM not contiguous! \n");
LOG_F(WARNING, "MPC106: RAM not contiguous!");
ram_size += bank_end - bank_start + 1;
}
}
if (!this->add_ram_region(0, ram_size)) {
LOG_F(ERROR, "MPC106 RAM allocation failed! \n");
LOG_F(ERROR, "MPC106 RAM allocation failed!");
}
}
static const PropMap Grackle_Properties = {
{"pci_A1",
new StrProperty("")},
{"pci_B1",
new StrProperty("")},
{"pci_C1",
new StrProperty("")},
};
static const DeviceDescription Grackle_Descriptor = {
MPC106::create, {}, Grackle_Properties
};
REGISTER_DEVICE(Grackle, Grackle_Descriptor);
+16 -22
View File
@@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@@ -18,9 +18,8 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** MPC106 (Grackle) emulation
Author: Max Poliakovski
/** MPC106 (Grackle) emulation
Grackle IC is a combined memory and PCI controller manufactured by Motorola.
It's the central device in the Gossamer architecture.
@@ -35,27 +34,28 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#ifndef MPC106_H_
#define MPC106_H_
#include "hwcomponent.h"
#include "memctrlbase.h"
#include "mmiodevice.h"
#include "pcidevice.h"
#include "pcihost.h"
#include <cinttypes>
#include <unordered_map>
#include <devices/common/mmiodevice.h>
#include <devices/common/pci/pcidevice.h>
#include <devices/common/pci/pcihost.h>
#include <devices/memctrl/memctrlbase.h>
#include <cinttypes>
#include <memory>
#include <unordered_map>
class MPC106 : public MemCtrlBase, public PCIDevice, public PCIHost {
public:
MPC106();
~MPC106();
~MPC106() = default;
bool supports_type(HWCompType type);
static std::unique_ptr<HWComponent> create() {
return std::unique_ptr<MPC106>(new MPC106());
}
uint32_t read(uint32_t reg_start, uint32_t offset, int size);
void write(uint32_t reg_start, uint32_t offset, uint32_t value, int size);
uint32_t read(uint32_t rgn_start, uint32_t offset, int size);
void write(uint32_t rgn_start, uint32_t offset, uint32_t value, int size);
/* PCI host bridge API */
bool pci_register_device(int dev_num, PCIDevice* dev_instance);
int device_postinit();
protected:
/* PCI access */
@@ -70,8 +70,6 @@ protected:
return true;
};
bool pci_register_mmio_region(uint32_t start_addr, uint32_t size, PCIDevice* obj);
void setup_ram(void);
private:
@@ -161,10 +159,6 @@ private:
};
uint32_t config_addr;
// uint32_t config_data;
std::unordered_map<int, PCIDevice*> pci_0_bus;
std::vector<PCIDevice*> io_space_devs;
};
#endif
+207
View File
@@ -0,0 +1,207 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** Platinum Memory/Display Controller emulation. */
#include <devices/deviceregistry.h>
#include <devices/memctrl/platinum.h>
#include <devices/video/displayid.h>
#include <loguru.hpp>
#include <cinttypes>
#include <memory>
using namespace Platinum;
PlatinumCtrl::PlatinumCtrl() : MemCtrlBase()
{
this->name = "Platinum Memory Controller";
supports_types(HWCompType::MEM_CTRL | HWCompType::MMIO_DEV);
// add MMIO region for the configuration and status registers
add_mmio_region(0xF8000000, 0x500, this);
// determine actual VRAM size (min. 1MB, max. 4MB)
this->vram_size = 1 << 20;
// insert video memory region into the main memory map
this->add_ram_region(0xF1000000UL, this->vram_size);
// initialize the CPUID register with the following CPU:
// PowerPC 601 @ 75 MHz, bus frequency: 37,5 MHz
this->cpu_id = (0x3001 << 16) | ClkSrc3 | (CpuSpeed3::CPU_75_BUS_38 << 8);
this->display_id = std::unique_ptr<DisplayID> (new DisplayID());
}
uint32_t PlatinumCtrl::read(uint32_t rgn_start, uint32_t offset, int size)
{
if (size != 4) {
LOG_F(WARNING, "Platinum: unsupported register access size %d!", size);
return 0;
}
switch (offset) {
case PlatinumReg::CPU_ID:
return this->cpu_id;
case PlatinumReg::DRAM_REFRESH:
return this->dram_refresh;
case PlatinumReg::BANK_0_BASE:
case PlatinumReg::BANK_1_BASE:
case PlatinumReg::BANK_2_BASE:
case PlatinumReg::BANK_3_BASE:
case PlatinumReg::BANK_4_BASE:
case PlatinumReg::BANK_5_BASE:
case PlatinumReg::BANK_6_BASE:
case PlatinumReg::BANK_7_BASE:
return this->bank_base[(offset - PlatinumReg::BANK_0_BASE) >> 4];
case PlatinumReg::CACHE_CONFIG:
return 0; // report no L2 cache installed
case PlatinumReg::FB_BASE_ADDR:
return this->fb_addr;
case PlatinumReg::MON_ID_SENSE:
LOG_F(INFO, "Platinum: display sense read");
return (this->cur_mon_id ^ 7);
default:
LOG_F(WARNING, "Platinum: unknown register read at offset 0x%X", offset);
}
return 0;
}
void PlatinumCtrl::write(uint32_t rgn_start, uint32_t offset, uint32_t value, int size)
{
switch (offset) {
case PlatinumReg::ROM_TIMING:
this->rom_timing = value;
break;
case PlatinumReg::DRAM_TIMING:
this->dram_timing = value;
break;
case PlatinumReg::DRAM_REFRESH:
this->dram_refresh = value;
break;
case PlatinumReg::BANK_0_BASE:
case PlatinumReg::BANK_1_BASE:
case PlatinumReg::BANK_2_BASE:
case PlatinumReg::BANK_3_BASE:
case PlatinumReg::BANK_4_BASE:
case PlatinumReg::BANK_5_BASE:
case PlatinumReg::BANK_6_BASE:
case PlatinumReg::BANK_7_BASE:
this->bank_base[(offset - PlatinumReg::BANK_0_BASE) >> 4] = value;
break;
case PlatinumReg::FB_BASE_ADDR:
this->fb_addr = value;
LOG_F(INFO, "Platinum: Framebuffer address set to 0x%X", value);
break;
case PlatinumReg::FB_CONFIG_1:
this->fb_config_1 = value;
break;
case PlatinumReg::FB_CONFIG_2:
this->fb_config_2 = value;
break;
case PlatinumReg::VMEM_PAGE_MODE:
this->vmem_fp_mode = value;
break;
case PlatinumReg::MON_ID_SENSE:
LOG_F(INFO, "Platinum: display sense written with 0x%X", value);
this->cur_mon_id = this->display_id->read_monitor_sense(value & 7, value ^ 7);
break;
case PlatinumReg::FB_RESET:
this->fb_reset = value;
break;
case PlatinumReg::VRAM_REFRESH:
this->vram_refresh = value;
break;
case PlatinumReg::SWATCH_CONFIG:
this->swatch_config = value;
break;
case PlatinumReg::SWATCH_INT_MASK:
this->swatch_int_mask = value;
break;
case PlatinumReg::SWATCH_HAL:
LOG_F(INFO, "Swatch HAL set to 0x%X", value);
break;
case PlatinumReg::SWATCH_HFP:
LOG_F(INFO, "Swatch HFP set to 0x%X", value);
break;
case PlatinumReg::SWATCH_HPIX:
LOG_F(INFO, "Swatch HPIX set to 0x%X", value);
break;
case PlatinumReg::SWATCH_VAL:
LOG_F(INFO, "Swatch VAL set to 0x%X", value);
break;
case PlatinumReg::SWATCH_VFP:
LOG_F(INFO, "Swatch VFP set to 0x%X", value);
break;
default:
LOG_F(WARNING, "Platinum: unknown register write at offset 0x%X", offset);
}
}
void PlatinumCtrl::insert_ram_dimm(int slot_num, uint32_t capacity)
{
if (slot_num < 0 || slot_num >= 4) {
ABORT_F("Platinum: invalid DIMM slot %d", slot_num);
}
switch (capacity) {
case DRAM_CAP_2MB:
case DRAM_CAP_4MB:
case DRAM_CAP_8MB:
case DRAM_CAP_16MB:
case DRAM_CAP_32MB:
case DRAM_CAP_64MB:
this->bank_size[slot_num * 2 + 0] = capacity;
break;
case DRAM_CAP_128MB:
this->bank_size[slot_num * 2 + 0] = DRAM_CAP_64MB;
this->bank_size[slot_num * 2 + 1] = DRAM_CAP_64MB;
break;
default:
ABORT_F("Platinum: unsupported DRAM capacity %d", capacity);
}
}
void PlatinumCtrl::map_phys_ram()
{
uint32_t total_ram = 0;
for (int i = 0; i < 8; i++) {
total_ram += this->bank_size[i];
}
if (total_ram > DRAM_CAP_64MB) {
ABORT_F("Platinum: RAM bigger than 64MB not supported yet");
}
if (!add_ram_region(0x00000000, total_ram)) {
ABORT_F("Platinum: could not allocate RAM storage");
}
}
static const DeviceDescription Platinum_Descriptor = {
PlatinumCtrl::create, {}, {}
};
REGISTER_DEVICE(Platinum, Platinum_Descriptor);
+192
View File
@@ -0,0 +1,192 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** Platinum Memory Controller definitions.
Author: Max Poliakovski
Platinum is a single chip memory and video subsystem controller designed
especially for the Power Macintosh 7200 computer, code name Catalyst.
*/
#ifndef PLATINUM_MEMCTRL_H
#define PLATINUM_MEMCTRL_H
#include <devices/common/hwcomponent.h>
#include <devices/common/mmiodevice.h>
#include <devices/memctrl/memctrlbase.h>
#include <devices/video/displayid.h>
#include <cinttypes>
#include <memory>
namespace Platinum {
/** Clock source encoding in the CPUID register */
enum {
ClkSrc0 = 0x0000, // 33 MHz
ClkSrc2 = 0x8000, // 20 MHz
ClkSrc3 = 0x8040 // 31,3344 MHz
};
/** CPU/Bus speed encoding for ClkSrc0 */
enum CpuSpeed0 {
CPU_66_BUS_33 = 0,
CPU_80_BUS_40_1 = 1,
CPU_80_BUS_27_1 = 2,
CPU_100_BUS_50 = 3,
CPU_100_BUS_33 = 4,
CPU_120_BUS_60 = 5,
CPU_120_BUS_40 = 6,
CPU_120_BUS_30 = 7,
CPU_133_BUS_66 = 8,
CPU_133_BUS_44 = 9,
CPU_133_BUS_33 = 10,
CPU_150_BUS_75 = 11,
CPU_150_BUS_50 = 12,
CPU_150_BUS_37 = 13
};
/** CPU/Bus speed encoding for ClkSrc2 */
enum CpuSpeed2 {
CPU_40_BUS_20 = 0,
CPU_48_BUS_24 = 1,
CPU_48_BUS_16 = 2,
CPU_60_BUS_30 = 3,
CPU_60_BUS_20 = 4,
CPU_72_BUS_36 = 5,
CPU_72_BUS_24 = 6,
CPU_72_BUS_18 = 7,
CPU_80_BUS_40_2 = 8,
CPU_80_BUS_27_2 = 9,
CPU_80_BUS_20 = 10,
CPU_90_BUS_45 = 11,
CPU_90_BUS_30 = 12,
CPU_90_BUS_22 = 13
};
/** CPU/Bus speed encoding for ClkSrc3 */
enum CpuSpeed3 {
CPU_62_BUS_31 = 0,
CPU_75_BUS_38 = 1, // actual bus frequency: 37500000 Hz
CPU_75_BUS_25 = 2,
CPU_94_BUS_47 = 3,
CPU_94_BUS_31 = 4,
CPU_113_BUS_56 = 5,
CPU_113_BUS_38 = 6, // actual bus frequency: 37500000 Hz
CPU_113_BUS_28 = 7,
CPU_125_BUS_64 = 8, // actual bus frequency: 63500000 Hz
CPU_125_BUS_42 = 9,
CPU_125_BUS_31 = 10,
CPU_141_BUS_72 = 11, // actual bus frequency: 71500000 Hz
CPU_141_BUS_47 = 12,
CPU_141_BUS_35 = 13
};
/** Configuration and status register offsets. */
enum PlatinumReg : uint32_t {
CPU_ID = 0x000,
ASIC_REVISION = 0x010,
ROM_TIMING = 0x020,
CACHE_CONFIG = 0x030,
DRAM_TIMING = 0x040,
DRAM_REFRESH = 0x050,
BANK_0_BASE = 0x060,
BANK_1_BASE = 0x070,
BANK_2_BASE = 0x080,
BANK_3_BASE = 0x090,
BANK_4_BASE = 0x0A0,
BANK_5_BASE = 0x0B0,
BANK_6_BASE = 0x0C0,
BANK_7_BASE = 0x0D0,
GP_SW_SCRATCH = 0x0E0,
PCI_ADDR_MASK = 0x0F0,
FB_BASE_ADDR = 0x100,
FB_CONFIG_1 = 0x140,
FB_CONFIG_2 = 0x150,
VMEM_PAGE_MODE = 0x160,
MON_ID_SENSE = 0x170,
FB_RESET = 0x180,
VRAM_REFRESH = 0x1B0,
SWATCH_CONFIG = 0x200,
SWATCH_INT_MASK = 0x210,
SWATCH_HAL = 0x300,
SWATCH_HFP = 0x310,
SWATCH_HPIX = 0x320,
SWATCH_VAL = 0x370,
SWATCH_VFP = 0x380,
IRIDIUM_CONFIG = 0x4A0,
};
// FB_RESET register bits.
enum {
VRAM_SM_RESET = (1 << 0), // VRAM state machine reset
VREFRESH_SM_RESET = (1 << 1), // Video refresh state machine reset
SWATCH_RESET = (1 << 2), // Swatch reset
};
}; // namespace Platinum
class PlatinumCtrl : public MemCtrlBase, public MMIODevice {
public:
PlatinumCtrl();
~PlatinumCtrl() = default;
static std::unique_ptr<HWComponent> create() {
return std::unique_ptr<PlatinumCtrl>(new PlatinumCtrl());
}
/* MMIODevice methods */
uint32_t read(uint32_t rgn_start, uint32_t offset, int size);
void write(uint32_t rgn_start, uint32_t offset, uint32_t value, int size);
void insert_ram_dimm(int slot_num, uint32_t capacity);
void map_phys_ram();
private:
uint32_t cpu_id;
uint8_t cpu_type; // 0 - MPC601, 1 - 603/604 CPU
// memory controller state
uint32_t rom_timing = 0;
uint32_t dram_timing = 0xEFF;
uint32_t dram_refresh = 0x1F4;
uint32_t bank_base[8];
uint32_t bank_size[8] = { 0 };
// display controller state
uint32_t fb_addr = 0xF1000000;
uint32_t fb_config_1 = 0x1F00;
uint32_t fb_config_2 = 0x1FFF;
uint32_t fb_reset = 7;
uint32_t vram_refresh = 0x1F4;
uint32_t vram_size = 0;
uint8_t vmem_fp_mode = 0;
uint8_t cur_mon_id = 0;
// video timing generator (Swatch) state
uint32_t swatch_config = 0xFFD;
uint32_t swatch_int_mask = 0;
std::unique_ptr<DisplayID> display_id;
};
#endif // PLATINUM_MEMCTRL_H
@@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
Copyright (C) 2018-21 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@@ -47,12 +47,13 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#ifndef SPD_EEPROM_H
#define SPD_EEPROM_H
#include "hwcomponent.h"
#include "i2c.h"
#include <devices/common/hwcomponent.h>
#include <devices/common/i2c/i2c.h>
#include <loguru.hpp>
#include <cinttypes>
#include <stdexcept>
#include <string>
#include <loguru.hpp>
enum RAMType : int { SDRAM = 4 };
@@ -62,14 +63,11 @@ public:
SpdSdram168(uint8_t addr) {
this->dev_addr = addr;
this->pos = 0;
supports_types(HWCompType::RAM);
};
~SpdSdram168() = default;
bool supports_type(HWCompType type) {
return type == HWCompType::RAM;
};
void set_capacity(int capacity_megs) {
switch (capacity_megs) {
case 32:
-78
View File
@@ -1,78 +0,0 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef PCI_DEVICE_H
#define PCI_DEVICE_H
#include "mmiodevice.h"
#include "pcihost.h"
#include <cinttypes>
#include <string>
/* convert little-endian DWORD to big-endian DWORD */
#define LE2BE(x) (x >> 24) | ((x & 0x00FF0000) >> 8) | ((x & 0x0000FF00) << 8) | (x << 24)
/** PCI configuration space registers offsets */
enum {
CFG_REG_CMD = 0x04, // command/status register
CFG_REG_BAR0 = 0x10, // base address register 0
CFG_REG_BAR1 = 0x14, // base address register 1
CFG_REG_BAR2 = 0x18, // base address register 2
CFG_REG_BAR3 = 0x1C, // base address register 3
CFG_REG_BAR4 = 0x20, // base address register 4
CFG_REG_BAR5 = 0x24, // base address register 5
CFG_EXP_BASE = 0x30, // expansion ROM base
};
class PCIDevice : public MMIODevice {
public:
PCIDevice(std::string name) {
this->pci_name = name;
};
virtual ~PCIDevice() = default;
virtual bool supports_io_space(void) = 0;
/* I/O space access methods */
virtual bool pci_io_read(uint32_t offset, uint32_t size, uint32_t* res) {
return false;
};
virtual bool pci_io_write(uint32_t offset, uint32_t value, uint32_t size) {
return false;
};
/* configuration space access methods */
virtual uint32_t pci_cfg_read(uint32_t reg_offs, uint32_t size) = 0;
virtual void pci_cfg_write(uint32_t reg_offs, uint32_t value, uint32_t size) = 0;
virtual void set_host(PCIHost* host_instance) {
this->host_instance = host_instance;
};
protected:
std::string pci_name; // human-readable device name
PCIHost* host_instance; // host bridge instance to call back
uint32_t base_addr; // base address register 0
};
#endif /* PCI_DEVICE_H */
-14
View File
@@ -1,14 +0,0 @@
#ifndef PCI_HOST_H
#define PCI_HOST_H
#include <cinttypes>
class PCIDevice; // forward declaration to prevent errors
class PCIHost {
public:
virtual bool pci_register_device(int dev_num, PCIDevice* dev_instance) = 0;
virtual bool pci_register_mmio_region(uint32_t start_addr, uint32_t size, PCIDevice* obj) = 0;
};
#endif /* PCI_HOST_H */
+480
View File
@@ -0,0 +1,480 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** Character I/O backend implementations. */
#include <devices/serial/chario.h>
#include <loguru.hpp>
#include <cinttypes>
#include <cstring>
#include <memory>
bool CharIoNull::rcv_char_available()
{
return false;
}
int CharIoNull::xmit_char(uint8_t c)
{
return 0;
}
int CharIoNull::rcv_char(uint8_t *c)
{
*c = 0xFF;
return 0;
}
//======================== STDIO character I/O backend ========================
#ifdef _WIN32
#include <fcntl.h>
#include <io.h>
#include <windows.h>
HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE);
HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD old_in_mode, old_out_mode;
int old_stdin_trans_mode;
void CharIoStdin::mysig_handler(int signum) {
SetStdHandle(signum, hInput);
SetStdHandle(signum, hOutput);
}
int CharIoStdin::rcv_enable() {
if (this->stdio_inited)
return 0;
GetConsoleMode(hInput, &old_in_mode);
GetConsoleMode(hOutput, &old_out_mode);
DWORD new_in_mode = old_in_mode;
new_in_mode &= ~ENABLE_ECHO_INPUT;
new_in_mode &= ~ENABLE_LINE_INPUT;
new_in_mode &= ~ENABLE_PROCESSED_INPUT;
new_in_mode |= ENABLE_EXTENDED_FLAGS;
new_in_mode |= ENABLE_INSERT_MODE;
new_in_mode |= ENABLE_VIRTUAL_TERMINAL_INPUT;
SetConsoleMode(hInput, new_in_mode);
SetConsoleMode(hOutput, old_out_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
// disable automatic CRLF translation
old_stdin_trans_mode = _setmode(_fileno(stdin), _O_BINARY);
this->stdio_inited = true;
LOG_F(INFO, "Winterm: receiver initialized");
return 0;
}
void CharIoStdin::rcv_disable() {
if (!this->stdio_inited)
return;
// restore original console mode
SetConsoleMode(hInput, old_in_mode);
SetConsoleMode(hOutput, old_out_mode);
// restore original translation mode
_setmode(_fileno(stdin), old_stdin_trans_mode);
this->stdio_inited = false;
LOG_F(INFO, "Winterm: receiver disabled");
}
bool CharIoStdin::rcv_char_available() {
DWORD events;
INPUT_RECORD buffer;
PeekConsoleInput(hInput, &buffer, 1, &events);
return !!(events > 0);
}
int CharIoStdin::xmit_char(uint8_t c) {
_write(_fileno(stdout), &c, 1);
return 0;
}
int CharIoStdin::rcv_char(uint8_t* c) {
_read(_fileno(stdin), c, 1);
return 0;
}
#else // non-Windows OS (Linux, mac OS etc.)
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/select.h>
#include <unistd.h>
static struct sigaction old_act_sigint, new_act_sigint;
static struct sigaction old_act_sigterm, new_act_sigterm;
static struct termios orig_termios;
void CharIoStdin::mysig_handler(int signum)
{
// restore original terminal state
tcsetattr(STDIN_FILENO, TCSANOW, &orig_termios);
// restore original signal handler for SIGINT
signal(SIGINT, old_act_sigint.sa_handler);
signal(SIGTERM, old_act_sigterm.sa_handler);
LOG_F(INFO, "Old terminal state restored, SIG#=%d", signum);
// re-post signal
raise(signum);
}
int CharIoStdin::rcv_enable()
{
if (this->stdio_inited)
return 0;
// save original terminal state
tcgetattr(STDIN_FILENO, &orig_termios);
struct termios new_termios = orig_termios;
new_termios.c_cflag &= ~(CSIZE | PARENB);
new_termios.c_cflag |= CS8;
new_termios.c_lflag &= ~(ECHO | ICANON | ISIG);
new_termios.c_iflag &= ~(ICRNL);
tcsetattr(STDIN_FILENO, TCSANOW, &new_termios);
// save original signal handler for SIGINT
// then redirect SIGINT to new handler
memset(&new_act_sigint, 0, sizeof(new_act_sigint));
new_act_sigint.sa_handler = mysig_handler;
sigaction(SIGINT, &new_act_sigint, &old_act_sigint);
// save original signal handler for SIGTERM
// then redirect SIGTERM to new handler
memset(&new_act_sigterm, 0, sizeof(new_act_sigterm));
new_act_sigterm.sa_handler = mysig_handler;
sigaction(SIGTERM, &new_act_sigterm, &old_act_sigterm);
this->stdio_inited = true;
return 0;
}
void CharIoStdin::rcv_disable()
{
if (!this->stdio_inited)
return;
// restore original terminal state
tcsetattr(STDIN_FILENO, TCSANOW, &orig_termios);
// restore original signal handler for SIGINT
signal(SIGINT, old_act_sigint.sa_handler);
// restore original signal handler for SIGTERM
signal(SIGTERM, old_act_sigterm.sa_handler);
this->stdio_inited = false;
}
bool CharIoStdin::rcv_char_available()
{
static int consecutivechars = 0;
if (consecutivechars >= 15) {
consecutivechars++;
if (consecutivechars >= 400)
consecutivechars = 0;
return 0;
}
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(STDIN_FILENO, &readfds);
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
int sel_rv = select(1, &readfds, NULL, NULL, &timeout);
if (sel_rv > 0)
consecutivechars++;
else
consecutivechars = 0;
return sel_rv > 0;
}
int CharIoStdin::xmit_char(uint8_t c)
{
write(STDOUT_FILENO, &c, 1);
return 0;
}
int CharIoStdin::rcv_char(uint8_t *c)
{
read(STDIN_FILENO, c, 1);
return 0;
}
#endif
//======================== SOCKET character I/O backend ========================
#ifdef _WIN32
#else // non-Windows OS (Linux, mac OS etc.)
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/errno.h>
CharIoSocket::CharIoSocket()
{
int rc;
path = "dingussocket";
do {
rc = unlink(path);
if (rc == 0) {
LOG_F(INFO, "socket unlinked %s", path);
}
else if (errno != ENOENT) {
LOG_F(INFO, "socket unlink err: %s", strerror(errno));
break;
}
sockaddr_un address;
memset(&address, 0, sizeof(address));
address.sun_family = AF_UNIX;
strcpy(address.sun_path, path);
sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sockfd == -1) {
LOG_F(INFO, "socket create err: %s", strerror(errno));
break;
}
rc = bind(sockfd, (sockaddr*)(&address), sizeof(address));
if (rc == -1) {
LOG_F(INFO, "socket bind err: %s", strerror(errno));
close(sockfd);
sockfd = -1;
break;
}
rc = listen(sockfd, 100);
if (rc == -1) {
LOG_F(INFO, "socket listen err: %s", strerror(errno));
close(sockfd);
sockfd = -1;
break;
}
LOG_F(INFO, "socket listen %d", sockfd);
} while (0);
};
CharIoSocket::~CharIoSocket() {
unlink(path);
if (errno != ENOENT) {
LOG_F(INFO, "socket unlink err: %s", strerror(errno));
}
if (sockfd != -1) {
close(sockfd);
sockfd = -1;
}
}
int CharIoSocket::rcv_enable()
{
if (this->socket_inited)
return 0;
this->socket_inited = true;
return 0;
}
void CharIoSocket::rcv_disable()
{
if (!this->socket_inited)
return;
this->socket_inited = false;
}
bool CharIoSocket::rcv_char_available()
{
static int consecutivechars = 0;
static int count = 0;
if (consecutivechars >= 15) {
consecutivechars++;
if (consecutivechars >= 800)
consecutivechars = 0;
return 0;
}
int sel_rv = 0;
bool havechars = false;
fd_set readfds;
fd_set writefds;
fd_set errorfds;
int sockmax = 0;
if (sockfd != -1) {
FD_ZERO(&readfds);
FD_SET(sockfd, &readfds);
if (sockfd > sockmax) sockmax = sockfd;
if (acceptfd != -1) {
FD_SET(acceptfd, &readfds);
if (acceptfd > sockmax) sockmax = acceptfd;
}
writefds = readfds;
errorfds = readfds;
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
sel_rv = select(sockmax + 1, &readfds, &writefds, &errorfds, &timeout);
if (sel_rv == -1) {
LOG_F(INFO, "socket select err: %s", strerror(errno));
}
}
if (sel_rv > 0) {
if (sockfd != -1) {
if (FD_ISSET(sockfd, &readfds)) {
uint8_t c;
int received = recv(sockfd, &c, 1, 0);
if (received == -1) {
if (acceptfd == -1) {
//LOG_F(INFO, "socket sock read (not accepted yet) err: %s", strerror(errno)); // this happens once before accept
}
else {
LOG_F(INFO, "socket sock read err: %s", strerror(errno)); // should never happen
}
}
else if (received == 1) {
LOG_F(INFO, "socket sock read '%c'", c); // should never happen
}
else {
LOG_F(INFO, "socket sock read %d", received); // should never happen
}
if (acceptfd == -1) {
sockaddr_un acceptfdaddr;
memset(&acceptfdaddr, 0, sizeof(acceptfdaddr));
socklen_t len = sizeof(acceptfdaddr);
acceptfd = accept(sockfd, (struct sockaddr *) &acceptfdaddr, &len);
if (acceptfd == -1){
LOG_F(INFO, "socket accept err: %s", strerror(errno));
}
else {
LOG_F(INFO, "socket accept %d", acceptfd);
}
}
} // if read
if (FD_ISSET(sockfd, &writefds)) {
LOG_F(INFO, "socket sock write");
}
if (FD_ISSET(sockfd, &errorfds)) {
LOG_F(INFO, "socket sock error");
}
} // if sockfd
if (acceptfd != -1) {
if (FD_ISSET(acceptfd, &readfds)) {
// LOG_F(INFO, "socket accept read havechars");
havechars = true;
consecutivechars++;
} // if read
if (FD_ISSET(acceptfd, &writefds)) {
// LOG_F(INFO, "socket accept write"); // this is usually always true
}
if (FD_ISSET(acceptfd, &errorfds)) {
LOG_F(INFO, "socket accept error");
}
} // if acceptfd
}
else
consecutivechars = 0;
return havechars;
}
int CharIoSocket::xmit_char(uint8_t c)
{
if (acceptfd == -1)
CharIoSocket::rcv_char_available();
if (acceptfd != -1) {
int sent = send(acceptfd, &c, 1, 0);
if (sent == -1) {
LOG_F(INFO, "socket accept write err: %s", strerror(errno));
}
if (sent == 1) {
// LOG_F(INFO, "socket accept write '%c'", c);
}
else {
LOG_F(INFO, "socket accept write %d", sent);
}
}
return 0;
}
int CharIoSocket::rcv_char(uint8_t *c)
{
if (acceptfd == -1)
CharIoSocket::rcv_char_available();
if (acceptfd != -1) {
int received = recv(acceptfd, c, 1, 0);
if (received == -1) {
LOG_F(INFO, "socket accept read err: %s", strerror(errno));
}
else if (received == 1) {
// LOG_F(INFO, "socket accept read '%c'", c ? *c : 0);
}
else {
LOG_F(INFO, "socket accept read %d", received);
}
}
return 0;
}
#endif
+101
View File
@@ -0,0 +1,101 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** Character I/O definitions. */
#ifndef CHAR_IO_H
#define CHAR_IO_H
#include <cinttypes>
#ifdef _WIN32
#else
#include <termios.h>
#include <signal.h>
#endif
enum {
CHARIO_BE_NULL = 0, // NULL backend: swallows everything, receives nothing
CHARIO_BE_STDIO = 1, // STDIO backend: uses STDIN for input and STDOUT for output
CHARIO_BE_SOCKET = 2, // socket backend: uses a socket for input and output
};
/** Interface for character I/O backends. */
class CharIoBackEnd {
public:
CharIoBackEnd() = default;
virtual ~CharIoBackEnd() = default;
virtual int rcv_enable() { return 0; };
virtual void rcv_disable() {};
virtual bool rcv_char_available() = 0;
virtual int xmit_char(uint8_t c) = 0;
virtual int rcv_char(uint8_t *c) = 0;
};
/** Null character I/O backend. */
class CharIoNull : public CharIoBackEnd {
public:
CharIoNull() = default;
~CharIoNull() = default;
bool rcv_char_available();
int xmit_char(uint8_t c);
int rcv_char(uint8_t *c);
};
/** Stdin character I/O backend. */
class CharIoStdin : public CharIoBackEnd {
public:
CharIoStdin() { this->stdio_inited = false; };
~CharIoStdin() = default;
int rcv_enable();
void rcv_disable();
bool rcv_char_available();
int xmit_char(uint8_t c);
int rcv_char(uint8_t *c);
private:
static void mysig_handler(int signum);
bool stdio_inited;
};
/** Socket character I/O backend. */
class CharIoSocket : public CharIoBackEnd {
public:
CharIoSocket();
~CharIoSocket();
int rcv_enable();
void rcv_disable();
bool rcv_char_available();
int xmit_char(uint8_t c);
int rcv_char(uint8_t *c);
private:
bool socket_inited = false;
int sockfd = -1;
int acceptfd = -1;
const char* path = 0;
};
#endif // CHAR_IO_H
+334
View File
@@ -0,0 +1,334 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** @file Enhanced Serial Communications Controller (ESCC) emulation. */
#include <devices/deviceregistry.h>
#include <devices/serial/chario.h>
#include <devices/serial/escc.h>
#include <loguru.hpp>
#include <machines/machineproperties.h>
#include <cinttypes>
#include <memory>
#include <string>
#include <vector>
/** Remap the compatible addressing scheme to MacRISC one. */
const uint8_t compat_to_macrisc[6] = {
EsccReg::Port_B_Cmd, EsccReg::Port_A_Cmd,
EsccReg::Port_B_Data, EsccReg::Port_A_Data,
EsccReg::Enh_Reg_B, EsccReg::Enh_Reg_A
};
EsccController::EsccController()
{
// allocate channels
this->ch_a = std::unique_ptr<EsccChannel> (new EsccChannel("A"));
this->ch_b = std::unique_ptr<EsccChannel> (new EsccChannel("B"));
// attach backends
std::string backend_name = GET_STR_PROP("serial_backend");
this->ch_a->attach_backend(
(backend_name == "stdio") ? CHARIO_BE_STDIO :
#ifdef _WIN32
#else
(backend_name == "socket") ? CHARIO_BE_SOCKET :
#endif
CHARIO_BE_NULL
);
this->ch_b->attach_backend(CHARIO_BE_NULL);
this->reg_ptr = 0;
}
void EsccController::reset()
{
this->master_int_cntrl &= 0xFC;
this->master_int_cntrl |= 0xC0;
this->ch_a->reset(true);
this->ch_b->reset(true);
}
uint8_t EsccController::read(uint8_t reg_offset)
{
uint8_t result = 0;
switch(reg_offset) {
case EsccReg::Port_B_Cmd:
LOG_F(9, "ESCC: reading Port B register RR%d", this->reg_ptr);
if (this->reg_ptr == 2) {
// TODO: implement interrupt vector modifications
result = this->int_vec;
} else {
result = this->ch_b->read_reg(this->reg_ptr);
}
this->reg_ptr = 0;
break;
case EsccReg::Port_A_Cmd:
LOG_F(9, "ESCC: reading Port A register RR%d", this->reg_ptr);
if (this->reg_ptr == 2) {
return this->int_vec;
} else {
return this->ch_a->read_reg(this->reg_ptr);
}
this->reg_ptr = 0;
break;
case EsccReg::Port_B_Data:
return this->ch_b->receive_byte();
case EsccReg::Port_A_Data:
return this->ch_a->receive_byte();
default:
LOG_F(9, "ESCC: reading from unimplemented register 0x%x", reg_offset);
}
return result;
}
void EsccController::write(uint8_t reg_offset, uint8_t value)
{
switch(reg_offset) {
case EsccReg::Port_B_Cmd:
this->write_internal(this->ch_b.get(), value);
break;
case EsccReg::Port_A_Cmd:
this->write_internal(this->ch_a.get(), value);
break;
case EsccReg::Port_B_Data:
this->ch_b->send_byte(value);
break;
case EsccReg::Port_A_Data:
this->ch_a->send_byte(value);
break;
default:
LOG_F(9, "ESCC: writing 0x%X to unimplemented register 0x%x", value, reg_offset);
}
}
void EsccController::write_internal(EsccChannel *ch, uint8_t value)
{
if (this->reg_ptr) {
// chip-specific registers
if (this->reg_ptr == 9) {
// see if some reset is requested
switch(value & 0xC0) {
case RESET_CH_B:
this->master_int_cntrl &= 0xDF;
this->ch_b->reset(false);
break;
case RESET_CH_A:
this->master_int_cntrl &= 0xDF;
this->ch_a->reset(false);
break;
case RESET_ESCC:
this->reset();
break;
}
this->master_int_cntrl = value & 0x3F;
} else if (this->reg_ptr == 2) {
this->int_vec = value;
} else { // channel-specific registers
ch->write_reg(this->reg_ptr, value);
}
this->reg_ptr = 0;
} else {
this->reg_ptr = value & 7;
switch(value >> 3) {
case WR0Cmd::Point_High:
this->reg_ptr |= 8;
break;
}
}
}
// ======================== ESCC Channel methods ==============================
void EsccChannel::attach_backend(int id)
{
switch(id) {
case CHARIO_BE_NULL:
this->chario = std::unique_ptr<CharIoBackEnd> (new CharIoNull);
break;
case CHARIO_BE_STDIO:
this->chario = std::unique_ptr<CharIoBackEnd> (new CharIoStdin);
break;
#ifdef _WIN32
#else
case CHARIO_BE_SOCKET:
this->chario = std::unique_ptr<CharIoBackEnd> (new CharIoSocket);
break;
#endif
default:
LOG_F(ERROR, "ESCC: unknown backend ID %d, using NULL instead", id);
this->chario = std::unique_ptr<CharIoBackEnd> (new CharIoNull);
}
}
void EsccChannel::reset(bool hw_reset)
{
this->chario->rcv_disable();
this->write_regs[1] &= 0x24;
this->write_regs[3] &= 0xFE;
this->write_regs[4] |= 0x04;
this->write_regs[5] &= 0x61;
this->write_regs[15] = 0xF8;
this->read_regs[0] &= 0x3C;
this->read_regs[0] |= 0x44;
this->read_regs[1] = 0x06;
this->read_regs[3] = 0x00;
this->read_regs[10] = 0x00;
// initialize DPLL
this->dpll_active = 0;
this->dpll_mode = DpllMode::NRZI;
this->dpll_clock_src = 0;
// initialize Baud Rate Generator (BRG)
this->brg_active = 0;
this->brg_clock_src = 0;
if (hw_reset) {
this->write_regs[10] = 0;
this->write_regs[11] = 8;
this->write_regs[14] &= 0xC0;
this->write_regs[14] |= 0x20;
} else {
this->write_regs[10] &= 0x60;
this->write_regs[14] &= 0xC3;
this->write_regs[14] |= 0x20;
}
}
void EsccChannel::write_reg(int reg_num, uint8_t value)
{
switch (reg_num) {
case 3:
if ((this->write_regs[3] ^ value) & 0x10) {
this->write_regs[3] |= 0x10;
this->read_regs[0] |= 0x10; // set SYNC_HUNT flag
LOG_F(9, "ESCC: Hunt mode entered.");
}
if ((this->write_regs[3] ^ value) & 1) {
if (value & 1) {
this->write_regs[3] |= 0x1;
this->chario->rcv_enable();
LOG_F(9, "ESCC: receiver enabled.");
} else {
this->write_regs[3] ^= 0x1;
this->chario->rcv_disable();
LOG_F(9, "ESCC: receiver disabled.");
this->write_regs[3] |= 0x10; // enter HUNT mode
this->read_regs[0] |= 0x10; // set SYNC_HUNT flag
}
}
this->write_regs[3] = (this->write_regs[3] & 0x11) | (value & 0xEE);
return;
case 7:
if (this->write_regs[15] & 1) {
this->wr7_enh = value;
return;
}
break;
case 14:
switch (value >> 5) {
case DPLL_NULL_CMD:
break;
case DPLL_ENTER_SRC_MODE:
this->dpll_active = 1;
this->read_regs[10] &= 0x3F;
break;
case DPLL_DISABLE:
this->dpll_active = 0;
// fallthrough
case DPLL_RST_MISSING_CLK:
this->read_regs[10] &= 0x3F;
break;
case DPLL_SET_SRC_BGR:
this->dpll_clock_src = 0;
break;
case DPLL_SET_SRC_RTXC:
this->dpll_clock_src = 1;
break;
case DPLL_SET_FM_MODE:
this->dpll_mode = DpllMode::FM;
break;
case DPLL_SET_NRZI_MODE:
this->dpll_mode = DpllMode::NRZI;
break;
default:
LOG_F(WARNING, "ESCC: unimplemented DPLL command %d", value >> 5);
}
if (value & 0x1C) { // Local Loopback, Auto Echo DTR/REQ bits set
LOG_F(WARNING, "ESCC: unexpected value in WR14 = 0x%X", value);
}
if (this->brg_active ^ (value & 1)) {
this->brg_active = value & 1;
LOG_F(9, "ESCC: BRG %s", this->brg_active ? "enabled" : "disabled");
}
return;
}
this->write_regs[reg_num] = value;
}
uint8_t EsccChannel::read_reg(int reg_num)
{
if (!reg_num) {
if (this->chario->rcv_char_available()) {
this->read_regs[0] |= 1;
}
}
return this->read_regs[reg_num];
}
void EsccChannel::send_byte(uint8_t value)
{
// TODO: put one byte into the Data FIFO
this->chario->xmit_char(value);
}
uint8_t EsccChannel::receive_byte()
{
// TODO: remove one byte from the Receive FIFO
uint8_t c;
this->chario->rcv_char(&c);
this->read_regs[0] &= ~1;
return c;
}
static const vector<string> CharIoBackends = {"null", "stdio", "socket"};
static const PropMap Escc_Properties = {
{"serial_backend", new StrProperty("null", CharIoBackends)},
};
static const DeviceDescription Escc_Descriptor = {
EsccController::create, {}, Escc_Properties
};
REGISTER_DEVICE(Escc, Escc_Descriptor);
+146
View File
@@ -0,0 +1,146 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** @file Enhanced Serial Communications Controller (ESCC) definitions. */
#ifndef ESCC_H
#define ESCC_H
#include <devices/common/hwcomponent.h>
#include <devices/serial/chario.h>
#include <cinttypes>
#include <memory>
#include <string>
/** ESCC register positions */
/* Please note that the registers below are provided
by the Apple I/O controllers for accessing ESCC
in a more convenient way. Actual physical addresses
are controller dependent.
The registers are ordered according with the MacRISC
scheme used in the PCI Power Macintosh models.
Pre-PCI Macs uses the so-called compatibility
addressing. Please use compat_to_macrisc table
below for converting from compat to MacRISC.
*/
enum EsccReg : uint8_t {
Port_B_Cmd = 0,
Port_B_Data = 1, // direct access to WR8/RR8
Port_A_Cmd = 2,
Port_A_Data = 3, // direct access to WR8/RR8
Enh_Reg_B = 4, // undocumented Apple extension
Enh_Reg_A = 5, // undocumented Apple extension
};
extern const uint8_t compat_to_macrisc[6];
/** LocalTalk LTPC registers provided by a MacIO controller. */
enum LocalTalkReg : uint8_t {
Rec_Count = 8,
Start_A = 9,
Start_B = 0xA,
Detect_AB = 0xB,
};
enum WR0Cmd : uint8_t {
Point_High = 1,
};
/** ESCC reset commands. */
enum {
RESET_ESCC = 0xC0,
RESET_CH_A = 0x80,
RESET_CH_B = 0x40
};
/** DPLL commands in WR14. */
enum {
DPLL_NULL_CMD = 0,
DPLL_ENTER_SRC_MODE = 1,
DPLL_RST_MISSING_CLK = 2,
DPLL_DISABLE = 3,
DPLL_SET_SRC_BGR = 4,
DPLL_SET_SRC_RTXC = 5,
DPLL_SET_FM_MODE = 6,
DPLL_SET_NRZI_MODE = 7
};
enum DpllMode : uint8_t {
NRZI = 0,
FM = 1
};
/** ESCC Channel class. */
class EsccChannel {
public:
EsccChannel(std::string name) { this->name = name; };
~EsccChannel() = default;
void attach_backend(int id);
void reset(bool hw_reset);
uint8_t read_reg(int reg_num);
void write_reg(int reg_num, uint8_t value);
void send_byte(uint8_t value);
uint8_t receive_byte();
private:
std::string name;
uint8_t read_regs[16];
uint8_t write_regs[16];
uint8_t wr7_enh;
uint8_t dpll_active;
uint8_t dpll_mode;
uint8_t dpll_clock_src;
uint8_t brg_active;
uint8_t brg_clock_src;
std::unique_ptr<CharIoBackEnd> chario;
};
/** ESCC Controller class. */
class EsccController : public HWComponent {
public:
EsccController();
~EsccController() = default;
static std::unique_ptr<HWComponent> create() {
return std::unique_ptr<EsccController>(new EsccController());
}
// ESCC registers access
uint8_t read(uint8_t reg_offset);
void write(uint8_t reg_offset, uint8_t value);
private:
void reset();
void write_internal(EsccChannel* ch, uint8_t value);
std::unique_ptr<EsccChannel> ch_a;
std::unique_ptr<EsccChannel> ch_b;
int reg_ptr; // register pointer for reading/writing (same for both channels)
uint8_t master_int_cntrl;
uint8_t int_vec;
};
#endif // ESCC_H
+7 -14
View File
@@ -28,11 +28,12 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
Author: Max Poliakovski 2019-21
*/
#include "awacs.h"
#include "dbdma.h"
#include "endianswap.h"
#include "machines/machinebase.h"
#include "soundserver.h"
#include <devices/common/dbdma.h>
#include <devices/sound/awacs.h>
#include <devices/sound/soundserver.h>
#include <endianswap.h>
#include <machines/machinebase.h>
#include <algorithm>
#include <loguru.hpp>
#include <memory>
@@ -45,14 +46,6 @@ AwacsBase::AwacsBase()
this->out_stream_ready = false;
}
AwacsBase::~AwacsBase()
{
// disconnect from SoundServer
if (this->out_stream_ready) {
snd_server->close_out_stream();
}
}
void AwacsBase::set_sample_rate(int sr_id)
{
if (sr_id > this->max_sr_id) {
@@ -163,7 +156,7 @@ void AwacsScreamer::snd_ctrl_write(uint32_t offset, uint32_t value, int size)
subframe = (value >> 14) & 3;
reg_num = (value >> 20) & 7;
data = ((value >> 8) & 0xF00) | ((value >> 24) & 0xFF);
LOG_F(9, "Screamer subframe = %d, reg = %d, data = %08X\n",
LOG_F(9, "Screamer subframe = %d, reg = %d, data = %08X",
subframe, reg_num, data);
if (!subframe)
this->control_regs[reg_num] = data;
+9 -8
View File
@@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
Copyright (C) 2018-21 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@@ -21,17 +21,18 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
/** AWAC sound devices family definitions.
Audio Waveform Aplifier and Converters (AWACs) is a family of custom audio
Audio Waveform Amplifier and Converters (AWACs) is a family of custom audio
chips used in Power Macintosh.
*/
#ifndef AWAC_H
#define AWAC_H
#include "dmacore.h"
#include "dbdma.h"
#include "i2c.h"
#include "soundserver.h"
#include <devices/common/dbdma.h>
#include <devices/common/dmacore.h>
#include <devices/common/i2c/i2c.h>
#include <devices/sound/soundserver.h>
#include <cinttypes>
#include <memory>
@@ -39,7 +40,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
class AwacsBase {
public:
AwacsBase();
~AwacsBase();
~AwacsBase() = default;
void set_dma_out(DmaOutChannel *dma_out_ch) {
this->dma_out_ch = dma_out_ch;
@@ -148,7 +149,7 @@ private:
class AwacsScreamer : public AwacsBase {
public:
AwacsScreamer();
~AwacsScreamer();
~AwacsScreamer() = default;
uint32_t snd_ctrl_read(uint32_t offset, int size);
void snd_ctrl_write(uint32_t offset, uint32_t value, int size);
@@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@@ -19,9 +19,10 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "dmacore.h"
#include "endianswap.h"
#include "soundserver.h"
#include <devices/common/dmacore.h>
#include <devices/sound/soundserver.h>
#include <endianswap.h>
#include <loguru.hpp>
#include <cubeb/cubeb.h>
#ifdef _WIN32
@@ -113,8 +114,10 @@ int SoundServer::start()
void SoundServer::shutdown()
{
switch (this->status) {
case SND_STREAM_OPENED:
close_out_stream();
/* fall through */
case SND_SERVER_UP:
//soundio_device_unref(this->out_device);
/* fall through */
case SND_API_READY:
cubeb_destroy(this->cubeb_ctx);
@@ -202,6 +205,8 @@ int SoundServer::open_out_stream(uint32_t sample_rate, void *user_data)
LOG_F(INFO, "Sound output stream opened.");
this->status = SND_STREAM_OPENED;
return 0;
}
@@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
Copyright (C) 2018-21 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@@ -34,19 +34,23 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#ifndef SOUND_SERVER_H
#define SOUND_SERVER_H
#include "hwcomponent.h"
#include <cubeb/cubeb.h>
#include <devices/common/hwcomponent.h>
enum {
SND_SERVER_DOWN = 0,
SND_API_READY,
SND_SERVER_UP,
SND_STREAM_OPENED
};
class SoundServer : public HWComponent {
public:
SoundServer() { this->start(); };
SoundServer() {
supports_types(HWCompType::SND_SERVER);
this->start();
};
~SoundServer() { this->shutdown(); };
int start();
@@ -55,10 +59,6 @@ public:
int start_out_stream();
void close_out_stream();
bool supports_type(HWCompType type) {
return type == HWCompType::SND_SERVER;
};
private:
int status; /* server status */
cubeb *cubeb_ctx;
-434
View File
@@ -1,434 +0,0 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** VIA-CUDA combo device emulation.
Author: Max Poliakovski 2019
*/
#include "viacuda.h"
#include "adb.h"
#include <cinttypes>
#include <loguru.hpp>
using namespace std;
ViaCuda::ViaCuda() {
this->name = "ViaCuda";
/* FIXME: is this the correct
VIA initialization? */
this->via_regs[VIA_A] = 0x80;
this->via_regs[VIA_DIRB] = 0xFF;
this->via_regs[VIA_DIRA] = 0xFF;
this->via_regs[VIA_T1LL] = 0xFF;
this->via_regs[VIA_T1LH] = 0xFF;
this->via_regs[VIA_IER] = 0x7F;
// PRAM Pre-Initialization
this->pram_obj = new NVram("pram.bin", 256);
this->adb_obj = new ADB_Bus();
this->init();
}
ViaCuda::~ViaCuda() {
if (this->pram_obj)
delete (this->pram_obj);
}
void ViaCuda::init() {
this->old_tip = 0;
this->old_byteack = 0;
this->treq = 1;
this->in_count = 0;
this->out_count = 0;
this->poll_rate = 11;
}
uint8_t ViaCuda::read(int reg) {
uint8_t res;
LOG_F(9, "Read VIA reg %x \n", (uint32_t)reg);
res = this->via_regs[reg & 0xF];
/* reading from some VIA registers triggers special actions */
switch (reg & 0xF) {
case VIA_B:
res = this->via_regs[VIA_B];
break;
case VIA_A:
case VIA_ANH:
LOG_F(WARNING, "Attempted read from VIA Port A! \n");
break;
case VIA_IER:
res |= 0x80; /* bit 7 always reads as "1" */
}
return res;
}
void ViaCuda::write(int reg, uint8_t value) {
switch (reg & 0xF) {
case VIA_B:
this->via_regs[VIA_B] = value;
write(value);
break;
case VIA_A:
case VIA_ANH:
LOG_F(WARNING, "Attempted write to VIA Port A! \n");
break;
case VIA_DIRB:
LOG_F(9, "VIA_DIRB = %x \n", (uint32_t)value);
this->via_regs[VIA_DIRB] = value;
break;
case VIA_DIRA:
LOG_F(9, "VIA_DIRA = %x \n", (uint32_t)value);
this->via_regs[VIA_DIRA] = value;
break;
case VIA_PCR:
LOG_F(9, "VIA_PCR = %x \n", (uint32_t)value);
this->via_regs[VIA_PCR] = value;
break;
case VIA_ACR:
LOG_F(9, "VIA_ACR = %x \n", (uint32_t)value);
this->via_regs[VIA_ACR] = value;
break;
case VIA_IER:
this->via_regs[VIA_IER] = (value & 0x80) ? value & 0x7F : this->via_regs[VIA_IER] & ~value;
LOG_F(INFO, "VIA_IER updated to %d \n", (uint32_t)this->via_regs[VIA_IER]);
print_enabled_ints();
break;
default:
this->via_regs[reg & 0xF] = value;
}
}
void ViaCuda::print_enabled_ints() {
const char* via_int_src[] = {"CA2", "CA1", "SR", "CB2", "CB1", "T2", "T1"};
for (int i = 0; i < 7; i++) {
if (this->via_regs[VIA_IER] & (1 << i))
LOG_F(INFO, "VIA %s interrupt enabled \n", via_int_src[i]);
}
}
inline bool ViaCuda::ready() {
return ((this->via_regs[VIA_DIRB] & 0x38) == 0x30);
}
inline void ViaCuda::assert_sr_int() {
this->via_regs[VIA_IFR] |= 0x84;
}
void ViaCuda::write(uint8_t new_state) {
if (!ready()) {
LOG_F(WARNING, "Cuda not ready! \n");
return;
}
int new_tip = !!(new_state & CUDA_TIP);
int new_byteack = !!(new_state & CUDA_BYTEACK);
/* return if there is no state change */
if (new_tip == this->old_tip && new_byteack == this->old_byteack)
return;
LOG_F(9, "Cuda state changed! \n");
this->old_tip = new_tip;
this->old_byteack = new_byteack;
if (new_tip) {
if (new_byteack) {
this->via_regs[VIA_B] |= CUDA_TREQ; /* negate TREQ */
this->treq = 1;
if (this->in_count) {
process_packet();
/* start response transaction */
this->via_regs[VIA_B] &= ~CUDA_TREQ; /* assert TREQ */
this->treq = 0;
}
this->in_count = 0;
} else {
LOG_F(9, "Cuda: enter sync state \n");
this->via_regs[VIA_B] &= ~CUDA_TREQ; /* assert TREQ */
this->treq = 0;
this->in_count = 0;
this->out_count = 0;
}
assert_sr_int(); /* send dummy byte as idle acknowledge or attention */
} else {
if (this->via_regs[VIA_ACR] & 0x10) { /* data transfer: Host --> Cuda */
if (this->in_count < 16) {
this->in_buf[this->in_count++] = this->via_regs[VIA_SR];
assert_sr_int(); /* tell the system we've read the data */
} else {
LOG_F(WARNING, "Cuda input buffer exhausted! \n");
}
} else { /* data transfer: Cuda --> Host */
(this->*out_handler)();
assert_sr_int(); /* tell the system we've written the data */
}
}
}
/* sends zeros to host at infinitum */
void ViaCuda::null_out_handler() {
this->via_regs[VIA_SR] = 0;
}
/* sends data from out_buf until exhausted, then switches to next_out_handler */
void ViaCuda::out_buf_handler() {
if (this->out_pos < this->out_count) {
LOG_F(9, "OutBufHandler: sending next byte 0x%X", this->out_buf[this->out_pos]);
this->via_regs[VIA_SR] = this->out_buf[this->out_pos++];
} else if (this->is_open_ended) {
LOG_F(9, "OutBufHandler: switching to next handler");
this->out_handler = this->next_out_handler;
this->next_out_handler = &ViaCuda::null_out_handler;
(this->*out_handler)();
} else {
LOG_F(9, "Sending last byte");
this->out_count = 0;
this->via_regs[VIA_B] |= CUDA_TREQ; /* negate TREQ */
this->treq = 1;
}
}
void ViaCuda::response_header(uint32_t pkt_type, uint32_t pkt_flag) {
this->out_buf[0] = pkt_type;
this->out_buf[1] = pkt_flag;
this->out_buf[2] = this->in_buf[1]; /* copy original cmd */
this->out_count = 3;
this->out_pos = 0;
this->out_handler = &ViaCuda::out_buf_handler;
this->next_out_handler = &ViaCuda::null_out_handler;
this->is_open_ended = false;
}
void ViaCuda::error_response(uint32_t error) {
this->out_buf[0] = CUDA_PKT_ERROR;
this->out_buf[1] = error;
this->out_buf[2] = this->in_buf[0];
this->out_buf[3] = this->in_buf[1]; /* copy original cmd */
this->out_count = 4;
this->out_pos = 0;
this->out_handler = &ViaCuda::out_buf_handler;
this->next_out_handler = &ViaCuda::null_out_handler;
this->is_open_ended = false;
}
void ViaCuda::process_packet() {
if (this->in_count < 2) {
LOG_F(ERROR, "Cuda: invalid packet (too few data)!\n");
error_response(CUDA_ERR_BAD_SIZE);
return;
}
switch (this->in_buf[0]) {
case CUDA_PKT_ADB:
LOG_F(9, "Cuda: ADB packet received \n");
process_adb_command(this->in_buf[1], this->in_count - 2);
break;
case CUDA_PKT_PSEUDO:
LOG_F(9, "Cuda: pseudo command packet received \n");
LOG_F(9, "Command: %x \n", (uint32_t)(this->in_buf[1]));
LOG_F(9, "Data count: %d \n ", this->in_count);
for (int i = 0; i < this->in_count; i++) {
LOG_F(9, "%x ,", (uint32_t)(this->in_buf[i]));
}
pseudo_command(this->in_buf[1], this->in_count - 2);
break;
default:
LOG_F(ERROR, "Cuda: unsupported packet type = %d \n", (uint32_t)(this->in_buf[0]));
error_response(CUDA_ERR_BAD_PKT);
}
}
void ViaCuda::process_adb_command(uint8_t cmd_byte, int data_count) {
int adb_dev = cmd_byte >> 4; // 2 for keyboard, 3 for mouse
int cmd = cmd_byte & 0xF;
if (!cmd) {
LOG_F(9, "Cuda: ADB SendReset command requested\n");
response_header(CUDA_PKT_ADB, 0);
} else if (cmd == 1) {
LOG_F(9, "Cuda: ADB Flush command requested\n");
response_header(CUDA_PKT_ADB, 0);
} else if ((cmd & 0xC) == 8) {
LOG_F(9, "Cuda: ADB Listen command requested\n");
int adb_reg = cmd_byte & 0x3;
if (adb_obj->listen(adb_dev, adb_reg)) {
response_header(CUDA_PKT_ADB, 0);
for (int data_ptr = 0; data_ptr < adb_obj->get_output_len(); data_ptr++) {
this->in_buf[(2 + data_ptr)] = adb_obj->get_output_byte(data_ptr);
}
} else {
response_header(CUDA_PKT_ADB, 2);
}
} else if ((cmd & 0xC) == 0xC) {
LOG_F(9, "Cuda: ADB Talk command requested\n");
response_header(CUDA_PKT_ADB, 0);
int adb_reg = cmd_byte & 0x3;
if (adb_obj->talk(adb_dev, adb_reg, this->in_buf[2])) {
response_header(CUDA_PKT_ADB, 0);
} else {
response_header(CUDA_PKT_ADB, 2);
}
} else {
LOG_F(ERROR, "Cuda: unsupported ADB command 0x%x \n", cmd);
error_response(CUDA_ERR_BAD_CMD);
}
}
void ViaCuda::pseudo_command(int cmd, int data_count) {
switch (cmd) {
case CUDA_START_STOP_AUTOPOLL:
if (this->in_buf[2]) {
LOG_F(INFO, "Cuda: autopoll started, rate: %dms", this->poll_rate);
} else {
LOG_F(INFO, "Cuda: autopoll stopped");
}
response_header(CUDA_PKT_PSEUDO, 0);
break;
case CUDA_READ_PRAM:
response_header(CUDA_PKT_PSEUDO, 0);
this->pram_obj->read_byte(this->in_buf[2]);
break;
case CUDA_WRITE_PRAM:
response_header(CUDA_PKT_PSEUDO, 0);
this->pram_obj->write_byte(this->in_buf[2], this->in_buf[3]);
break;
case CUDA_SET_AUTOPOLL_RATE:
this->poll_rate = this->in_buf[2];
LOG_F(INFO, "Cuda: autopoll rate set to: %d", this->poll_rate);
response_header(CUDA_PKT_PSEUDO, 0);
break;
case CUDA_GET_AUTOPOLL_RATE:
response_header(CUDA_PKT_PSEUDO, 0);
this->out_buf[3] = this->poll_rate;
this->out_count++;
break;
case CUDA_READ_WRITE_I2C:
response_header(CUDA_PKT_PSEUDO, 0);
i2c_simple_transaction(this->in_buf[2], &this->in_buf[3], this->in_count - 3);
break;
case CUDA_COMB_FMT_I2C:
response_header(CUDA_PKT_PSEUDO, 0);
if (this->in_count >= 5) {
i2c_comb_transaction(
this->in_buf[2], this->in_buf[3], this->in_buf[4], &this->in_buf[5], this->in_count - 5);
}
break;
case CUDA_OUT_PB0: /* undocumented call! */
LOG_F(INFO, "Cuda: send %d to PB0 \n", (int)(this->in_buf[2]));
response_header(CUDA_PKT_PSEUDO, 0);
break;
default:
LOG_F(ERROR, "Cuda: unsupported pseudo command 0x%x \n", cmd);
error_response(CUDA_ERR_BAD_CMD);
}
}
/* sends data from the current I2C to host ad infinitum */
void ViaCuda::i2c_handler() {
this->receive_byte(this->curr_i2c_addr, &this->via_regs[VIA_SR]);
}
void ViaCuda::i2c_simple_transaction(uint8_t dev_addr, const uint8_t* in_buf, int in_bytes) {
int op_type = dev_addr & 1; /* 0 - write to device, 1 - read from device */
dev_addr >>= 1; /* strip RD/WR bit */
if (!this->start_transaction(dev_addr)) {
LOG_F(WARNING, "Unsupported I2C device 0x%X \n", (int)(dev_addr));
error_response(CUDA_ERR_I2C);
return;
}
/* send data to the target I2C device until there is no more data to send
or the target device doesn't acknowledge that indicates an error */
for (int i = 0; i < in_bytes; i++) {
if (!this->send_byte(dev_addr, in_buf[i])) {
LOG_F(WARNING, "NO_ACK during sending, device 0x%X \n", (int)(dev_addr));
error_response(CUDA_ERR_I2C);
return;
}
}
if (op_type) { /* read request initiate an open ended transaction */
this->curr_i2c_addr = dev_addr;
this->out_handler = &ViaCuda::out_buf_handler;
this->next_out_handler = &ViaCuda::i2c_handler;
this->is_open_ended = true;
}
}
void ViaCuda::i2c_comb_transaction(
uint8_t dev_addr, uint8_t sub_addr, uint8_t dev_addr1, const uint8_t* in_buf, int in_bytes) {
int op_type = dev_addr1 & 1; /* 0 - write to device, 1 - read from device */
if ((dev_addr & 0xFE) != (dev_addr1 & 0xFE)) {
LOG_F(ERROR, "Combined I2C: dev_addr mismatch!\n");
error_response(CUDA_ERR_I2C);
return;
}
dev_addr >>= 1; /* strip RD/WR bit */
if (!this->start_transaction(dev_addr)) {
LOG_F(WARNING, "Unsupported I2C device 0x%X \n", (int)(dev_addr));
error_response(CUDA_ERR_I2C);
return;
}
if (!this->send_subaddress(dev_addr, sub_addr)) {
LOG_F(WARNING, "NO_ACK while sending subaddress, device 0x%X \n", (int)(dev_addr));
error_response(CUDA_ERR_I2C);
return;
}
/* send data to the target I2C device until there is no more data to send
or the target device doesn't acknowledge that indicates an error */
for (int i = 0; i < in_bytes; i++) {
if (!this->send_byte(dev_addr, in_buf[i])) {
LOG_F(WARNING, "NO_ACK during sending, device 0x%X \n", (int)(dev_addr));
error_response(CUDA_ERR_I2C);
return;
}
}
if (!op_type) { /* return dummy response for writes */
LOG_F(WARNING, "Combined I2C - write request!");
} else {
this->curr_i2c_addr = dev_addr;
this->out_handler = &ViaCuda::out_buf_handler;
this->next_out_handler = &ViaCuda::i2c_handler;
this->is_open_ended = true;
}
}
@@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@@ -19,21 +19,17 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef ATI_RAGE_H
#define ATI_RAGE_H
#ifndef ATI_MACH64_DEFS_H
#define ATI_MACH64_DEFS_H
#include "displayid.h"
#include "pcidevice.h"
#include <cinttypes>
/* PCI related definitions. */
/* ATI PCI device IDs. */
enum {
ATI_PCI_VENDOR_ID = 0x1002,
ATI_RAGE_PRO_DEV_ID = 0x4750,
ATI_RAGE_GT_DEV_ID = 0x4754,
ATI_RAGE_PRO_DEV_ID = 0x4750,
ATI_RAGE_GT_DEV_ID = 0x4754,
ATI_MACH64_GX_DEV_ID = 0x4758,
};
/** Mach registers offsets. */
/** Mach64 register offsets. */
enum {
ATI_CRTC_H_TOTAL_DISP = 0x0000,
ATI_CRTC_H_SYNC_STRT_WID = 0x0004,
@@ -89,6 +85,7 @@ enum {
ATI_CONFIG_CNTL = 0x00DC,
ATI_CONFIG_CHIP_ID = 0x00E0,
ATI_CONFIG_STAT0 = 0x00E4,
ATI_GX_CONFIG_STAT1 = 0x00E8, // GX only
ATI_CRC_SIG = 0x00E8,
ATI_DST_Y_X_ALIAS1 = 0x00F4,
ATI_DST_OFF_PITCH = 0x0100,
@@ -170,87 +167,15 @@ enum {
ATI_MPP_DATA = 0x04CC,
ATI_TVO_CNTL = 0x0500,
ATI_SETUP_CNTL = 0x0704,
};
/* Mach64 PLL register indices. */
enum {
PLL_REF_DIV = 2, // reference divider, same for all Mach64 clocks
PLL_VCLK_CNTL = 5,
VCLK_POST_DIV = 6,
VCLK0_FB_DIV = 7, // feedback divider for VCLK0
VCLK1_FB_DIV = 8, // feedback divider for VCLK1
VCLK2_FB_DIV = 9, // feedback divider for VCLK2
VCLK3_FB_DIV = 10, // feedback divider for VCLK3
PLL_EXT_CNTL = 11,
ATI_INVALID = 0xFFFF
};
constexpr auto APERTURE_SIZE = 0x01000000UL; /* Mach64 aperture size */
constexpr auto MM_REGS_0_OFF = 0x007FFC00UL; /* offset to memory mapped registers, block 0 */
constexpr auto MM_REGS_1_OFF = 0x007FF800UL; /* offset to memory mapped registers, block 1 */
constexpr auto MM_REGS_2_OFF = 0x003FFC00UL; /* offset to memory mapped registers, 4MB aperture */
constexpr auto BE_FB_OFFSET = 0x00800000UL; /* Offset to the big-endian frame buffer */
constexpr auto ATI_XTAL = 14318180.0f; // external crystal oscillator frequency
class ATIRage : public PCIDevice {
public:
ATIRage(uint16_t dev_id, uint32_t mem_amount);
~ATIRage();
/* MMIODevice methods */
uint32_t read(uint32_t reg_start, uint32_t offset, int size);
void write(uint32_t reg_start, uint32_t offset, uint32_t value, int size);
bool supports_type(HWCompType type) {
return type == HWCompType::MMIO_DEV;
};
/* PCI device methods */
bool supports_io_space(void) {
return true;
};
uint32_t pci_cfg_read(uint32_t reg_offs, uint32_t size);
void pci_cfg_write(uint32_t reg_offs, uint32_t value, uint32_t size);
/* I/O space access methods */
bool pci_io_read(uint32_t offset, uint32_t size, uint32_t* res);
bool pci_io_write(uint32_t offset, uint32_t value, uint32_t size);
protected:
const char* get_reg_name(uint32_t reg_offset);
bool io_access_allowed(uint32_t offset, uint32_t* p_io_base);
uint32_t read_reg(uint32_t offset, uint32_t size);
void write_reg(uint32_t offset, uint32_t value, uint32_t size);
float calc_pll_freq(int scale, int fb_div);
void verbose_pixel_format(int crtc_index);
void crtc_enable();
void draw_hw_cursor(uint8_t *dst_buf, int dst_pitch);
void update_screen();
private:
uint8_t block_io_regs[2048] = {0};
uint8_t pci_cfg[256] = {0}; /* PCI configuration space */
uint8_t plls[64] = {0}; // internal PLL registers
/* CRT controller parameters */
bool crtc_on = false;
int active_width; // width of the visible display area
int active_height; // height of the visible display area
float pixel_clock;
float refresh_rate;
/* Video RAM variables */
uint32_t vram_size;
uint8_t* vram_ptr;
uint32_t aperture_base;
DisplayID* disp_id;
uint8_t palette[256][4]; /* internal DAC palette in RGBA format */
int comp_index; /* color component index for DAC palette access */
uint8_t *surface;
};
#endif /* ATI_RAGE_H */
#endif // ATI_MACH64_DEFS_H
+365
View File
@@ -0,0 +1,365 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** ATI Mach64 GX emulation.
It emulates an ATI88800GX controller with an IBM RGB514 style RAMDAC.
Emulation is limited to a basic framebuffer for now.
*/
#include <core/timermanager.h>
#include <devices/deviceregistry.h>
#include <devices/video/atimach64defs.h>
#include <devices/video/atimach64gx.h>
#include <devices/video/displayid.h>
#include <devices/video/rgb514defs.h>
#include <loguru.hpp>
#include <memaccess.h>
#include <string>
AtiMach64Gx::AtiMach64Gx()
: PCIDevice("ati-mach64-gx"), VideoCtrlBase(1024, 768)
{
supports_types(HWCompType::MMIO_DEV | HWCompType::PCI_DEV);
/* set up PCI configuration space header */
this->vendor_id = PCI_VENDOR_ATI;
this->device_id = ATI_MACH64_GX_DEV_ID;
this->class_rev = (0x030000 << 8) | 3;
this->bars_cfg[0] = 0xFF000000UL; // declare main aperture (16MB)
this->pci_notify_bar_change = [this](int bar_num) {
this->notify_bar_change(bar_num);
};
// declare expansion ROM containing FCode and Mac OS drivers
if (this->attach_exp_rom_image(std::string("113-32900-004_Apple_MACH64.bin"))) {
LOG_F(WARNING, "AtiMach64Gx: could not load ROM - this device won't work properly!");
}
// initialize display identification
this->disp_id = std::unique_ptr<DisplayID> (new DisplayID(0x07, 0x3A));
// allocate video RAM
this->vram_size = 2 << 20;
this->vram_ptr = std::unique_ptr<uint8_t[]> (new uint8_t[this->vram_size]);
}
void AtiMach64Gx::notify_bar_change(int bar_num)
{
if (bar_num) // only BAR0 is supported
return;
if (this->aperture_base != (this->bars[bar_num] & 0xFFFFFFF0UL)) {
if (this->aperture_base) {
LOG_F(WARNING, "AtiMach64Gx: deallocating I/O memory not implemented");
}
this->aperture_base = this->bars[0] & 0xFFFFFFF0UL;
this->host_instance->pci_register_mmio_region(this->aperture_base,
APERTURE_SIZE, this);
// copy aperture address to CONFIG_CNTL:CFG_MEM_AP_LOC
this->config_cntl = (this->config_cntl & 0xFFFFC00FUL) |
((this->aperture_base >> 18) & 0x3FF0U);
LOG_F(INFO, "AtiMach64Gx: aperture address set to 0x%08X", this->aperture_base);
}
}
// map I/O register index to MMIO register offset
static const uint32_t io_idx_to_reg_offset[32] = {
ATI_CRTC_H_TOTAL_DISP, ATI_CRTC_H_SYNC_STRT_WID,
ATI_CRTC_V_TOTAL_DISP, ATI_CRTC_V_SYNC_STRT_WID,
ATI_CRTC_VLINE_CRNT_VLINE, ATI_CRTC_OFF_PITCH,
ATI_CRTC_INT_CNTL, ATI_CRTC_GEN_CNTL,
ATI_OVR_CLR, ATI_OVR_WID_LEFT_RIGHT,
ATI_OVR_WID_TOP_BOTTOM, ATI_CUR_CLR0,
ATI_CUR_CLR1, ATI_CUR_OFFSET,
ATI_CUR_HORZ_VERT_POSN, ATI_CUR_HORZ_VERT_OFF,
ATI_SCRATCH_REG0, ATI_SCRATCH_REG1,
ATI_CLOCK_CNTL, ATI_BUS_CNTL,
ATI_MEM_CNTL, ATI_MEM_VGA_WP_SEL,
ATI_MEM_VGA_RP_SEL, ATI_DAC_REGS,
ATI_DAC_CNTL, ATI_GEN_TEST_CNTL,
ATI_CONFIG_CNTL, ATI_CONFIG_CHIP_ID,
ATI_CONFIG_STAT0, ATI_GX_CONFIG_STAT1,
ATI_INVALID, ATI_INVALID
};
bool AtiMach64Gx::pci_io_read(uint32_t offset, uint32_t size, uint32_t* res)
{
*res = 0;
// check for valid I/O base and I/O access permission
if ((offset & 0x3FC) != 0x2EC || !(this->command & 1)) {
return false;
}
// convert ISA-style I/O address to MMIO register offset
offset = io_idx_to_reg_offset[(offset >> 10) & 0x1F] + (offset & 3);
// CONFIG_CNTL is accessible from I/O space only
if ((offset & ~3) == ATI_CONFIG_CNTL) {
*res = read_mem(((uint8_t *)&this->config_cntl) + (offset & 3), size);
} else {
*res = this->read_reg(offset, size);
}
return true;
}
bool AtiMach64Gx::pci_io_write(uint32_t offset, uint32_t value, uint32_t size)
{
// check for valid I/O base and I/O access permission
if ((offset & 0x3FC) != 0x2EC || !(this->command & 1)) {
return false;
}
// convert ISA-style I/O address to MMIO register offset
offset = io_idx_to_reg_offset[(offset >> 10) & 0x1F] + (offset & 3);
// CONFIG_CNTL is accessible from I/O space only
if ((offset & ~3) == ATI_CONFIG_CNTL) {
write_mem(((uint8_t *)&this->config_cntl) + (offset & 3), value, size);
switch (this->config_cntl & 3) {
case 0:
LOG_F(WARNING, "AtiMach64Gx: linear aperture disabled!");
break;
case 1:
LOG_F(INFO, "AtiMach64Gx: aperture size set to 4MB");
this->mm_regs_offset = MM_REGS_2_OFF;
break;
case 2:
LOG_F(INFO, "AtiMach64Gx: aperture size set to 8MB");
this->mm_regs_offset = MM_REGS_0_OFF;
break;
default:
LOG_F(ERROR, "AtiMach64Gx: invalid aperture size in CONFIG_CNTL");
}
} else {
this->write_reg(offset, value, size);
}
return true;
}
uint32_t AtiMach64Gx::read_reg(uint32_t offset, uint32_t size)
{
LOG_F(INFO, "AtiMach64Gx: read from I/O mapped reg at 0x%X", offset);
return read_mem(&this->mm_regs[offset], size);
}
void AtiMach64Gx::write_reg(uint32_t offset, uint32_t value, uint32_t size)
{
uint8_t gpio_dirs, gpio_levels;
int crtc_en;
// write value to internal registers with endian conversion
write_mem(&this->mm_regs[offset], value, size);
switch (offset & ~3) {
case ATI_CRTC_GEN_CNTL:
crtc_en = (this->mm_regs[ATI_CRTC_GEN_CNTL+3] >> 1) & 1;
if (crtc_en != this->crtc_enable) {
if (!crtc_en) {
this->disable_crtc_internal();
}
}
break;
case ATI_DAC_REGS:
if (size == 1) { // only byte accesses are allowed for DAC registers
int dac_reg_addr = ((this->mm_regs[ATI_DAC_CNTL] & 1) << 2) | (offset & 3);
rgb514_write_reg(dac_reg_addr, value);
}
break;
case ATI_DAC_CNTL:
// monitor ID is usually accessed using 8bit writes
if ((offset & 3) == 3) {
gpio_dirs = (value >> 3) & 7;
gpio_levels = value & 7;
gpio_levels = this->disp_id->read_monitor_sense(gpio_levels, gpio_dirs);
this->mm_regs[ATI_DAC_CNTL+3] = (this->mm_regs[ATI_DAC_CNTL+3] & 0xF8U) | gpio_levels;
}
break;
default:
LOG_F(INFO, "AtiMach64Gx: write 0x%X to I/O mapped reg at 0x%X", value, offset);
}
}
uint32_t AtiMach64Gx::read(uint32_t rgn_start, uint32_t offset, int size)
{
if (rgn_start == this->aperture_base) {
if (offset < this->vram_size) {
return read_mem(&this->vram_ptr[offset], size);
} else if (offset >= this->mm_regs_offset) {
return read_reg(offset - this->mm_regs_offset, size);
}
}
// memory mapped expansion ROM region
if (rgn_start == this->exp_rom_addr && offset < this->exp_rom_size) {
return read_mem(&this->exp_rom_data[offset], size);
}
return 0;
}
void AtiMach64Gx::write(uint32_t rgn_start, uint32_t offset, uint32_t value, int size)
{
if (rgn_start == this->aperture_base) {
if (offset < this->vram_size) {
write_mem(&this->vram_ptr[offset], value, size);
} else if (offset >= this->mm_regs_offset) {
write_reg(offset - this->mm_regs_offset, value, size);
}
}
}
void AtiMach64Gx::enable_crtc_internal()
{
int new_width = (this->mm_regs[ATI_CRTC_H_TOTAL_DISP+2] + 1) * 8;
int new_height = (READ_WORD_LE_A(&this->mm_regs[ATI_CRTC_V_TOTAL_DISP+2]) & 0x7FFUL) + 1;
if (new_width != this->active_width || new_height != this->active_height) {
LOG_F(WARNING, "AtiMach64Gx: display window resizing not implemented yet!");
}
/* calculate display refresh rate */
int hori_total = (this->mm_regs[ATI_CRTC_H_TOTAL_DISP] + 1) * 8;
int vert_total = (READ_WORD_LE_A(&this->mm_regs[ATI_CRTC_V_TOTAL_DISP]) & 0x7FFUL) + 1;
this->refresh_rate = this->pixel_clock / hori_total / vert_total;
int32_t src_offset = (READ_DWORD_LE_A(&this->mm_regs[ATI_CRTC_OFF_PITCH]) & 0xFFFFFU) * 8;
this->fb_pitch = ((READ_DWORD_LE_A(&this->mm_regs[ATI_CRTC_OFF_PITCH])) >> 19) & 0x1FF8U;
this->fb_ptr = &this->vram_ptr[src_offset];
// specify framebuffer converter
switch (this->pixel_depth) {
case 8:
this->convert_fb_cb = [this](uint8_t *dst_buf, int dst_pitch) {
this->convert_frame_8bpp(dst_buf, dst_pitch);
};
break;
default:
ABORT_F("AtiMach64Gx: unsupported pixel depth %d", this->pixel_depth);
}
uint64_t refresh_interval = static_cast<uint64_t>(1.0f / this->refresh_rate * NS_PER_SEC + 0.5);
TimerManager::get_instance()->add_cyclic_timer(
refresh_interval,
[this]() {
this->update_screen();
}
);
this->update_screen();
this->crtc_on = true;
this->crtc_enable = 1;
}
void AtiMach64Gx::disable_crtc_internal()
{
this->crtc_enable = 0;
}
// ========================== IBM RGB514 related code ==========================
void AtiMach64Gx::rgb514_write_reg(uint8_t reg_addr, uint8_t value)
{
switch (reg_addr) {
case Rgb514::CLUT_ADDR_WR:
this->clut_index = value;
break;
case Rgb514::CLUT_DATA:
this->clut_color[this->comp_index++] = value;
if (this->comp_index >= 3) {
// TODO: combine separate components into a single ARGB value
this->palette[this->clut_index][0] = this->clut_color[0];
this->palette[this->clut_index][1] = this->clut_color[1];
this->palette[this->clut_index][2] = this->clut_color[2];
this->palette[this->clut_index][3] = 255;
this->clut_index++;
this->comp_index = 0;
}
break;
case Rgb514::CLUT_MASK:
if (value != 0xFF) {
LOG_F(WARNING, "RGB514: pixel mask set to 0x%X", value);
}
break;
case Rgb514::INDEX_LOW:
this->dac_idx_lo = value;
break;
case Rgb514::INDEX_HIGH:
this->dac_idx_hi = value;
break;
case Rgb514::INDEX_DATA:
this->rgb514_write_ind_reg((this->dac_idx_hi << 8) + this->dac_idx_lo, value);
break;
default:
LOG_F(WARNING, "RGB514: access to unimplemented register at 0x%X", reg_addr);
}
}
void AtiMach64Gx::rgb514_write_ind_reg(uint8_t reg_addr, uint8_t value)
{
LOG_F(INFO, "RGB514: write 0x%X to register 0x%X", value, reg_addr);
this->dac_regs[reg_addr] = value;
switch (reg_addr) {
case Rgb514::MISC_CLK_CNTL:
if (value & PLL_ENAB) {
if ((this->dac_regs[Rgb514::PLL_CTL_1] & 3) != 1)
ABORT_F("RGB514: unsupported PLL source");
int m = 8 >> (this->dac_regs[Rgb514::F0_M0] >> 6);
int vco_div = (this->dac_regs[Rgb514::F0_M0] & 0x3F) + 65;
int ref_div = (this->dac_regs[Rgb514::F1_N0] & 0x1F) * m;
this->pixel_clock = ATI_XTAL * vco_div / ref_div;
LOG_F(INFO, "RGB514: dot clock set to %f Hz", this->pixel_clock);
}
break;
case Rgb514::PIX_FORMAT:
if (value == 3) {
this->pixel_depth = 8;
// HACK: not the best place for enabling display output!
this->enable_crtc_internal();
} else {
ABORT_F("RGB514: unimplemented pixel format %d", value);
}
break;
}
}
static const DeviceDescription AtiMach64Gx_Descriptor = {
AtiMach64Gx::create, {}, {}
};
REGISTER_DEVICE(AtiMach64Gx, AtiMach64Gx_Descriptor);
Loaded 100 of 192 files, more files were not shown because too many files have changed in this diff. Show more