Files
vincente adeafa5742 Add POSIX a9/xld/xc9 recreation sources and goldens.
Keep WINTEK/ read-only. Add assembler, linker, compiler driver, hex, runtime asm, reloc S-record docs, tests/golden, Makefile, and a pointer to the Mimic 21-Implementation plan. Ignore host bin/ and object files.
2026-10-18 13:41:26 -05:00

114 lines
10 KiB
Markdown

# AGENTS.md - Instructions for the cc09 Project
## Implementation plan
Later harness: read `/Users/vincente/Mimic/.V/01-Artifacts/21-Implementation/` (latest `Implementation_Plan.md`). cc09 waves: WA (a9), WB (xld), WC2 (xc9), WF (6800 assembler). Do not write `WINTEK/`. SuperMon 64 is mimic09.
## Mission
Your mission is to recreate the WINTEK 6809 cross assembler, linker, and C compiler for MacOS and UNIX Linux. Create an AGENTS.MD files with these instructions. A key feature of this toolset is the ability to generate Relocatable S-Records so that Assembly and C can be fed into the common linker. The WINTEK directory contains the original code, but WINTEK is long out of business and the source code has been lost. Try reverse compile these EXE files to capture the original functionality. Create a new top level directory structure for the new source code and executables, but do not touch the original code in the WINTEK directory. Results will be uploaded into http://gitea.binary-systems.com/vincente/cc09
## Project Rules (Critical)
- **NEVER modify or write files inside the WINTEK/ directory.** It preserves the original 1995 DOS binaries, runtime sources (.ASM, .H), sample .REL/.LIB/.MIK, .STA, batch files, etc. All new work goes in new top-level directories.
- The original WINTEK tools (A9.EXE = 6809 assembler v3.1r0, XLD.EXE = linker, XC9.EXE driver + XC_1.EXE pass1 + XC9_3.EXE codegen, HEX.EXE, TE.EXE) are the behavioral spec.
- Primary unique feature: **Relocatable S-Records**. The object/library format is an extension of Motorola S-records using non-standard record types (S4, S8, SD, and specially formatted S2/S3/S7 payloads) that carry section info, symbol definitions (GLOBAL/EXTERN), module chunks, and relocation data. This allows .REL files produced by the assembler (from hand asm or from C compiler's emitted .ASM) and .LIB archives to be fed to the common linker (XLD) to produce absolute S-record images (standard S1/S9 etc.).
- Support mixing Assembly and C objects through this common relocatable object format and linker.
- Target host platforms: macOS and Linux/UNIX. Tools must be portable, written in clean C (C99/C11), build with make + gcc/clang, no MS-DOS dependencies.
- CLI compatibility: Match original command lines and options as closely as practical for drop-in replacement in existing build flows (BATCH files, .DIR linker scripts, etc.).
- Assembler (a9): `a9 input-files [-l listing-file] [-o output-file] [-nh]`
- Linker (xld): `xld [-v] [-nh] [-map mapfile] [userfile ...] [-d dirfile] [-l libfile ...] [-o absfile]`
- C driver (xc9): invokes passes, produces .asm -> .rel -> link using defaults like rnlib9.lib + memory layout.
- Original also had: Wintek 6809 Assembler Version 3.1r0 (1986), C passes v2.1r0 / driver v1.0r3, full macro/conditional/ section/BSCT support, sophisticated memory reservation in linker (SECTION/MODULE directives), map files, etc.
- Runtime: The provided .ASM (CSTART.ASM, CHARIO.ASM, CLRIO.ASM, STDIO.ASM) + IOLOC.H + STDIO.H + prebuilt libs (KRLIB9.LIB, RNLIB9.LIB containing printf etc + the many .L* .D* .F* .I* runtime helpers for long/float/double/mul/div/switch etc.) must be supported. New implementation should be able to assemble these and produce equivalent (or improved) .REL outputs that the new linker accepts.
- Output deliverables live in new top-level dirs (see structure below). Final artifacts (source + built native binaries for mac/linux) go to the gitea remote.
## Directory Structure (New Top Level - Do Not Touch WINTEK/)
```
cc09/ # This repo root (new artifacts here)
├── AGENTS.md # This file (and any updates)
├── README.md # User guide, build, usage, status vs original
├── WINTEK/ # **READ-ONLY ORIGINAL** - untouched 1995 bits
│ ├── *.EXE # A9, XLD, XC*, HEX, TE
│ ├── *.ASM *.H # Original runtime sources (use as spec/tests)
│ ├── *.REL *.LIB # Original reloc S-record examples
│ ├── *.MIK *.DIR # Sample outputs and linker directives
│ └── ... # (other data, batches, sta, etc.)
├── src/
│ ├── common/ # Shared: srec_reloc.{c,h}, opcode6809.{c,h}, expr.{c,h}, sections.{c,h}, util.{c,h}
│ ├── a9/ # Assembler sources + local Makefile snippet
│ │ └── a9.c ...
│ ├── xld/ # Linker sources
│ │ └── xld.c ...
│ ├── xc9/ # C compiler (driver + passes). Note: full reimpl is large.
│ │ ├── xc9.c # Driver (orchestrates or shells out)
│ │ └── ... # Pass1 (frontend), pass3 (6809 cg) - can be phased
│ └── Makefile # or use root
├── include/ # Target headers for C (port of STDIO.H etc + new)
│ └── stdio.h
│ └── ...
├── lib/ # Runtime sources (asm) + build rules to produce .rel/.lib
│ ├── src/
│ │ ├── cstart.asm
│ │ ├── chario.asm
│ │ ├── clrio.asm
│ │ ├── stdio.asm
│ │ └── ...
│ ├── krcstart.rel # (generated)
│ └── rnlib9.lib ... # (may start with ports of original .REL content or rebuild)
├── bin/ # Built host tools (a9, xld, xc9, hex, ...) - gitignore or not
├── examples/
│ ├── asm/
│ └── c/
├── docs/
│ ├── reloc-srecords.md # Detailed reverse-engineered format spec
│ ├── 6809-calling-conv.md
│ └── wintek-compat.md
├── tests/
│ ├── asm/
│ ├── linker/
│ └── c/
├── Makefile # Top level: build all, test, clean, install-to-bin/
└── ... (LICENSE, .gitignore, etc.)
```
## Implementation Priorities & Notes
1. **Relocatable S-Records are first-class.** Reverse the exact record usage (S0 module header with name, S4 for section/alloc/BSCT control, S8 for symbol export with local offset+attrs, S2/S3 data with embedded <section><offset> + payload (with zero placeholders for fixups), S7 chunk terminators per symbol/proc, SD trailer, S9 end). The "addr" fields and embedded bytes are critical for the linker to place contributions without overlap and apply relocs (8-bit, 16-bit, PC-rel, external refs).
2. Start by making `a9` and `xld` able to round-trip the original runtime sources (assemble CSTART.ASM + includes -> KRCSTART-like .rel; link it + libs -> .MIK S-rec image + map).
3. Assembler must be two-pass, track relocatable expressions (error on illegal reloc ops), support all 6809 addressing (indexed auto inc/dec 1/2, indirect [], DP direct, extended, PC-rel, imm, inherent), register lists (PSHS/PULS order insensitive per rules), long/short branches auto, macros (NARG, params), conditionals, includes, SET/EQU, named/unnamed sections, BSCT (direct page 0-FF special), GLOBAL/EXTERN, listing with line numbers + symbols + pagination, -nh, date stamps, error/warning messages matching original text where possible.
4. Linker: support memory directives (0000-FFFF ranges, SECTION foo BSCT @ 0-FF, PSCT @ ..., MODULE, DEFINE sym=val), free/reserve/alloc lists, multiple user .rel + -l libs (search order), map output (section summary, symbols, unsatisfied refs, allocation), -v, entry point, checksums in S-rec, warnings for BSCT out of 0-FF or overlaps.
5. C compiler: The original is multi-pass (xc_1 pass1 parser to ICF, xc9_3 tree-to-6809-asm codegen emitting the special runtime calls like .LMUL .DADD .CTOI etc + .LINK for arg setup). Full faithful re-creation is a major undertaking (C front-end + 6809 cg + float/long support + the runtime lib). Phase it: (a) driver that matches CLI and chains a9+xld, (b) runtime headers, (c) optional: integrate an external 6809 C compiler (e.g. ack, gcc-6809 port, vbcc) that can be taught to emit asm compatible with our a9 syntax and the calling conv/runtime symbols, (d) later native passes if needed for exact match.
6. Runtime & conv: Replicate the special C entry (.CSTRT), stack in DSCT, .clrio init, the .LINK mechanism (A holds param byte count before call; special returns), IO via ACIA (but make targetable), the many compiler support symbols in rnlib9.
7. Testing: Use original .ASM as golden inputs. Use dosbox (or similar) + original EXEs on crafted tests to generate reference .rel/.map/.lst/.mik for comparison. Add roundtrip and link tests.
8. Build: Pure make, no exotic deps. Cross-build note: the *tools* run on mac/linux and target 6809 (no host execution of 6809 code unless emulator added).
9. When in doubt, match the error messages, copyright banners ("Copyright 1986, Wintek Corporation"), version strings, and behavior from strings in the EXEs.
10. Upload results (regular git push or PRs) to the indicated gitea instance. Keep history clean.
## Reverse Engineering Tips
- Use `strings WINTEK/*.EXE | cat` liberally (already shows Usage, versions, all errors, section names ASCT/BSCT/DSCT/PSCT, runtime syms .CTOD etc, directives).
- The .REL and .LIB *are text S-record files* (CRLF); parse them to understand object format (see docs/reloc-srecords.md once written).
- To observe original: `dosbox` (mount drive with .EXE + .ASM testcase; run `A9.EXE foo.asm -l foo.lst -o foo.rel`); capture outputs for spec.
- ndisasm or custom 16-bit x86 disassemblers on the .EXE (after MZ header skip) can reveal structure but high-level recreation > instruction-by-instruction port.
- Provided .ASM in WINTEK/ are small and excellent for validating sections + linking + C/asm interop.
- .DIR files show real SECTION/MODULE usage for multi-board memory maps.
## Legal/Status Note
WINTEK Corporation is long defunct. These recreations are for preservation, education, and enabling continued use/maintenance of 6809-based legacy systems and cross-development. No affiliation. All new code here is original work.
## Getting Started for Agents/Contributors
- Read this file fully.
- Explore WINTEK/ contents via read-only tools (ls, strings, hexdump, python parses) — **never edit**.
- `make` at root should eventually build bin/a9 bin/xld etc.
- Add tests early.
- Update docs/ with findings on formats.
- Before committing changes that affect output format or CLI, ensure compatibility tests (reference vs new) pass.
This file itself was created per the mission statement and should be kept up-to-date with any refined process.
(End of initial instructions)