[← Home](../README.md) · [CPU & MMU](README.md) # 68040.library and 68060.library — CPU Support Libraries ## Overview The 68040 and 68060 processors **removed certain instructions** that the 68020/68030 supported in hardware. These "unimplemented" instructions cause a Line-F exception when executed. The `68040.library` and `68060.library` are **trap handler libraries** that catch these exceptions and **emulate the missing instructions in software**, providing transparent backward compatibility. Without these libraries, any program using the affected instructions would crash with a Line-F exception on 040/060 hardware. --- ## What Instructions Are Emulated? ### 68040.library The 68040 removed several FPU instructions that the 68881/68882 supported: | Category | Missing Instructions | Description | |---|---|---| | Transcendental FPU | `FSIN`, `FCOS`, `FTAN`, `FASIN`, `FACOS`, `FATAN` | Trig functions | | Transcendental FPU | `FSINH`, `FCOSH`, `FTANH`, `FATANH` | Hyperbolic functions | | Transcendental FPU | `FLOG2`, `FLOG10`, `FLOGN`, `FLOGNP1` | Logarithms | | Transcendental FPU | `FETOX`, `FETOXM1`, `FTWOTOX`, `FTENTOX` | Exponentials | | Other FPU | `FMOD`, `FREM` | Modulo/remainder | | Other FPU | `FGETEXP`, `FGETMAN` | Get exponent/mantissa | | Other FPU | `FSGLDIV`, `FSGLMUL` | Single-precision ops | | Integer | `MOVEP` | Move peripheral (not on all 040 revisions) | ### 68060.library The 68060 removed **everything the 040 removed** plus additional instructions: | Category | Additionally Missing | Description | |---|---|---| | Integer | `MOVEP` | Move peripheral (byte-strided) | | Integer | `CAS2` | Compare-and-swap dual | | Integer | `CHK2`, `CMP2` | Range check | | Integer | `MULU.L (64-bit)` | 64-bit unsigned multiply | | Integer | `MULS.L (64-bit)` | 64-bit signed multiply | | Integer | `DIVU.L (64-bit)` | 64-bit unsigned divide | | Integer | `DIVS.L (64-bit)` | 64-bit signed divide | | FPU | All 68040-missing FPU ops | Same as above | | FPU | `FMOVECR` | Move constant ROM | | FPU | `FDABS`, `FDSQRT`, etc. | Some double-precision ops | --- ## How They Work ``` 1. Program executes FSIN (opcode $F200 xxxx) 2. 68040/060 CPU has no microcode for this → Line-F exception (#11) 3. CPU vectors to the Line-F exception handler 4. 68040.library's handler decodes the opcode from the stack frame 5. Software emulates FSIN using basic FADD/FMUL/FDIV 6. Result is placed in the correct FPU register 7. Handler returns → program continues as if nothing happened ``` --- ## Installation These libraries are **loaded at boot time** as resident modules. They install themselves as the Line-F exception vector handler. ``` ; In startup-sequence or user-startup: LIBS:68040.library ; for 68040 systems ; or: LIBS:68060.library ; for 68060 systems (replaces 68040.library) ``` The library is typically loaded by `SetPatch` or an explicit `C:LoadModule`: ``` C:LoadModule LIBS:68040.library ; or for 68060: C:LoadModule LIBS:68060.library ``` --- ## struct (ROM Tag) Both libraries register as `RTF_COLDSTART` resident modules with high priority to ensure they are initialised before any user code runs: ```c /* Typical RomTag for 68040.library: */ static struct Resident romtag = { RTC_MATCHWORD, /* $4AFC */ &romtag, &endskip, RTF_COLDSTART, /* flags: cold start */ 40, /* version */ NT_LIBRARY, 105, /* priority: very high */ "68040.library", "68040.library 40.1 (1.1.93)\r\n", initRoutine }; ``` --- ## Detection ```c /* Check which CPU is present: */ if (SysBase->AttnFlags & AFF_68040) /* 68040 */ if (SysBase->AttnFlags & AFF_68060) /* 68060 */ /* AttnFlags bits: */ #define AFB_68010 0 #define AFB_68020 1 #define AFB_68030 2 #define AFB_68040 3 #define AFB_68060 7 /* added by 68060.library */ #define AFB_68881 4 /* FPU present */ #define AFB_68882 5 #define AFB_FPU40 6 /* 040 internal FPU */ ``` --- ## Performance Impact Software emulation of transcendental FPU instructions is **10–100x slower** than the 68881/68882 hardware implementation. Performance-critical code should: - Use lookup tables for trig functions - Use polynomial approximations (Chebyshev, CORDIC) - Avoid `FSIN`/`FCOS` in tight loops --- ## Common Sources | Library | Source | |---|---| | 68040.library 37.4 | Commodore (OS 3.0 distribution) | | 68040.library 40.1 | Commodore (OS 3.1 distribution) | | 68060.library 40.1 | Phase5 (original 68060 accelerators) | | 68060.library 46.x | Motorola reference implementation | --- ## References - Motorola: *MC68040 User's Manual* — unimplemented instruction list - Motorola: *MC68060 User's Manual* — unimplemented instruction list - NDK39: `exec/execbase.h` — `AttnFlags` - Phase5: 68060.library source (public domain)