diff --git a/README.md b/README.md index a480c3d..e0e486d 100644 --- a/README.md +++ b/README.md @@ -49,16 +49,18 @@ behave as on-target. USB-only commands (`probe`/`version`/`list`) are declined. ## Flash programming Reference RAM flash-routine method: vendored routines run on the target CPU. -Geometry (`--part`) comes from a build-time table of 188 parts (vendored USBDM -device DB), which also backs `identify`. Families: HCS08/RS08, HCS12, CFV1, plus -S12G EEPROM. Destructive commands confirm unless `--force`. +Geometry (`--part`) comes from a build-time table of 193 parts (vendored USBDM +device DB), which also backs `identify`. Families: HCS08/RS08, HCS12 (classic S12, +S12G, and S12XE/XS/XF on the paged map), CFV1, plus S12G EEPROM. Destructive +commands confirm unless `--force`. HCS12 flash operations start with a +special-mode reset, so the target does not need to be halted first. ## GDB remote stub `usbdm gdb` serves the GDB remote protocol over TCP: registers, memory, single-step, `continue`/`vCont`, flash `load` (`vFlash*`), `monitor reset`/`halt`. -Hardware breakpoints on HCS08, classic-S12 HCS12, and CFV1; S12G and watchpoints -use GDB software breakpoints. +Hardware breakpoints on HCS08, classic-S12 HCS12, and CFV1; S12G, S12X and +watchpoints use GDB software breakpoints. ## Layout diff --git a/build.zig b/build.zig index 8490a23..5e659ad 100644 --- a/build.zig +++ b/build.zig @@ -90,6 +90,9 @@ pub fn build(b: *std.Build) void { exe.root_module.addAnonymousImport("flash_hcs12_gmmc", .{ .root_source_file = b.path("vendor/flash-routines/HCS12-GMMC-FTMRG-flash-program.s19"), }); + exe.root_module.addAnonymousImport("flash_hcs12_xmmc", .{ + .root_source_file = b.path("vendor/flash-routines/HCS12-XMMC-XFTMRK-flash-program.s19"), + }); exe.root_module.addAnonymousImport("flash_cfv1", .{ .root_source_file = b.path("vendor/flash-routines/ColdfireV1-default-flash-program.elf.S19"), }); diff --git a/src/device.zig b/src/device.zig index 20754ee..5fbb12f 100644 --- a/src/device.zig +++ b/src/device.zig @@ -101,6 +101,20 @@ test "generated HCS08 table: geometry and routine-blob selection" { try std.testing.expectEqual(@as(u32, 8), g240.write_align); try std.testing.expectEqual(@as(u32, 0x1400), g240.ram_start); try std.testing.expectEqual(@as(u32, 0x100), g240.eeprom.?.sector); // erase sector, not the XML phrase size + // S12XE: XMMC/XFTMRK, FTM at 0x0100, PPAGE-banked C0..FF from pages="1024K". + const xep = lookup("mc9s12xep100-family").?; + try std.testing.expectEqual(Hcs12Routine.xmmc_xftmrk, xep.hcs12_routine); + try std.testing.expectEqual(@as(u32, 0x0100), xep.flash_base); + try std.testing.expectEqual(@as(u32, 8), xep.write_align); + try std.testing.expectEqual(@as(u32, 1024), xep.page_size); + try std.testing.expectEqual(@as(u16, 0x0015), xep.paged.?.ppage_addr); + try std.testing.expectEqual(@as(u8, 0xC0), xep.paged.?.page_first); + try std.testing.expectEqual(@as(u8, 0xFF), xep.paged.?.page_last); + try std.testing.expect(xep.spanInFlash(0xFA8000, 0xFABFFF) and xep.spanInFlash(0xC000, 0xFFFF)); + // Size-derived page count, not the 0xFE reset page: XS256 is 16 pages. + try std.testing.expectEqual(@as(u8, 0xF0), lookup("mc9s12xs256").?.paged.?.page_first); + // The linear-map entry stays out: its window is not reachable by the paged routine. + try std.testing.expect(lookup("mc9s12xep-linear") == null); } test "generated device table: known parts resolve with sane geometry" { diff --git a/src/device_types.zig b/src/device_types.zig index 9c4f011..bb9de6f 100644 --- a/src/device_types.zig +++ b/src/device_types.zig @@ -6,8 +6,8 @@ const std = @import("std"); /// Flash family - selects the RAM routine + ABI (HCS08 small-code, HCS12 large-code). pub const Family = enum { hcs08, hcs12, cfv1 }; -/// HCS12 routine: classic S12 = MMCV4/FTS, S12G = GMMC/FTMRG. -pub const Hcs12Routine = enum { mmcv4_fts, gmmc_ftmrg }; +/// HCS12 routine: classic S12 = MMCV4/FTS, S12G = GMMC/FTMRG, S12XE = XMMC/XFTMRK. +pub const Hcs12Routine = enum { mmcv4_fts, gmmc_ftmrg, xmmc_xftmrk }; /// PPAGE-banked HCS12 flash. Window always 0x8000-0xBFFF; pages /// page_first..page_last map globally as (page << 16) | offset (routine sets diff --git a/src/main.zig b/src/main.zig index ed558ac..13681cf 100644 --- a/src/main.zig +++ b/src/main.zig @@ -546,6 +546,10 @@ fn runSim( sim.cpu = .{ .hcs08 = &hcs08_cpu }; }, .hcs12 => { + // No S12XE (XFTMRK) flash model: the FTS model would run the routine + // against the wrong controller and report a meaningless pass. + if (d.hcs12_routine == .xmmc_xftmrk) + fatalCode(.usage, "--sim has no S12XE (XFTMRK) flash model; '{s}' needs real hardware", .{d.name}); hcs12_cpu = if (d.hcs12_routine == .gmmc_ftmrg) lib.cpu12.Cpu.initGmmc(arena, d.flash_base) catch fatal("out of memory building the emulator", .{}) else @@ -1332,9 +1336,11 @@ const lib_device = lib.device; // Vendored HCS08 small-code routines (embedded). 0xB0 variant: RAM above 0x80. const flash_blob_80 = @embedFile("flash_hcs08_0x80"); const flash_blob_b0 = @embedFile("flash_hcs08_0xB0"); -// Vendored HCS12 large-code routines. FTS = S12 MMCV4; GMMC = S12G FTMRG. +// Vendored HCS12 large-code routines. FTS = S12 MMCV4; GMMC = S12G FTMRG; +// XMMC = S12XE XFTMRK (same large-code ABI as GMMC). const flash_blob_hcs12_fts = @embedFile("flash_hcs12_fts"); const flash_blob_hcs12_gmmc = @embedFile("flash_hcs12_gmmc"); +const flash_blob_hcs12_xmmc = @embedFile("flash_hcs12_xmmc"); // Vendored CFV1 (ColdFire V1) large-code routine (32-bit ABI, relocatable-off). const flash_blob_cfv1 = @embedFile("flash_cfv1"); @@ -1555,6 +1561,14 @@ fn prepareFlash(arena: std.mem.Allocator, bdm: *Bdm, flags: *const Flags, out: * fatal("--part '{s}' has a flash controller base 0x{x} that doesn't fit the {s} routine's 16-bit field", .{ d.name, d.flash_base, @tagName(d.family) }); } bringUp(bdm, flags) catch |err| fatal("connect failed: {s}", .{@errorName(err)}); + // HCS12: a target running its own code acks memory access but not a PC + // write (BDM_RC_ACK_TIMEOUT), so the routine cannot be started. Reset into + // special mode first, as the reference programmer does ("reset s h"): the + // CPU comes up halted in active BDM on its reset clock. Measure after this. + if (d.family == .hcs12) { + bdm.sess.reset(.special, .hardware) catch |err| fatal("special-mode reset failed: {s}", .{@errorName(err)}); + bdm.sess.connect() catch |err| fatal("connect after reset failed: {s}", .{@errorName(err)}); + } const bus_hz = flags.bus_hz orelse blk: { // sync==0: no measurement; sync==1: firmware "unknown" sentinel (syncToHz // clamps to ~4.3 GHz). Treat both as unknown, demand --bus-hz. @@ -1564,9 +1578,43 @@ fn prepareFlash(arena: std.mem.Allocator, bdm: *Bdm, flags: *const Flags, out: * out.print("{s}using measured bus clock ~{d} kHz (override with --bus-hz){s}\n", .{ st.dim, hz / 1000, st.reset }) catch {}; break :blk hz; }; + if (d.family == .hcs12 and d.hcs12_routine == .xmmc_xftmrk) initXmmcFlash(bdm, d, bus_hz); return flashPrepFor(arena, d, bus_hz); } +/// XFTMRK flash-clock divider for a bus clock in kHz, or null if no divider puts +/// the flash clock in 800..1050 kHz. Clocked from OSCCLK = 2 x the special-mode +/// bus clock (ref HCS12-XFTMRK-flash-scripts.tcl calculateFlashDivider). +fn xmmcFlashDivider(bus_khz: u32) ?u8 { + if (bus_khz < 1000) return null; + const fm: f64 = 2.0 * @as(f64, @floatFromInt(bus_khz)); + const div = @floor(fm / 1050.0 - 0.0001); + if (div < 0 or div > 0x3F) return null; + const flash_clk = fm / (div + 1.0); + if (flash_clk < 800 or flash_clk > 1050) return null; + return @intFromFloat(div); +} + +/// The XFTMRK routine expects FCLKDIV already loaded (it reports an invalid +/// divider otherwise), so do what the reference Tcl initTarget/initFlash do: +/// disable COP, load FCLKDIV, unprotect P-flash and EEPROM. +fn initXmmcFlash(bdm: *Bdm, d: lib_device.Device, bus_hz: u32) void { + const div = xmmcFlashDivider(bus_hz / 1000) orelse + fatal("no XFTMRK flash clock divider for a {d} kHz bus clock (needs 800..1050 kHz from 2x bus); check --bus-hz", .{bus_hz / 1000}); + const base: u32 = d.flash_base; + const writes = [_]struct { addr: u32, val: u8 }{ + .{ .addr = d.watchdog_addr, .val = 0x40 }, // COPCTL: COP off + .{ .addr = base + 0x00, .val = div }, // FCLKDIV + .{ .addr = base + 0x08, .val = 0xFF }, // FPROT: unprotected + .{ .addr = base + 0x09, .val = 0xFF }, // EPROT: unprotected + }; + for (writes) |w| { + if (w.addr == 0) continue; + bdm.sess.writeMem(.byte, w.addr, &.{w.val}) catch |err| + fatal("XFTMRK flash init: write 0x{x:0>4} failed: {s}", .{ w.addr, @errorName(err) }); + } +} + /// FlashPrep for a device at a known bus clock, no I/O (shared by prepareFlash + gdb). fn flashPrepFor(arena: std.mem.Allocator, d: lib_device.Device, bus_hz: u32) FlashPrep { // frequency_khz is u16 (max ~65.5 MHz); reject rather than overflow the cast. @@ -1593,6 +1641,7 @@ fn flashPrepFor(arena: std.mem.Allocator, d: lib_device.Device, bus_hz: u32) Fla const blob = switch (d.hcs12_routine) { .mmcv4_fts => flash_blob_hcs12_fts, .gmmc_ftmrg => flash_blob_hcs12_gmmc, + .xmmc_xftmrk => flash_blob_hcs12_xmmc, }; const routine = ramflash.parseLarge(arena, blob) catch |err| fatal("cannot load HCS12 flash routine: {s}", .{@errorName(err)}); @@ -2407,6 +2456,24 @@ test "e2e HCS08: program -> readback -> blank-check(not blank) -> erase -> blank try std.testing.expectEqualSlices(u8, &.{ 0xFF, 0xFF, 0xFF, 0xFF }, &back); } +test "XFTMRK flash divider matches the reference Tcl calculation" { + // 12 MHz OSCCLK board, measured in special mode: 5990 kHz -> 11 (~998 kHz). + try std.testing.expectEqual(@as(?u8, 11), xmmcFlashDivider(5990)); + try std.testing.expectEqual(@as(?u8, 11), xmmcFlashDivider(6000)); + try std.testing.expectEqual(@as(?u8, 15), xmmcFlashDivider(8000)); + try std.testing.expectEqual(@as(?u8, null), xmmcFlashDivider(999)); +} + +test "parseLarge reads the vendored XFTMRK blob (same large-code ABI as GMMC)" { + const gpa = std.testing.allocator; + const r = try ramflash.parseLarge(gpa, flash_blob_hcs12_xmmc); + defer gpa.free(r.image); + try std.testing.expectEqual(@as(u32, 0x3000), r.image_address); + try std.testing.expect(r.capabilities & ramflash.CAP_RELOCATABLE != 0); + try std.testing.expect(r.capabilities & ramflash.CAP_DATA_FIXED == 0); + try std.testing.expect(r.entry > r.image_address and r.entry < r.image_address + r.image.len); +} + test "e2e HCS12: large-code program + verify + mass-erase via the sim" { const gpa = std.testing.allocator; const d = lib_device.lookup("mc9s12c32").?; diff --git a/tools/gen_devices.zig b/tools/gen_devices.zig index 383bf47..b0aba38 100644 --- a/tools/gen_devices.zig +++ b/tools/gen_devices.zig @@ -170,6 +170,7 @@ const SUP_HCS08 = [_][]const u8{ "HCS08-default-flash-program", "HCS08-small-fla const MMCV4 = [_][]const u8{ "HCS12-MMCV4-FTS-flash-program", "HCS12-MMCV4-FTS_2-flash-program", "HCS12-MMCV4-FTS_T-flash-program" }; const GMMC = "HCS12-GMMC-FTMRG-flash-program"; const GMMC_EE = "HCS12-GMMC-FTMRG-eeprom-program"; +const XMMC = "HCS12-XMMC-XFTMRK-flash-program"; const SUP_CFV1 = [_][]const u8{ "CFV1-default-FlashProgram", "CFV1-Watchdog-FlashProgram" }; fn inList(list: []const []const u8, s: []const u8) bool { @@ -408,7 +409,7 @@ fn processFile( const supported = switch (fam) { .hcs08 => inList(&SUP_HCS08, rt), - .hcs12 => inList(&MMCV4, rt) or std.mem.eql(u8, rt, GMMC), + .hcs12 => inList(&MMCV4, rt) or std.mem.eql(u8, rt, GMMC) or std.mem.eql(u8, rt, XMMC), .cfv1 => inList(&SUP_CFV1, rt), }; if (!supported) { @@ -510,7 +511,8 @@ fn processFile( const flash_end2 = if (has_w2) merged[1].end else 0; const is_gmmc = fam == .hcs12 and std.mem.eql(u8, rt, GMMC); - const hcs12_routine: []const u8 = if (is_gmmc) "gmmc_ftmrg" else "mmcv4_fts"; + const is_xmmc = fam == .hcs12 and std.mem.eql(u8, rt, XMMC); + const hcs12_routine: []const u8 = if (is_gmmc) "gmmc_ftmrg" else if (is_xmmc) "xmmc_xftmrk" else "mmcv4_fts"; var have_paged = false; var pg_first: u64 = 0; var pg_last: u64 = 0; @@ -521,8 +523,17 @@ fn processFile( const pe = attr(b, "pageEnd"); if (pr != null and pe != null) { saw_reset = true; - const prv = parseNum(pr.?); const pev = parseNum(pe.?); + // MMCV4 entries give the lowest page as pageReset. XMMC ones give + // the reset page (0xFE) and size the window with `pages` instead: + // a byte size when >= 1024 (e.g. "1024K" -> 64 pages), else a count + // (ref DeviceXmlParser memoryRange). + const prv = if (is_xmmc) blk: { + const pg = attr(b, "pages") orelse break :blk parseNum(pr.?); + const n = parseNum(pg); + const count = if (n >= 1024) n / 0x4000 else n; + break :blk if (count >= 1 and count <= pev + 1) pev + 1 - count else parseNum(pr.?); + } else parseNum(pr.?); // paged only if pages exist below the top two fixed windows; else they cover all -> null if (prv + 2 <= pev) { have_paged = true; diff --git a/vendor/flash-routines/HCS12-XMMC-XFTMRK-flash-program.s19 b/vendor/flash-routines/HCS12-XMMC-XFTMRK-flash-program.s19 new file mode 100644 index 0000000..b818512 --- /dev/null +++ b/vendor/flash-routines/HCS12-XMMC-XFTMRK-flash-program.s19 @@ -0,0 +1,35 @@ +S06A0000433A5C555342444D5C466C6173685F50726F6772616D6D696E675F526F7574696E65735C48435331322D584D4D432D5846544D524B2D666C6173682D70726F6772616D5C62696E5C48435331322D584D4D432D5846544D524B2D666C6173682D70726F6772616DF8 +S12330003002300033E5E03E00000000000000000000000000000000000000000000000014 +S1233020000000000000000000000000000000000000000000000000EEFAFFD26B030C0059 +S12330400000B7C55B1018EC003DB7450E008034EC01C140251A86FDC1802508A601C1C040 +S1233060250686FFE701261F585849498A402012C1082513C10CC40337B7018759598A10DA +S1233080EAB06C0169003DC60205FAFFAB1B9CEEFAFF7B6E80EE800F01011DEE80ED046D9F +S12330A0820E408006C60B15FAFF8DEE82C6FF6B086B09EE800D01011B843D1B9DEEFAFF36 +S12330C04DEE041A066E81EE81C6806B00EE81E6006B800F80B0F60F802006C60815FAFF78 +S12330E0570F801006C60915FAFF4D1B833D1B9CEEFAFF1A6E80EE800F010218EE80ED042E +S12331006D82CC08306B4669426A4A15FAFFACEE800D01021B843D1B93ECFAFEF16C86EE31 +S1233120860E011004182000BDEE86EC046C8BEE86EC0A6C80EC0C6C82EE86ECE0106C8812 +S1233140EE86EC0E496C84EC818C40002416C6116B8A0F830106C60215FAFEDCEE860F0FA9 +S1233160011C2014C6066B8A0F830706C60215FAFEC6EE860F0F0706C60215FAFEBAEE8B58 +S1233180C6306B062052EE8B6902E68A6B0AE6816B0BC6016B02EC826C0AC602074BEC840A +S12331A0270D186384C6036B02EC716D886C0AEC842707186384C604072FEC842707186324 +S12331C084C605072415FAFEF2EC82C300086C82EC80C90089006C80EE84191F6D8404758D +S12331E0A5EE860D01101B8D3D6B02ED8AEC716D8A6C0A3D1B96ECFAFE146C84EE840F01A9 +S12332002059EE84EC0A6C80EC0C6C82EE84ECE0106C88EE84EC0E496C860F83012FC60284 +S123322015FAFE142027EC82EE8015FAFE14EE8819026D88AC002706C60515FAFDFAEC8287 +S1233240C300026C82EC80C90089006C80EE86191F6D860475D0EE840D01201B8A3D1BF197 +S1233260EDECFAFDA96C88EE880E010404182000B4EE88EC046CF011EE88EC0A6C80EC0C41 +S12332806C82EE88EC0E830001E3826C86CC000018E3806C84EE88ED086D8F0318516D8CEE +S12332A0CCFFFF6C8AEE88ED0E26022073EE8218A48C6E82E481A4806C80EE8F18AB866ECD +S12332C086CC000018E3846C8418A48C6E86EC8AE485A4846C84EC818C40002403C6128F2F +S12332E0C60A6B8EEEF011C6306B062027EEF0116902E68E6B0AE6816B0BC6016B02EC82A2 +S12333006C0A15FAFDB5EC8FE3826C82CC000018E3806C80EC82AC8626D3EC80AC8426CD49 +S1233320EE880D01041BF0133D1B98EEFAFCDF6E84EE840F01084CEE84EC0A6C80EC0C6CB0 +S123334082EE84EC0EC30001496C860F83012BC60215FAFCE32023EC82EE8015FAFCE304F7 +S12333608406C60615FAFCD0186386EC82C300026C82EC80C90089006C80EC8626D9EE8469 +S12333800D01081B883DEEFAFC840F00010ECD00006D08876D060430FD0220F83D1B9C1A18 +S12333A0FAFC5F6E82EE82EC0C6C80EE82ED066940EE800D00801869021A0AB75415FAFCAC +S12333C08915FAFCC815FAFFBD15FAFD2115FAFE8D15FAFF5415FAFD3E15FAFE17EE800CB1 +S11533E000801B843D14101BFAFC4C3215FAFFAD003DD0 +S105FFFE33F1D9 +S9030000FC diff --git a/vendor/flash-routines/README.md b/vendor/flash-routines/README.md index 6e7d991..7813253 100644 --- a/vendor/flash-routines/README.md +++ b/vendor/flash-routines/README.md @@ -2,3 +2,7 @@ - [Source tree @ 4f18e1b](https://github.com/podonoghue/usbdm-flash-routines/tree/4f18e1bf67138d5b23927258a31590cbda247863) - License: GPL +- `HCS12-XMMC-XFTMRK-flash-program.s19` is from the USBDM 4.12 package + ([usbdm-eclipse-makefiles-build](https://github.com/podonoghue/usbdm-eclipse-makefiles-build) + `PackageFiles/DeviceData/HCS12/`), the same place the FTS and GMMC blobs here + match byte for byte.