disasm_with_labels.asm 155 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888
  1. ; z80dasm 1.1.3
  2. ; command line: z80dasm -l -s rom_strings.sym -g 0x0000 -a metric_cpu-s_mc8_v1.2_2716.bin
  3. ;##############################################################################
  4. ; METRIC 8 COMPUTER SYSTEM
  5. ; MONITOR/DEBUGGER REFERENCE MANUAL
  6. ;##############################################################################
  7. ;
  8. ; SYSTEM OVERVIEW:
  9. ; The Metric 8 is a sophisticated Z80-based computer system (circa 1980) with
  10. ; an advanced monitor/debugger ROM providing comprehensive development tools.
  11. ;
  12. ; HARDWARE SPECIFICATIONS:
  13. ; • CPU: Z80 running at 3.93216MHz (sophisticated timing), crystal at 19.6608MHz
  14. ; • ROM: 2 pcs of 2716 EPROM (2KB) containing monitor/debugger
  15. ; • RAM: 1KB base workspace + 64KB paged external memory
  16. ; • I/O: Z80 DMA controller, NEC µPD765A FDC, sophisticated peripherals
  17. ; • Memory Management: Advanced paging system with 8KB windows
  18. ;
  19. ; MONITOR COMMANDS:
  20. ; The monitor provides a '>' prompt and accepts single-letter commands
  21. ; followed by hexadecimal parameters. All addresses are 4-digit hex.
  22. ;
  23. ; MEMORY EXAMINATION COMMANDS:
  24. ; • M XXXX - Memory examine/modify starting at address XXXX
  25. ; Shows: ADDR: YY (where YY is current byte value)
  26. ; Enter new hex value or press Enter to skip
  27. ; Press 'Q' to quit back to monitor
  28. ;
  29. ; • D XXXX - Memory dump (hex display) starting at address XXXX
  30. ; Format: XXXX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX *................*
  31. ; Shows 32 bytes total (2 lines of 16 bytes each)
  32. ; Non-printable characters shown as '.' in ASCII area
  33. ;
  34. ; • C XXXX YYYY ZZZZ - Memory compare command
  35. ; Compares memory block [XXXX..YYYY] with block starting at ZZZZ
  36. ; Displays differences in format showing both addresses and values
  37. ; Only shows mismatched bytes, silent if blocks are identical
  38. ;
  39. ; MEMORY MODIFICATION COMMANDS:
  40. ; • F XXXX YYYY ZZ - Memory fill command
  41. ; Fills memory from XXXX to YYYY with byte value ZZ
  42. ; Uses efficient Z80 LDIR instruction for fast block operations
  43. ;
  44. ; DEBUGGING/BREAKPOINT COMMANDS:
  45. ; • B XXXX - Set breakpoint or execution counter
  46. ; Sets debugging parameters for program execution control
  47. ; Counter stored at memory location 0x011D0
  48. ;
  49. ; • G XXXX - Go/Execute program starting from address XXXX
  50. ; Transfers control to user program at specified address
  51. ; Restores all CPU registers and begins execution
  52. ; Supports breakpoint detection and interrupt handling
  53. ; Returns to monitor if breakpoint hit or program exits
  54. ;
  55. ; • I XXXX - Configure debug display parameters and modes
  56. ; Sets debugging display parameters stored at 0x11D6
  57. ; Used for display width, format, or visualization options
  58. ; Special case: "II" jumps to specialized display handler
  59. ;
  60. ; • J XXXX - Jump/Execute program (alternate Go command)
  61. ; Similar functionality to G command - executes program
  62. ; Parses hex address and transfers control to user program
  63. ; Supports same breakpoint and register restoration features
  64. ;
  65. ; • L - Load/Transfer data operations
  66. ; Data loading and transfer command for memory operations
  67. ; Supports loading data between memory locations
  68. ; May include external storage access capabilities
  69. ;
  70. ; SYSTEM DISPLAY COMMANDS:
  71. ; • R - Register display - Show CPU register contents
  72. ; Displays all Z80 registers and system status
  73. ; Shows AF, BC, DE, HL, IX, IY, SP, PC values
  74. ;
  75. ; ADDITIONAL COMMANDS:
  76. ; • Debug parameters can be configured via additional commands
  77. ; • Monitor supports commands: B,C,D,E,F,G,H,I,J,L,M,N,O,P,R,S,T,W,X
  78. ; • Use single letters followed by hex parameters as needed
  79. ; Values stored at 0x011D6 for various debugging modes
  80. ;
  81. ; SYSTEM MESSAGES:
  82. ; • "BREAK AT XXXX" - Displayed when execution breaks at address XXXX
  83. ; System returns to monitor prompt after breakpoints
  84. ;
  85. ; TECHNICAL FEATURES:
  86. ; • Sophisticated hex string parser supporting multi-parameter input
  87. ; • Intelligent input processing with space skipping and case conversion
  88. ; • Dual-purpose code/data sections for memory efficiency
  89. ; • Register state preservation and restoration
  90. ; • Interrupt mode 2 support with vector table
  91. ; • Advanced memory paging with 64KB external memory card
  92. ; • Professional error handling with graceful returns to command prompt
  93. ;
  94. ; CONTROL CHARACTERS:
  95. ; The monitor responds to standard terminal control characters for user control:
  96. ;
  97. ; • Ctrl+C (0x03 ETX) - Emergency System Reset/Break
  98. ; Immediately aborts current operation and returns to monitor
  99. ; Resets stack pointer and provides clean restart
  100. ; Similar to "break" functionality in modern terminals
  101. ; Works during any input operation or command execution
  102. ;
  103. ; • Ctrl+S (0x13 XOFF) - Pause/Resume Output Flow Control
  104. ; Temporarily pauses output display (standard XOFF)
  105. ; Press any key to resume output (XON behavior)
  106. ; Useful for stopping scrolling during long displays
  107. ; Standard terminal flow control implementation
  108. ;
  109. ; • Backspace (0x08) - Character Deletion During Input
  110. ; Deletes previous character from input buffer
  111. ; Echoes backspace-space-backspace sequence to terminal
  112. ; Provides visual feedback of character removal
  113. ; Standard line editing functionality
  114. ;
  115. ; • DEL (0x7F) - Alternative Character Deletion
  116. ; Functions identically to Backspace (0x08)
  117. ; Supports both common delete key encodings
  118. ; Maintains compatibility with different terminal types
  119. ;
  120. ; These control characters provide immediate user control over system operation
  121. ; and implement standard terminal behavior expected in professional systems.
  122. ;
  123. ; MEMORY MAP:
  124. ; • 0x0000-0x0FFF: ROM code space (4KB 2716 EPROM)
  125. ; • 0x1000-0x13FF: Base RAM workspace (1KB)
  126. ; • 0x1100: Keypress detection buffer (SIO interrupt stores received characters here)
  127. ; • 0x1101-0x1102: Character timing measurement storage (16-bit, for baud rate detection)
  128. ; • 0x1104: Console input buffer start
  129. ; • 0x1168: Input processing buffer (temporary storage)
  130. ; • 0x1169: Command input buffer (keyboard input processing)
  131. ; • 0x11F2: Console output buffer pointer
  132. ; • 0x11D0: Execution counter/breakpoint parameter
  133. ; • 0x11D6: Debug mode parameter
  134. ; • 0x1300-0x1320: Interrupt vector table (IM2 mode)
  135. ; • 0x1400: Stack pointer start (grows downward)
  136. ; • 0x2000-0x3FFF: Paged RAM window (8KB, bank-switched)
  137. ;
  138. ; SYSTEM CONFIGURATION MEMORY LOCATIONS:
  139. ; Hardware Configuration Block (0x1200-0x1227):
  140. ; • 0x1200: 16-bit DMA/buffer address pointer for disk operations
  141. ; • 0x1202: Current drive number (0-3) for multi-drive operations
  142. ; • 0x1203: Drive status/ready flags (0x01 = drive ready bit set)
  143. ; • 0x1205: 16-bit status counter A (initialized to 0xFFFF = ready state)
  144. ; • 0x1207: 16-bit status counter B (initialized to 0xFFFF = ready state)
  145. ; • 0x1209: Sector size parameter (0x24 = 36 decimal bytes per sector)
  146. ; • 0x120A: I/O port configuration byte (0xF0 = upper nibble port settings)
  147. ; • 0x120B: Drive count/operation mode (0x03 = 3 drives or operational mode 3)
  148. ; • 0x120C: Additional FDC configuration and status data
  149. ;
  150. ; FDC Command Parameter Block (0x1213-0x1215):
  151. ; • 0x1213: NEC µPD765A command byte (0x03 = READ DATA command)
  152. ; • 0x1214: Drive/Head selection and DMA control (combined from 0x120B + 0x120A)
  153. ; • 0x1215: Track/Sector/Size parameters (combined from 0x1209 + 0x1204)
  154. ;
  155. ; FDC Drive Control Block (0x1225-0x1227):
  156. ; • 0x1225: Drive parameter/selector register
  157. ; • 0x1226: Drive control flags and status
  158. ; • 0x1227: Drive enable/operation status (1=enabled, 0=disabled)
  159. ;
  160. ; Buffer Management Block (0x1244-0x1248):
  161. ; • 0x1244: 16-bit buffer pointer (low bank) - cleared to 0x0000 at startup
  162. ; • 0x1246: 16-bit buffer pointer (mid bank) - cleared to 0x0000 at startup
  163. ; • 0x1248: 16-bit buffer pointer (high bank) - cleared to 0x0000 at startup
  164. ;
  165. ; FDC Operation Status Block (0x124A-0x124B):
  166. ; • 0x124A: FDC operation completion status flag (0=pending, non-zero=completed)
  167. ; • 0x124B: Additional FDC status and control parameters
  168. ;
  169. ; Hardware Interface Configuration:
  170. ; • 0x1242: SIO serial port configuration pointer (points to table at 0x0507)
  171. ; • 0x1243: Serial port base address configuration byte (used by I/O routines)
  172. ;
  173. ; Monitor/Debugger Variables:
  174. ; • 0x011CF: Saved byte backup (breakpoint restoration storage)
  175. ; • 0x011D0: Breakpoint/execution counter for debugging
  176. ; • 0x011D1: Active breakpoint memory location (current breakpoint address)
  177. ; • 0x011D3: Original byte backup (saved from breakpoint location for restoration)
  178. ; • 0x011D4: Debug parameters and execution status storage
  179. ; • 0x011D6: Debug display parameters and mode settings
  180. ; • 0x011D7: Interrupt vector high byte for IM2 mode
  181. ; • 0x011D8: Complete register save area (AF,BC,DE,HL,IX,IY,SP) for user program execution
  182. ; • 0x011EA: Program execution start address (G/J commands)
  183. ; • 0x011EC: User stack pointer backup storage
  184. ; • 0x011EE: Saved debug parameters (temporary storage during execution)
  185. ; • 0x011F0: Current breakpoint address
  186. ; • 0x011F2: Console input/output buffer pointer (0x1104 default)
  187. ; • 0x011F4: Input buffer current position pointer
  188. ; • 0x011F6: Current prompt character (0x2A='*', 0x3E='>', 0x20=' ')
  189. ;
  190. ; Memory Paging System Variables:
  191. ; • 0x1326: Breakpoint address backup (for memory paging operations)
  192. ;
  193. ; This monitor/debugger represents advanced 1980s software engineering with
  194. ; modular design, efficient algorithms, and professional user interface.
  195. ;
  196. ;##############################################################################
  197. ;Memory architecture:
  198. ;• 0x0000-0x0FFF: ROM code space (4KB 2716 EPROM)
  199. ;• 0x1000-0x13FF: Base RAM workspace (1KB)
  200. ;• 0x1242 SIO port config 0x0507
  201. ;• 0x1300-0x1320: Interrupt vector table (IM2 mode) from 0x024f
  202. ;• 0x1400: Stack pointer start (grows downward toward RAM)
  203. ;• 0x2000-0x3FFF: Paged RAM window (8KB, bank-switched)
  204. ;• Extended RAM: 8x 4164 chips = 8 pages × 8KB = 64KB total
  205. ;• Page switching: Hardware register selects active 8KB bank
  206. ;• Effective memory: 1KB base + 8KB paged window + ROM
  207. ;==============================================================================
  208. ; MEMORY PAGING SYSTEM DOCUMENTATION
  209. ;==============================================================================
  210. ; The Metric 8 system implements a sophisticated memory paging mechanism to
  211. ; access a 64KB external memory card through an 8KB window at 0x2000-0x3FFF.
  212. ;
  213. ; CLEVER PORT SHARING STRATEGY:
  214. ; The system exploits the fact that ROM code only uses CTC Channels 1 and 2,
  215. ; leaving Channels 0 and 3 unused. Memory paging "hijacks" these unused ports
  216. ; without conflicting with essential CTC timing functions.
  217. ;
  218. ; PORT ALLOCATION STRATEGY:
  219. ; • PORT 0x000 (CTC Channel 0): UNUSED by ROM → Available for memory control
  220. ; • PORT 0x001 (CTC Channel 1): Used by ROM for timing functions
  221. ; • PORT 0x002 (CTC Channel 2): Used by ROM for SIO baud rate generation
  222. ; • PORT 0x003 (CTC Channel 3): UNUSED by ROM → Available for page selection
  223. ;
  224. ; MEMORY ARCHITECTURE:
  225. ; • Base System RAM: 1KB at 0x1000-0x13FF (always accessible)
  226. ; • Paged Memory Window: 8KB at 0x2000-0x3FFF (bank-switched via ports 0x000/0x003)
  227. ; • External Memory Card: 64KB organized as 8 pages × 8KB each
  228. ; • Total Addressable Memory: 1KB + 64KB = 65KB (plus ROM)
  229. ;
  230. ; CONTROL PORTS:
  231. ; • PORT 0x000: Memory Management Control Register (hijacks unused CTC Channel 0)
  232. ; - Bit 5 (0x20): Enable paging system
  233. ; - Controls overall memory management subsystem
  234. ;
  235. ; • PORT 0x003: Page Selection Register (hijacks unused CTC Channel 3)
  236. ; - 3-bit page number (0-7) selects which 8KB page appears at 0x2000-0x3FFF
  237. ; - Sequential writes may configure additional memory features
  238. ;
  239. ; COEXISTENCE WITH STANDARD HARDWARE:
  240. ; This design allows memory paging to work with standard SIO/CTC configuration:
  241. ; • SIO: ports 0x04-0x07 (unchanged)
  242. ; • CTC Channels 1&2: ports 0x01-0x02 (unchanged, ROM functions preserved)
  243. ; • Memory Management: ports 0x000 & 0x003 (reuse of unused CTC channels)
  244. ; • Result: 64KB memory card works with standard I/O configuration!
  245. ;
  246. ; OBSERVED PORT OPERATIONS:
  247. ; 1. out (000h),20h ; Enable memory management (bit 5 = paging enable)
  248. ; 2. out (003h),87h ; Select page 7 with setup flags (page + control bits)
  249. ; 3. out (003h),01h ; Select page 1 for actual operation
  250. ; 4. out (003h),03h ; Select page 3 (restore/cleanup page)
  251. ;==============================================================================
  252. I/O subsystem:
  253. • SIO 8440 chip for serial console (Pin 15 TxDA, Pin 17 RxDA) port 05-07
  254. • MC1488/MC1489 RS-232 drivers (require ±12V)
  255. • CTC for baud rate generation and timing port 00-04
  256. • Z80 DMA controller for high-speed floppy transfers - port 0xF3
  257. • FDC NEC µPD765A for 8-inch floppy drives (77 tracks, 26 sectors/track):
  258. - 0xF0: FDC Main Status Register (read)
  259. - 0xF1: FDC Data Register (read/write via register C)
  260. - 0xF4: FDC Data Register (direct access)
  261. - 0xF6: FDC Command/Status Register
  262. - 0xFC: Additional FDC control
  263. • I/O ports accessed via register C
  264. org 00000h
  265. l0000h:
  266. jr l000eh ;0000 ; Boot entry: jump to initialization routine
  267. jp l00fdh ;0002 ; Main system routine (post-init)
  268. jp l0dabh ;0005 ; Error/exception handler or alternate entry
  269. jp l0294h ;0008 ; Memory test or hardware check
  270. jp l0de0h ;000b ; Interrupt/Z80 DMA handler or alternate
  271. l000eh:
  272. ld sp,01400h ;000e ; Set stack pointer to top of 1K SRAM (boot RAM)
  273. ld hl,01000h ;0011 ; HL points to start of SRAM
  274. ld de,01001h ;0014 ; DE points to next byte in SRAM
  275. ld bc,003ffh ;0017 ; BC = 1023 bytes (1K - 1)
  276. ld (hl),000h ;001a ; Zero first byte of SRAM
  277. ldir ;001c ; Zero entire 1K SRAM (boot RAM clear)
  278. ld a,013h ;001e ; Load interrupt vector base (IM2 table address)
  279. l0020h:
  280. ld i,a ;0020 ; Set Z80 interrupt vector register (IM2 base)
  281. im 2 ;0022 ; Set interrupt mode 2 (for SIO, Z80 DMA, FDC)
  282. call hw_init_01ach ;0024 ; Call hardware init (Z80 DMA, SIO, FDC setup)
  283. ld hl,l0507h ;0027 ; HL = pointer to boot string or table
  284. l002ah:
  285. jr nz,l002fh ;002a Fail hw test if it find some form of other thing then sio on port 04 06
  286. l002ch:
  287. ld hl,0c3c1h ;002c This should be an alternate SIO card
  288. l002fh:
  289. ld (01242h),hl ;002f ; Save HL (alternate pointer) at 01242h for later use
  290. ld a,l ;0032 ; Load low byte of HL into A (may be used for error handling)
  291. ld hl,linterruptvector_table ;0033 ; Set HL to interrupt vector table address
  292. jr l003bh ;0036 ; Continue with boot sequence (alternate path)
  293. jp l07bch ;0038 ; Jump to CP/M loader or main OS entry if needed
  294. l003bh:
  295. ld de,01300h ;003b ; DE = destination Interruptvector addres
  296. ld bc,l0020h ;003e ; BC = length to copy (from l0020h)
  297. ldir ;0041 ; Block copy: transfer BC bytes from HL to DE
  298. ld hl,setupsio_table ;0043 ; HL = pointer to primary SIO configuration table
  299. cp 007h ;0046 ; Compare A to 7 (hardware detection/boot status)
  300. jr z,l004dh ;0048 ; If A == 7, use primary SIO card (normal case)
  301. ld hl,00214h ;004a ; If not, use secondary SIO card at high port addresses (0xC0+)
  302. l004dh:
  303. call setup_peripherals_01a1h ;004d ; Initialize peripherals (SIO)
  304. push hl ;0050 ; Save HL (pointer or error address)
  305. ld hl,01100h ;0051 ; HL = system buffer or stack
  306. sub a ;0054 ; Clear A (set to zero)
  307. ld (hl),a ;0055 ; Zero system buffer
  308. ei ;0056 Enable interrupts ; ROM code space
  309. l0057h:
  310. or (hl) ;0057 OR accumulator A with memory contents at address HL ; ROM code space,wait for keypress
  311. jr z,l0057h ;0058 Jump relative if Z to 0x0057 ; ROM code space
  312. di ;005a
  313. call sub_0149h ;005b
  314. ld a,030h ;005e //WR= error reset
  315. out (c),a ;0060
  316. jr l0069h ;0062
  317. rst 38h ;0064
  318. rst 38h ;0065
  319. jp l07b3h ;0066 ; Jump to interrupt/error handler - displays "BREAK AT " message
  320. l0069h:
  321. call sub_015dh ;0069 ; Call automatic baud rate detection and SIO configuration
  322. ld a,(01242h) ;006c
  323. cp 007h ;006f
  324. ld c,002h ;0071 ; Set C to 0x02 (standard CTC port)
  325. jr z,l0077h ;0073
  326. ld c,0c4h ;0075 ; Set C to 0xC4 (alternate CTC port)
  327. l0077h:
  328. ;==============================================================================
  329. ; ADAPTIVE CTC CHANNEL INITIALIZATION (0x0079)
  330. ;==============================================================================
  331. ; Purpose: Configure CTC channel with automatic port selection and dual-phase setup
  332. ; Entry: sub_0079h - Called during hardware initialization sequence
  333. ;
  334. ; Input Registers:
  335. ; - A: 0x47 (CTC control word for initial configuration)
  336. ; - B: 0x02 (time constant value)
  337. ; - C: CTC port address (0x02 for standard, 0xC4 for alternate hardware)
  338. ;
  339. ; CTC Control Word 0x47 Breakdown:
  340. ; - Bit 7: 0 = Interrupt enable
  341. ; - Bit 6: 1 = Counter mode (not timer mode)
  342. ; - Bit 5: 0 = Prescaler ÷16
  343. ; - Bit 4: 0 = Positive edge trigger
  344. ; - Bit 3: 1 = Time constant follows command
  345. ; - Bit 2: 1 = Software reset active
  346. ; - Bits 1-0: 11 = Control word identifier
  347. ;
  348. ; Two-Phase Operation:
  349. ; PHASE 1 (0x0079-0x007D): Send control word and time constant
  350. ; PHASE 2 (0x0087-0x008D): Send final control word (0x47) and divisor (0x02)
  351. ;
  352. ; Port Selection Logic:
  353. ; - Compares value from 0x1242 with 0x07 (SIO Channel B Control port)
  354. ; - If 0x1242 = 0x07: Uses standard mapping, C = 0x02 (CTC Channel 2)
  355. ; - If 0x1242 ≠ 0x07: Uses alternate mapping, C = 0xC4 (alternate CTC port)
  356. ;
  357. ; Hardware Compatibility:
  358. ; - Standard: SIO at 0x04-0x07, CTC at 0x00-0x03, configures CTC Channel 2
  359. ; - Alternate: Different SIO ports, CTC at 0xC0-0xC7, configures alternate CTC
  360. ;
  361. ; Time Constant 0x02:
  362. ; - Divides input clock by 2
  363. ; - Used for SIO clock generation (baud rate timing)
  364. ; - Combined with ÷16 prescaler = total division of 32
  365. ;
  366. ; Integration: Part of larger peripheral initialization sequence
  367. ; Called after SIO detection and before final hardware setup
  368. ;==============================================================================
  369. ld a,047h ;0077
  370. sub_0079h:
  371. out (c),a ;0079 Send CTC control word (0x47) to selected port
  372. out (c),b ;007b Send time constant (0x02) to CTC channel
  373. ld a,c ;007d Copy port address to A for comparison
  374. cp 0xc4 ;007f Compare with 0xC4 (alternate hardware marker)
  375. jr z,0x0085 ;0080 If alternate hardware, jump to port adjustment
  376. dec c ;0082 Standard hardware: decrement to previous channel
  377. jr 0x0087 ;0083 Continue to phase 2 initialization
  378. ; Alternate hardware port adjustment
  379. inc c ;0085 Increment port (0xC4 → 0xC5)
  380. inc c ;0086 Increment port (0xC5 → 0xC6)
  381. ; Phase 2: Final CTC configuration
  382. l0087h:
  383. ld a,047h ;0087 Reload CTC control word
  384. out (c),a ;0089 Send control word to final port
  385. ld a,002h ;008b Load final time constant/divisor
  386. out (c),a ;008d Send final configuration to CTC
  387. ;==============================================================================
  388. ; PHASE 3: COMPLETE HARDWARE INITIALIZATION AND SIO TESTING (0x008F+)
  389. ;==============================================================================
  390. ; After CTC setup, complete remaining peripheral initialization and test SIO
  391. ;==============================================================================
  392. ; Continue table-driven peripheral initialization
  393. pop hl ;008f Restore HL from stack (pointer to init table)
  394. inc hl ;0090 Advance to next table entry
  395. call setup_peripherals_01a1h ;0091 Continue table-driven peripheral setup
  396. ; Load test parameters and call diagnostic routine
  397. ld bc,058c0h ;0094 Load test parameters: B=0x58 (count), C=0xC0 (port/data)
  398. call sub_0f0ah ;0097 Call diagnostic/test routine with parameters
  399. ; SIO port preparation and buffer clearing
  400. call sub_0149h ;009a Get SIO port configuration (loads C with SIO control port)
  401. dec c ;009d Convert to SIO data port (control port - 1)
  402. in a,(c) ;009e Read SIO data port (clear any pending RX data)
  403. in a,(c) ;00a0 Read SIO data port again (ensure buffer clear)
  404. in a,(c) ;00a2 Third read to fully flush RX buffer
  405. ; Test SIO transmission capability
  406. call sub_0121h ;00a4 Send test character via SIO (verify TX works)
  407. ; Second SIO buffer clearing after transmission test
  408. dec c ;00a7 Set C back to SIO data port
  409. in a,(c) ;00a8 Read SIO data port (clear any echo/response data)
  410. in a,(c) ;00aa Read SIO data port again (ensure clean state)
  411. in a,(c) ;00ac Third read to fully flush post-transmission buffer
  412. ; Hardware initialization complete - enable interrupts and continue
  413. ei ;00ae Enable interrupts (hardware now fully operational)
  414. ; Print startup message after successful hardware initialization
  415. ld hl,l00e5h ;00af Load pointer to "Self diagnostics ..." message
  416. call l00fdh ;00b2 Print the startup message via serial port
  417. ; Run comprehensive system diagnostics
  418. call sub_0e89h ;00b5 Execute full system diagnostic suite
  419. ;==============================================================================
  420. ; SYSTEM CONFIGURATION MEMORY INITIALIZATION (0x00B8-0x00DF)
  421. ;==============================================================================
  422. ; Purpose: Initialize critical system configuration variables after diagnostics
  423. ; This section sets up default values for system operation parameters
  424. ;==============================================================================
  425. ld a,001h ;00b8 Load configuration value 0x01
  426. ld (01203h),a ;00ba Store at 0x1203: Drive/status flags (bit 0 = drive ready)
  427. ld a,024h ;00bd Load configuration value 0x24 (36 decimal)
  428. ld (01209h),a ;00bf Store at 0x1209: Sector size parameter (36 bytes/sector?)
  429. ld a,0f0h ;00c2 Load configuration value 0xF0 (240 decimal)
  430. ld (0120ah),a ;00c4 Store at 0x120A: Port/channel configuration byte
  431. ld a,003h ;00c7 Load configuration value 0x03
  432. ld (0120bh),a ;00c9 Store at 0x120B: Drive count or operation mode
  433. ld hl,l0000h ;00cc Load 16-bit value 0x0000
  434. ld (01244h),hl ;00cf Store at 0x1244: Clear 16-bit buffer/pointer (low)
  435. ld (01246h),hl ;00d2 Store at 0x1246: Clear 16-bit buffer/pointer (mid)
  436. ld (01248h),hl ;00d5 Store at 0x1248: Clear 16-bit buffer/pointer (high)
  437. dec hl ;00d8 Decrement HL to 0xFFFF
  438. ld (01205h),hl ;00d9 Store at 0x1205: Initialize 16-bit counter/status (0xFFFF)
  439. ld (01207h),hl ;00dc Store at 0x1207: Initialize 16-bit counter/status (0xFFFF)
  440. call sub_0acbh ;00df Call additional hardware setup routine
  441. jp l0294h ;00e2 Jump to main system initialization
  442. ;==============================================================================
  443. ; STARTUP MESSAGE: "Self diagnostics ..."
  444. ;==============================================================================
  445. ; Displayed after successful hardware initialization to indicate system status
  446. ;==============================================================================
  447. l00e5h:
  448. defb "Self diagnostics ..." ;00e5-00f8 Startup message text
  449. defb 0xda ;00f9 Special character (Ú or box drawing)
  450. defb 0x0d ;00fa Carriage return
  451. defb 0x0a ;00fb Line feed
  452. defb 0x00 ;00fc Null terminator
  453. ;==============================================================================
  454. ; STRING OUTPUT ROUTINES - Console Display Functions
  455. ;==============================================================================
  456. ; Two different string output routines for different output methods
  457. ;==============================================================================
  458. ;==============================================================================
  459. ; PRINT STRING ROUTINE (0x00FD) - Standard Console Output
  460. ;==============================================================================
  461. ; Purpose: Print null-terminated string to console via standard output
  462. ; Input: HL = pointer to null-terminated string
  463. ; Output: String displayed on console via sub_0127h (console output with wait)
  464. ; Operation: Loops through string characters until null terminator found
  465. ; Used by: Main system messages, prompts, and user interface text
  466. ;==============================================================================
  467. l00fdh: ; Standard Print String Routine
  468. ld a,(hl) ;00fd Load character from string
  469. and a ;00fe Test for null terminator (sets Z flag if A=0)
  470. ret z ;00ff Return if end of string reached
  471. ld c,a ;0100 Move character to C register for output
  472. call sub_0127h ;0101 Call console output with wait (transmitter ready check)
  473. inc hl ;0104 Point to next character in string
  474. jr l00fdh ;0105 Loop back for next character
  475. ;==============================================================================
  476. ; ALTERNATE PRINT STRING ROUTINE (0x0107) - Direct Serial Output
  477. ;==============================================================================
  478. ; Purpose: Print null-terminated string via direct serial output method
  479. ; Input: HL = pointer to null-terminated string
  480. ; Output: String sent via sub_0111h (direct serial output)
  481. ; Operation: Similar to standard routine but uses different output method
  482. ; Used by: Low-level system messages or when console output unavailable
  483. ;==============================================================================
  484. l0107h: ; Alternate Print String Routine
  485. ld a,(hl) ;0107 Load character from string
  486. and a ;0108 Test for null terminator (sets Z flag if A=0)
  487. ret z ;0109 Return if end of string reached
  488. ld c,a ;010a Move character to C register for output
  489. call sub_0111h ;010b Call direct serial output routine
  490. inc hl ;010e Point to next character in string
  491. jr l0107h ;010f Loop back for next character
  492. ;==============================================================================
  493. ; DIRECT SERIAL OUTPUT ROUTINE (0x0111)
  494. ;==============================================================================
  495. ; Purpose: Output character directly to serial port without full handshaking
  496. ; Input: C = character to output
  497. ; Operation: Calls sub_0121h then jumps to l012ah for immediate transmission
  498. ;==============================================================================
  499. sub_0111h: ; Direct Serial Output
  500. call sub_0121h ;0111 Set up serial port configuration
  501. jr l012ah ;0114 Jump to immediate output routine
  502. ;==============================================================================
  503. ; SERIAL INPUT WITH WAIT (0x0116)
  504. ;==============================================================================
  505. ; Purpose: Wait for and read character from serial input
  506. ; Output: C = received character
  507. ; Operation: Loops until character available, then returns it
  508. ;==============================================================================
  509. l0116h: ; Serial Input with Wait
  510. call sub_011ch ;0116 Check for input character available
  511. jr z,l0116h ;0119 Loop if no character available (Z flag set)
  512. ret ;011b Return with character in C register
  513. ;==============================================================================
  514. ; CHECK SERIAL INPUT READY (0x011C)
  515. ;==============================================================================
  516. ; Purpose: Check if serial input character is available (non-blocking)
  517. ; Output: Z flag = set if no character, clear if character available
  518. ; C = character (if available)
  519. ; Operation: Tests serial port status and reads character if present
  520. ;==============================================================================
  521. sub_011ch: ; Check Serial Input Ready
  522. call sub_0121h ;011c Set up serial port configuration
  523. jr l013dh ;011f Jump to input status check routine
  524. ;==============================================================================
  525. ; SERIAL PORT SETUP ROUTINE (0x0121)
  526. ;==============================================================================
  527. ; Purpose: Set up serial port configuration for I/O operations
  528. ; Input: C = character to be output (preserved in B register)
  529. ; Output: C = serial port address from configuration byte at 0x1243
  530. ; Operation: Saves character, loads port address, prepares for I/O
  531. ;==============================================================================
  532. sub_0121h: ; Serial Port Setup
  533. ld b,c ;0121 Save character/input parameter in B register
  534. ld a,(01243h) ;0122 Load serial port base address from config byte
  535. l0125h:
  536. ld c,a ;0125 Move port address to C register
  537. ret ;0126 Return with port address in C, character in B
  538. ;ROUTINE: Console Output with Wait (0x0127)
  539. ;--------------------------------------------
  540. ;Purpose: Outputs character and waits for transmitter ready
  541. sub_0127h:
  542. call sub_0149h ;0127 Get serial port base address
  543. l012ah:
  544. in a,(c) ;012a Read status register
  545. bit 2,a ;012c Test transmitter ready bit
  546. jr z,l012ah ;012e Wait if not ready
  547. dec c ;0130 Point to data register
  548. out (c),b ;0131 Output character
  549. ret ;0133 Return to caller
  550. l0134h:
  551. call sub_013ah ;0134 Check for input character available
  552. jr z,l0134h ;0137 Loop if no character available (Z flag set)
  553. ret ;0139 Return with character received
  554. ;==============================================================================
  555. ; CHECK SERIAL INPUT READY (0x013A) - Non-blocking Input Status Check
  556. ;==============================================================================
  557. ; Purpose: Check if serial input character is available without blocking
  558. ; Output: Z flag = set if no character, clear if character available
  559. ; C = character data (if available), A = character (7-bit masked)
  560. ; Operation: Tests serial port RX status bit, reads and processes character
  561. ;==============================================================================
  562. sub_013ah: ; Check Serial Input Ready (Non-blocking)
  563. call sub_0149h ;013a Get serial port base address in C register
  564. l013dh: ; Input status check entry point
  565. in a,(c) ;013d Read status register from serial port
  566. bit 0,a ;013f Test bit 0 (receiver ready/character available)
  567. ret z ;0141 Return with Z flag set if no character available
  568. dec c ;0142 Decrement C to point to data register (status+1=data)
  569. in a,(c) ;0143 Read character from serial port data register
  570. and 07fh ;0145 Mask to 7 bits (remove parity/high bit)
  571. sub_0147h: ; Character processing entry point
  572. ld c,a ;0147 Store character in C register for caller
  573. ret ;0148 Return with character in both A and C registers
  574. ;==============================================================================
  575. ; GET SERIAL PORT BASE ADDRESS (0x0149) - Port Configuration Setup
  576. ;==============================================================================
  577. ; Purpose: Load serial port base address from configuration and set up registers
  578. ; Input: C = current value (preserved in B)
  579. ; Output: C = serial port base address, B = saved previous C value
  580. ; Operation: Loads port address from config pointer at 0x1242
  581. ;==============================================================================
  582. sub_0149h: ; Get Serial Port Base Address
  583. ld b,c ;0149 Save current C register value in B
  584. ld a,(01242h) ;014a Load serial port configuration from memory
  585. ld c,a ;014d Set C to serial port base address
  586. ret ;014e Return with port address in C, old value in B
  587. ;==============================================================================
  588. ; ECHO/BRIDGE MODE (0x014F) - Bidirectional Serial Bridge
  589. ;==============================================================================
  590. ; Purpose: Implement echo or serial bridge functionality between ports
  591. ; Operation: Infinite loop that transfers data bidirectionally
  592. ; Used for: Terminal echo, serial port bridging, or communication testing
  593. ;==============================================================================
  594. l014fh: ; Echo/Bridge Mode Entry Point
  595. call sub_013ah ;014f Check for input character from serial port
  596. call nz,sub_0111h ;0152 If character received, output it (echo/bridge)
  597. call sub_011ch ;0155 Check for input from alternate source
  598. call nz,sub_0127h ;0158 If character received, output via console
  599. jr l014fh ;015b Loop continuously (infinite bridge mode)
  600. ;==============================================================================
  601. ; AUTOMATIC BAUD RATE CALCULATOR AND SIO CONFIGURATOR (0x015D)
  602. ;==============================================================================
  603. ; Purpose: Dynamically calculate and configure SIO baud rate based on timing measurements
  604. ; Entry: sub_015dh - Called after serial character timing measurements
  605. ;
  606. ; Input Values:
  607. ; - 0x1100: 8-bit timing measurement (character bit timing)
  608. ; - 0x1101-0x1102: 16-bit timing measurement (character/timeout count)
  609. ;
  610. ; Algorithm:
  611. ; 1. SCALING PHASE:
  612. ; - Loads 16-bit timing value from 0x1101-0x1102 into HL
  613. ; - Multiplies HL by 8 (three left shifts) to scale timing value
  614. ; - Loads 8-bit measurement from 0x1100 into A
  615. ;
  616. ; 2. MAGNITUDE DETECTION:
  617. ; - Performs arithmetic right shifts on A while incrementing HL
  618. ; - Counts significant bits to determine timing magnitude
  619. ; - Stops when carry flag set (found first '1' bit)
  620. ; - Result: BC = scaled timing value for table lookup
  621. ;
  622. ; 3. TABLE LOOKUP:
  623. ; - Searches baud rate table at l0183h (contains standard timing values)
  624. ; - Each table entry: 16-bit timing threshold + 8-bit SIO divisor value
  625. ; - Finds best matching standard baud rate for measured timing
  626. ; - Uses binary search approach with carry flag comparison
  627. ;
  628. ; 4. SIO CONFIGURATION:
  629. ; - Returns A = 0x44 (SIO WR4 register value: 1 stop bit, 16x clock)
  630. ; - Returns B = clock divisor value from table
  631. ;
  632. ; Table Format (l0183h):
  633. ; Each 3-byte entry: [Low_Threshold] [High_Threshold] [SIO_Divisor]
  634. ; - Thresholds define timing ranges for standard baud rates
  635. ; - SIO_Divisor used in WR4 register for clock generation
  636. ;
  637. ; Auto-Detection Process:
  638. ; - System measures actual bit timing from incoming serial data
  639. ; - This routine converts measurements to appropriate SIO clock settings
  640. ; - Enables automatic baud rate detection without manual configuration
  641. ;
  642. ; Applications:
  643. ; - Auto-configure terminal connections
  644. ; - Adapt to different computer baud rates automatically
  645. ; - Robust serial communication without preset requirements
  646. ;
  647. ; Returns: A = 0x44 (WR4 value), B = clock divisor, timing configured
  648. ; Notes: Sophisticated floating-point-like integer math for 1980s hardware
  649. ;==============================================================================
  650. sub_015dh: ; Automatic Baud Rate Calculator Entry Point
  651. ld hl,(01101h) ;015d Load 16-bit timing measurement from memory
  652. ld a,(01100h) ;0160 Load 8-bit character timing measurement
  653. add hl,hl ;0163 Multiply HL by 8 (scale timing value)
  654. add hl,hl ;0164 Left shift 1: HL = HL * 2
  655. add hl,hl ;0165 Left shift 2: HL = HL * 4, total = original * 8
  656. l0166h: ; Magnitude detection loop
  657. sra a ;0166 Arithmetic right shift A (find first significant bit)
  658. inc hl ;0168 Increment HL counter for each bit shift
  659. jr nc,l0166h ;0169 Continue until carry set (found first '1' bit)
  660. ld c,l ;016b Store low byte of scaled timing in C
  661. ld b,h ;016c Store high byte of scaled timing in B (BC = timing)
  662. ld hl,l0183h ;016d Point to baud rate lookup table start
  663. sub a ;0170 Clear A register (will count table entries)
  664. l0171h: ; Table search loop
  665. inc a ;0171 Increment table entry counter
  666. ld e,(hl) ;0172 Load low byte of timing threshold from table
  667. inc hl ;0173 Move to next byte in table
  668. ld d,(hl) ;0174 Load high byte of timing threshold (DE = threshold)
  669. inc hl ;0175 Move to next table entry
  670. ex de,hl ;0176 Exchange: HL = threshold, DE = table pointer
  671. and a ;0177 Clear carry flag for subtraction
  672. sbc hl,bc ;0178 Compare threshold with measured timing (HL - BC)
  673. ex de,hl ;017a Restore: HL = table pointer, DE = difference
  674. inc hl ;017b Move to divisor byte in current table entry
  675. jr c,l0171h ;017c If threshold < timing, try next table entry
  676. dec hl ;017e Back up to divisor byte of matching entry
  677. ld b,(hl) ;017f Load SIO clock divisor value into B register
  678. ld a,044h ;0180 Load SIO WR4 register value (1 stop bit, 16x clock)
  679. ret ;0182 Return with A = WR4 value, B = clock divisor
  680. ;==============================================================================
  681. ; BAUD RATE DETECTION TABLE (0x0183-0x01A0)
  682. ;==============================================================================
  683. ; Format: Each 3-byte entry contains [Low_Limit] [High_Limit] [SIO_Divisor]
  684. ; Used by automatic baud rate detection routine at sub_015dh
  685. ; Entries ordered from fastest to slowest baud rates for binary search
  686. ;==============================================================================
  687. l0183h:
  688. ; Entry 1: High speed range
  689. defb 0x10, 0x01, 0x2C ;0183-0185 ; Limits: 0x0110-0x012C, Divisor: 0x2C (44)
  690. ; Entry 2:
  691. defb 0x5C, 0x00, 0x04 ;0186-0188 ; Limits: 0x005C-0x0004, Divisor: 0x04 (4)
  692. ; Entry 3:
  693. defb 0xBC, 0x00, 0x08 ;0189-018B ; Limits: 0x00BC-0x0008, Divisor: 0x08 (8)
  694. ; Entry 4:
  695. defb 0x7C, 0x01, 0x10 ;018C-018E ; Limits: 0x017C-0x0110, Divisor: 0x10 (16)
  696. ; Entry 5:
  697. defb 0xFC, 0x02, 0x20 ;018F-0191 ; Limits: 0x02FC-0x0220, Divisor: 0x20 (32)
  698. ; Entry 6:
  699. defb 0x05, 0x40, 0x70 ;0192-0194 ; Limits: 0x4005-0x7040, Divisor: 0x70 (112)
  700. ; Entry 7:
  701. defb 0x09, 0x80, 0x70 ;0195-0197 ; Limits: 0x8009-0x7080, Divisor: 0x70 (112)
  702. ; Entry 8:
  703. defb 0x0C, 0xA0, 0xFF ;0198-019A ; Limits: 0xA00C-0xFFA0, Divisor: 0xFF (255)
  704. ; Entry 9: Low speed range
  705. defb 0xFF, 0xFF, 0x00 ;019B-019D ; Limits: 0xFFFF-0x00FF, Divisor: 0x00 (0)
  706. ; Table terminator
  707. defb 0xFF, 0xFF ;019E-019F ; End marker
  708. defb 0x00 ;01A0 ; Padding/terminator
  709. setup_peripherals_01a1h:
  710. ld a,(hl) ;01a1 ; Load count from table (A = number of bytes to output)
  711. and a ;01a2 ; Test if count is zero
  712. ret z ;01a3 ; If zero, end of table, return
  713. ld b,a ;01a4 ; Set B = count
  714. inc hl ;01a5 ; Move to next table entry
  715. ld c,(hl) ;01a6 ; Load port number from table
  716. inc hl ;01a7 ; Move to data bytes
  717. otir ;01a8 ; Output B bytes from (HL) to port C, increment HL each time
  718. jr setup_peripherals_01a1h ;01aa ; Repeat for next table entry
  719. hw_init_01ach: ;Probably check if there is a perifle that can be read and write to which is not a SIO
  720. ld hl,initsio_l01d9h ;01ac ; HL = pointer to hardware register or test value
  721. call setup_peripherals_01a1h ;01af ; Call additional hardware init routine
  722. ld c,004h ;01b2 ; Set port C = 0x04 (SIO A data port)
  723. ld a,055h ;01b4 ; Load test pattern 0x55
  724. call check_peri_01d2h ;01b6 ; Output 0x55 to port 0x04 (SIO A data port)
  725. ret nz ;01b9 ; If error, return
  726. ld a,0aah ;01ba ; Load test pattern 0xAA
  727. call check_peri_01d2h ;01bc ; Output 0xAA to port 0x04 (SIO A data port)
  728. ret nz ;01bf ; If error, return
  729. ld a,055h ;01c0 ; Load test pattern 0x55
  730. ld c,006h ;01c2 ; Set port C = 0x06 (SIO B data port)
  731. call check_peri_01d2h ;01c4 ; Output 0x55 to port 0x06
  732. and 00fh ;01c7 ; Mask lower nibble (hardware check)
  733. ret nz ;01c9 ; If error, return
  734. ld a,0aah ;01ca ; Load test pattern 0xAA
  735. call check_peri_01d2h ;01cc ; Output 0xAA to port 0x06 (SIO B data port)
  736. and 00fh ;01cf ; Mask lower nibble (hardware check)
  737. ret ;01d1 ; Return (hardware init complete)
  738. check_peri_01d2h:
  739. out (c),a ;01d2 ; Output value in A to port C
  740. ld b,a ;01d4 ; Save value in B
  741. in a,(c) ;01d5 ; Read value back from port C into A
  742. xor b ;01d7 ; Compare read value with what was written
  743. ret ;01d8 ; Return
  744. initsio_l01d9h:
  745. ; PRIMARY SIO CARD initialization table (ports 0x04-0x07)
  746. ; This is the first/preferred SIO card on the bus
  747. defb 0x02 ;01d9 ; Count: 2 bytes to output
  748. defb 0x05 ;01da ; Port: 0x05 (Primary SIO Channel A Control)
  749. defb 0xCF ;01db ; Data byte 1: WR 7, SEND ABORT (SDLC), RESET Tx UNDERRUN/EOM LATCH
  750. defb 0x00 ;01dc ; Data byte 2: SYNC BIT 8-15 = 0
  751. defb 0x02 ;01dd ; Count: 2 bytes to output
  752. defb 0x07 ;01de ; Port: 0x07 (Primary SIO Channel B Control)
  753. defb 0xCF ;01df ; Data byte 1: WR 7, SEND ABORT (SDLC), RESET Tx UNDERRUN/EOM LATCH
  754. defb 0xF0 ;01e0 ; Data byte 2: Reset channel B sync bit 12-15
  755. defb 0x00 ;01e1 ; END marker
  756. setupsio_table:
  757. l01e2h:
  758. ; CTC setup for SIO timing
  759. defb 0x02 ;01e2 ; 2 bytes to output
  760. defb 0x02 ;01e3 ; CTC Channel 2 (provides clock for SIO)
  761. defb 0x47 ;01e4 ; CTC Control: Software reset, Time constant follows, Prescaler=16, Counter mode
  762. defb 0x01 ;01e5 ; Time constant = 1 (fast clock for SIO)
  763. ; PRIMARY SIO CHANNEL B (Console/Terminal) - Complete WR Configuration
  764. defb 0x11 ;01e6 ; 17 bytes to configure Channel B
  765. defb 0x07 ;01e7 ; Port 0x07 (Primary SIO Channel B Control)
  766. ; WR0 Commands and Register Selection
  767. defb 0x58 ;01e8 ; WR0: 01011000b = Channel Reset + Reset Rx CRC Checker
  768. defb 0x02 ;01e9 ; WR0: 00000010b = Select WR2 for next write
  769. defb 0x00 ;01ea ; WR2: Interrupt vector = 0x00 (disable interrupts)
  770. defb 0x04 ;01eb ; WR0: 00000100b = Select WR4 for next write
  771. defb 0x10 ;01ec ; WR4: 00010000b = 16x clock, No parity, 1 stop bit, No sync
  772. defb 0x15 ;01ed ; WR0: 00010101b = Select WR5 + Send Abort (SDLC mode prep)
  773. defb 0x84 ;01ee ; WR5: 10000100b = DTR active, 8 bits/char, Tx disabled initially, No RTS yet
  774. defb 0x03 ;01ef ; WR0: 00000011b = Select WR3 + Reset External Status interrupts
  775. defb 0xd2 ;01f0 ; WR3: 11010010b = Rx 8 bits, Enter Hunt Phase, Sync Char Load Inhibit, Rx disabled initially
  776. defb 0x06 ;01f1 ; WR0: 00000110b = Select WR6 for next write
  777. defb 0xff ;01f2 ; WR6: Sync Character bits 0-7 = 0xFF (sync pattern low byte)
  778. defb 0x07 ;01f3 ; WR0: 00000111b = Select WR7 for next write
  779. defb 0x0f ;01f4 ; WR7: Sync Character bits 8-15 = 0x0F (sync pattern high byte)
  780. defb 0x11 ;01f5 ; WR0: 00010001b = Select WR1 + Reset External Status interrupts
  781. defb 0x0c ;01f6 ; WR1: 00001100b = Status affects vector, Rx INT on first char or special condition
  782. defb 0x23 ;01f7 ; WR0: 00100011b = Select WR3 + Enable INT on next Rx char
  783. defb 0xd3 ;01f8 ; WR3: 11010011b = Rx Enable, 8 bits, Enter Hunt Phase, Sync Char Load Inhibit
  784. defb 0x00 ;01f9 ; END marker
  785. l01fa:
  786. ; PRIMARY SIO CHANNEL B (Console) - Simplified Setup for Normal Operation
  787. defb 0x0c ;01fa ; 12 bytes to configure Channel B for basic async operation
  788. defb 0x07 ;01fb ; Port 0x07 (Primary SIO Channel B Control)
  789. defb 0x18 ;01fc ; WR0: 00011000b = Channel Reset (clear all state)
  790. defb 0x02 ;01fd ; WR0: 00000010b = Select WR2 for next write
  791. defb 0x00 ;01fe ; WR2: Interrupt vector = 0x00 (no interrupts)
  792. defb 0x18 ;01ff ; WR0: 00011000b = Channel Reset (ensure clean state)
  793. defb 0x01 ;0200 ; WR0: 00000001b = Select WR1 for next write
  794. defb 0x04 ;0201 ; WR1: 00000100b = Status affects interrupt vector
  795. defb 0x04 ;0202 ; WR0: 00000100b = Select WR4 for next write
  796. defb 0x44 ;0203 ; WR4: 01000100b = 16x clock, 1 stop bit, No parity, 8 bits
  797. defb 0x03 ;0204 ; WR0: 00000011b = Select WR3 + Reset External Status
  798. defb 0xc1 ;0205 ; WR3: 11000001b = Rx Enable, 8 bits/char, No auto enables
  799. defb 0x05 ;0206 ; WR0: 00000101b = Select WR5
  800. defb 0xea ;0207 ; WR5: 11101010b = RTS, DTR, Tx Enable, 8 bits/char, Break
  801. ; PRIMARY SIO CHANNEL A (Auxiliary/Data) - Basic Setup
  802. defb 0x09 ;0208 ; 9 bytes to configure Channel A
  803. defb 0x05 ;0209 ; Port 0x05 (Primary SIO Channel A Control)
  804. defb 0x18 ;020a ; WR0: 00011000b = Channel Reset
  805. defb 0x01 ;020b ; WR0: 00000001b = Select WR1 for next write
  806. defb 0x00 ;020c ; WR1: 00000000b = No interrupts, no status affects vector
  807. defb 0x04 ;020d ; WR0: 00000100b = Select WR4 for next write
  808. defb 0x44 ;020e ; WR4: 01000100b = 16x clock, 1 stop bit, No parity, 8 bits
  809. defb 0x03 ;020f ; WR0: 00000011b = Select WR3 + Reset External Status
  810. defb 0xc1 ;0210 ; WR3: 11000001b = Rx Enable, 8 bits/char
  811. defb 0x05 ;0211 ; WR0: 00000101b = Select WR5
  812. defb 0xea ;0212 ; WR5: 11101010b = RTS, DTR, Tx Enable, 8 bits/char, Break
  813. defb 0x00 ;0213 ; END marker
  814. l0214h:
  815. ; SECONDARY SIO CARD initialization table (ports 0xC0-0xC4)
  816. ; This is an alternate SIO card on the bus, used if primary SIO fails
  817. defb 0x02 ;0214 ; 2 bytes to output
  818. defb 0x02 ;0215 ; CTC channel (shared timing for secondary SIO)
  819. defb 0x47 ;0216 ; CTC Control: SW reset, Time constant follows, Prescaler=16, Counter mode
  820. defb 0x04 ;0217 ; Time constant = 4 (different timing from primary)
  821. defb 0x02 ;0218 ; 2 bytes to output
  822. defb 0xc4 ;0219 ; Port 0xC4 (Secondary SIO Channel A Control - high address mapping)
  823. defb 0x47 ;021a ; Command: 01000111b = Select WR4 + Software reset + Time constant
  824. defb 0x01 ;021b ; Parameter value
  825. ; SECONDARY SIO CHANNEL B (Console backup) - Initial Setup
  826. defb 0x05 ;021c ; 5 bytes to output
  827. defb 0xc3 ;021d ; Port 0xC3 (Secondary SIO Channel B Control - high address mapping)
  828. defb 0x58 ;021e ; WR0: 01011000b = Channel Reset + Reset Rx CRC Checker
  829. defb 0x02 ;021f ; WR0: 00000010b = Select WR2 for next write
  830. defb 0x10 ;0220 ; WR2: Interrupt vector = 0x10 (different from primary)
  831. defb 0x01 ;0221 ; WR0: 00000001b = Select WR1 for next write
  832. defb 0x04 ;0222 ; WR1: 00000100b = Status affects interrupt vector
  833. ; SECONDARY SIO CHANNEL A (Data backup) - Complete Configuration
  834. defb 0x0f ;0223 ; 15 bytes to output (extensive SIO configuration)
  835. defb 0xc1 ;0224 ; Port 0xC1 (Secondary SIO Channel A Control - high address mapping)
  836. defb 0x58 ;0225 ; WR0: 01011000b = Channel Reset + Reset Rx CRC Checker
  837. defb 0x04 ;0226 ; WR0: 00000100b = Select WR4 for next write
  838. defb 0x10 ;0227 ; WR4: 00010000b = 16x clock, No parity, 1 stop bit
  839. defb 0x15 ;0228 ; WR0: 00010101b = Select WR5 + Send Abort (SDLC prep)
  840. defb 0x84 ;0229 ; WR5: 10000100b = DTR active, 8 bits/char, Tx disabled initially
  841. defb 0x03 ;022a ; WR0: 00000011b = Select WR3 + Reset External Status
  842. defb 0xd2 ;022b ; WR3: 11010010b = Rx 8 bits, Enter Hunt Phase, Sync Char Load Inhibit
  843. defb 0x06 ;022c ; WR0: 00000110b = Select WR6 for next write
  844. defb 0xFF ;022d ; WR6: Sync Character bits 0-7 = 0xFF
  845. defb 0x07 ;022e ; WR0: 00000111b = Select WR7 for next write
  846. defb 0x0f ;022f ; WR7: Sync Character bits 8-15 = 0x0F
  847. defb 0x11 ;0230 ; WR0: 00010001b = Select WR1 + Reset External Status
  848. defb 0x0c ;0231 ; WR1: 00001100b = Status affects vector, Rx INT on first char
  849. defb 0x23 ;0232 ; WR0: 00100011b = Select WR3 + Enable INT on next Rx char
  850. defb 0xd3 ;0233 ; WR3: 11010011b = Rx Enable, 8 bits, Enter Hunt Phase
  851. defb 0x00 ;0234 ; END marker
  852. l0235h:
  853. ; SECONDARY SIO - Final Configuration for Normal Operation
  854. ; Simplified setup for basic async operation on backup SIO card
  855. defb 0x0c ;0235 ; 12 bytes to output
  856. defb 0xc3 ;0236 ; Port 0xC3 (Secondary SIO Channel B Control)
  857. defb 0x18 ;0237 ; WR0: 00011000b = Channel Reset (clear all state)
  858. defb 0x02 ;0238 ; WR0: 00000010b = Select WR2 for next write
  859. defb 0x10 ;0239 ; WR2: Interrupt vector = 0x10 (secondary interrupt vector)
  860. defb 0x18 ;023a ; WR0: 00011000b = Channel Reset (ensure clean state)
  861. defb 0x01 ;023b ; WR0: 00000001b = Select WR1 for next write
  862. defb 0x04 ;023c ; WR1: 00000100b = Status affects interrupt vector
  863. defb 0x04 ;023d ; WR0: 00000100b = Select WR4 for next write
  864. defb 0x44 ;023e ; WR4: 01000100b = 16x clock, 1 stop bit, No parity, 8 bits
  865. defb 0x03 ;023f ; WR0: 00000011b = Select WR3 + Reset External Status
  866. defb 0xc1 ;0240 ; WR3: 11000001b = Rx Enable, 8 bits/char, No auto enables
  867. defb 0x05 ;0241 ; WR0: 00000101b = Select WR5
  868. defb 0xea ;0242 ; WR5: 11101010b = RTS, DTR, Tx Enable, 8 bits/char, Break
  869. ; SECONDARY SIO CHANNEL A - Basic Setup
  870. defb 0x09 ;0243 ; 9 bytes to output
  871. defb 0xc1 ;0244 ; Port 0xC1 (Secondary SIO Channel A Control)
  872. defb 0x18 ;0245 ; WR0: 00011000b = Channel Reset
  873. defb 0x01 ;0246 ; WR0: 00000001b = Select WR1 for next write
  874. defb 0x00 ;0247 ; WR1: 00000000b = No interrupts, no status affects vector
  875. defb 0x04 ;0248 ; WR0: 00000100b = Select WR4 for next write
  876. defb 0x44 ;0249 ; WR4: 01000100b = 16x clock, 1 stop bit, No parity, 8 bits
  877. defb 0x03 ;024a ; WR0: 00000011b = Select WR3 + Reset External Status
  878. defb 0xc1 ;024b ; WR3: 11000001b = Rx Enable, 8 bits/char
  879. defb 0x05 ;024c ; WR0: 00000101b = Select WR5
  880. defb 0xea ;024d ; WR5: 11101010b = RTS, DTR, Tx Enable, 8 bits/char, Break
  881. defb 0x00 ;024e ; END marker
  882. ;==============================================================================
  883. ; INTERRUPT VECTOR TABLE FOR SIO OPERATIONS
  884. ;==============================================================================
  885. ; These vectors are loaded into RAM at 0x1300+ for interrupt mode 2 operation
  886. ; Provides specific handlers for different SIO interrupt conditions
  887. ;==============================================================================
  888. defb 0x91 ;024f ; Interrupt vector base (0x1300)
  889. defb 0x02 ;0250 ; Interrupt linterupt2vector 0x1301
  890. defb 0x92 ;0251 ; Interrupt vector 0x1302
  891. defb 0x02 ;0252 ; Interrupt linterupt2vector 0x1303
  892. defb 0x6f ;0253 ; Interrupt vector 0x1304
  893. defb 0x02 ;0254 ; Interrupt linterupt1vector 0x1305
  894. defb 0x91 ;0255 ; Interrupt vector 0x1306
  895. defb 0x02 ;0256 ; Interrupt linterupt2vector 0x1307
  896. defb 0x91 ;0257 ; Interrupt vector 0x1308
  897. defb 0x02 ;0258 ; Interrupt linterupt2vector 0x1309
  898. defb 0x91 ;0259 ; Interrupt vector 0x130a
  899. defb 0x02 ;025a ; Interrupt linterupt2vector 0x130b
  900. defb 0x91 ;025b ; Interrupt vector 0x130c
  901. defb 0x02 ;025c ; Interrupt linterupt2vector 0x130d
  902. defb 0x91 ;025d ; Interrupt vector 0x130e
  903. defb 0x02 ;025e ; Interrupt linterupt2vector 0x130f
  904. defb 0x91 ;025f ; Interrupt vector 0x1310
  905. defb 0x02 ;0260 ; Interrupt linterupt2vector 0x1311
  906. defb 0x91 ;0261 ; Interrupt vector 0x1312
  907. defb 0x02 ;0262 ; Interrupt linterupt2vector 0x1313
  908. defb 0x91 ;0263 ; Interrupt vector 0x1314
  909. defb 0x02 ;0264 ; Interrupt linterupt2vector 0x1315
  910. defb 0x91 ;0265 ; Interrupt vector 0x1316
  911. defb 0x02 ;0266 ; Interrupt linterupt2vector 0x1317
  912. defb 0x91 ;0267 ; Interrupt vector 0x1318
  913. defb 0x02 ;0268 ; Interrupt linterupt2vector 0x1319
  914. defb 0x91 ;0269 ; Interrupt vector 0x1320
  915. defb 0x02 ;026A ; Interrupt linterupt2vector 0x1321
  916. defb 0x6f ;026B ; Interrupt vector 0x1322
  917. defb 0x02 ;026C ; Interrupt linterupt1vector 0x1323
  918. defb 0x91 ;026D ; Interrupt vector 0x1324
  919. defb 0x02 ;026E ; Interrupt linterupt2vector 0x1325
  920. linterupt1:
  921. push af ;026f ; Save accumulator and flags
  922. push bc ;0270 ; Save BC register pair
  923. call sub_0149h ;0271 ; Setup: loads serial port address into C (from 0x1242), C=command/status port
  924. push hl ;0274 ; Save HL register pair
  925. ld hl,l0000h ;0275 ; HL = 0 (used as a counter for timeout/retries)
  926. l0278h:
  927. dec c ;0278 ; C = C - 1 (now C=data port)
  928. in a,(c) ;0279 ; Read data register from port C
  929. or a ;027b ; Set flags based on A (is data nonzero?)
  930. jr nz,l0288h ;027c ; If data != 0, jump to l0288h (data ready)
  931. inc hl ;027e ; HL = HL + 1 (increment timeout/retry counter)
  932. inc c ;027f ; C = C + 1 (restore C to command/status port,)
  933. l0280h:
  934. in a,(c) ;0280 ; Read command/status register from port C,
  935. bit 0,a ;0282 ; Test bit 0 (is data available/ready) Rx CHARACTER AVAILABLE
  936. jr nz,l0278h ;0284 ; If bit 0 set, go back to data port read
  937. jr l0280h ;0286 ; Otherwise, keep polling command/status port
  938. l0288h:
  939. ld (01100h),a ;0288 ; Store received byte in 0x1100
  940. ld (01101h),hl ;028b ; Store HL (timeout/retry count) in 0x1101/0x1102
  941. pop hl ;028e ; Restore HL (was used as counter)
  942. pop bc ;028f ; Restore BC
  943. pop af ;0290 ; Restore AF
  944. linterupt2:
  945. ei ;0291 ; Enable interrupts
  946. reti ;0292 ; Return from interrupt
  947. ; --------------------------------------------------
  948. ; 0x0294: System Initialization Block
  949. ; - Sets stack pointer to 0x1400
  950. ; - Loads interrupt vector (A=0x13), sets I register
  951. ; - Sets interrupt mode 2 and enables interrupts
  952. ; - Calls sub_0770h to save CPU context and further setup
  953. ; --------------------------------------------------
  954. l0294h:
  955. ld sp,01400h ;0294
  956. ld a,013h ;0297
  957. ld i,a ;0299
  958. im 2 ;029b
  959. ei ;029d
  960. call sub_0770h ;029e
  961. ; --------------------------------------------------
  962. ; After 0x029e: Continued System/Memory Initialization
  963. ; - Sets HL to 01100h, stores at 011ech
  964. ; - Sets HL to l0000h, stores at 011f0h
  965. ; - Sets HL to l0fech, calls l00fdh (setup/copy routine)
  966. ; - Sets HL to 01104h, stores at 011f2h
  967. ; - Loads A with 0x2A, stores at 011f6h
  968. ; - Calls sub_080dh (setup/test routine)
  969. ; - Branches: if NZ, jumps to l02c9h; else, jumps to l0fbbh
  970. ; Purpose: Finalizes pointers, memory, and hardware setup, then tests/branches on result
  971. ; --------------------------------------------------
  972. ld hl,01100h ;02a1
  973. ld (011ech),hl ;02a4
  974. ld hl,l0000h ;02a7
  975. ld (011f0h),hl ;02aa
  976. ld hl,l0fech ;02ad Load HL with address of version string ("Ver 1.2x 8 inch")
  977. call l00fdh ;02b0 Print version string to console via standard output routine
  978. ld hl,01104h ;02b3 Set HL to console buffer address (0x1104)
  979. ld (011f2h),hl ;02b6 Store console buffer pointer at 0x011F2
  980. ld a,02ah ;02b9
  981. ld (011f6h),a ;02bb
  982. call sub_080dh ;02be
  983. jr nz,l02c9h ;02c1
  984. jp l0fbbh ;02c3
  985. l02c6h:
  986. call sub_080dh ;02c6 Get user input from console
  987. l02c9h:
  988. call sub_02e4h ;02c9 Execute parsed command (dual-purpose code)
  989. ; =============================================================================
  990. ; MAIN MONITOR COMMAND LOOP - Heart of the Debug System
  991. ; =============================================================================
  992. ; Purpose: Main command prompt and processing loop for the monitor/debugger
  993. ; Operation: Sets up input buffer, displays '>' prompt, processes commands
  994. ; Flow: All monitor commands (dump, compare, breakpoint, etc.) return here
  995. ; This is the central control point of the entire monitor system
  996. ; =============================================================================
  997. l02cch: ; Main Monitor Command Loop Entry Point
  998. ld hl,01104h ;02cc Point to console input buffer start
  999. ld (011f2h),hl ;02cf Store input buffer pointer for system use
  1000. ld a,03eh ;02d2 Load '>' character (0x3E = ASCII prompt)
  1001. ld (011f6h),a ;02d4 Store prompt character for display
  1002. jr l02c6h ;02d7 Jump to input processing (creates command loop)
  1003. l02d9h:
  1004. ld sp,01400h ;02d9
  1005. ld hl,l0323h ;02dc
  1006. call l00fdh ;02df
  1007. jr l02cch ;02e2
  1008. ;==============================================================================
  1009. ; DUAL-PURPOSE CODE/DATA TABLE (0x02E4-0x0322)
  1010. ;==============================================================================
  1011. ; This section serves as BOTH executable code AND data table!
  1012. ;
  1013. ; AS EXECUTABLE CODE (when called from 0x02C9):
  1014. ; - 0x02E4: CD 4C 06 call sub_064ch
  1015. ; - 0x02E7: 13 inc de
  1016. ; - 0x02E8: 42 ld b,d
  1017. ; - 0x02E9: E2 03 43 jp po,4303h
  1018. ; - etc.
  1019. ;
  1020. ; AS DATA TABLE (when accessed as bytes):
  1021. ; Contains ASCII character sequence B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,W,X
  1022. ; This appears to be a character encoding or command lookup table
  1023. ;
  1024. ; This technique was common in 1980s ROMs to save precious memory space
  1025. ; by making code serve dual purposes as both instructions and data
  1026. ;==============================================================================
  1027. sub_02e4h: ; Entry point when called as subroutine
  1028. call sub_064ch ;02e4 CD 4C 06
  1029. inc de ;02e7 13
  1030. ld b,d ;02e8 42 (ASCII 'B')
  1031. jp po,04303h ;02e9 E2 03 43 (0x43 = ASCII 'C')
  1032. inc hl ;02ec 23
  1033. inc bc ;02ed 03
  1034. ld b,h ;02ee 44 (ASCII 'D')
  1035. ld e,b ;02ef 58
  1036. inc bc ;02f0 03
  1037. ld b,l ;02f1 45 (ASCII 'E')
  1038. dec d ;02f2 15
  1039. dec b ;02f3 05
  1040. ld b,(hl) ;02f4 46 (ASCII 'F')
  1041. or a ;02f5 B7
  1042. inc bc ;02f6 03
  1043. ld b,a ;02f7 47 (ASCII 'G')
  1044. ld c,d ;02f8 4A (ASCII 'J')
  1045. inc b ;02f9 04
  1046. ld c,b ;02fa 48 (ASCII 'H')
  1047. add a,a ;02fb 87
  1048. ex af,af' ;02fc 08
  1049. ld c,c ;02fd 49 (ASCII 'I')
  1050. rst 8 ;02fe CF
  1051. inc bc ;02ff 03
  1052. ld c,d ;0300 4A (ASCII 'J')
  1053. ld b,c ;0301 41
  1054. inc b ;0302 04
  1055. ld c,h ;0303 4C (ASCII 'L')
  1056. sbc a,a ;0304 9F
  1057. ex af,af' ;0305 08
  1058. ld c,l ;0306 4D (ASCII 'M')
  1059. call p,04e04h ;0307 FC 04 4E
  1060. sbc a,a ;030a 9F
  1061. inc b ;030b 04
  1062. ld c,a ;030c 4F (ASCII 'O')
  1063. cp h ;030d BC
  1064. add hl,bc ;030e 09
  1065. ld d,b ;030f 50 (ASCII 'P')
  1066. ld l,a ;0310 6F
  1067. ex af,af' ;0311 08
  1068. ld d,d ;0312 52 (ASCII 'R')
  1069. rst 38h ;0313 FF
  1070. add hl,bc ;0314 09
  1071. ld d,e ;0315 53 (ASCII 'S')
  1072. ccf ;0316 3F
  1073. ld b,054h ;0317 06 54 (0x54 = ASCII 'T')
  1074. ld c,a ;0319 4F
  1075. ld bc,0fa57h ;031a 01 57 FA (0x57 = ASCII 'W')
  1076. add hl,bc ;031d 09
  1077. ld e,b ;031e 58 (ASCII 'X')
  1078. rrca ;031f 0F
  1079. inc b ;0320 04
  1080. ccf ;0321 3F (ASCII '?')
  1081. nop ;0322 00 (NULL terminator)
  1082. ; =============================================================================
  1083. ; MEMORY COMPARE COMMAND (C command) - Monitor/Debugger Function
  1084. ; =============================================================================
  1085. ; Purpose: Compare two memory regions and display any differences
  1086. ; Input: User enters hex addresses via console (C start1 end1 start2)
  1087. ; Example: "C 1000 10FF 2000" compares memory 1000-10FF with 2000-20FF
  1088. ; Operation: Compares memory at [start1..end1] with memory starting at start2
  1089. ; Output: Shows mismatched bytes with both addresses and different values
  1090. ; Silent operation if regions are identical (no output = blocks match)
  1091. ; Error handling: Returns to monitor prompt on invalid input or completion
  1092. ; Used by: Monitor command interpreter for memory debugging and verification
  1093. ; =============================================================================
  1094. l0323h: ; Memory Compare Command Entry Point
  1095. call sub_0760h ;0323 Parse 3 hex addresses from user input (start1, end1, start2)
  1096. jp z,l02d9h ;0326 Jump to error handler if hex parsing failed
  1097. ex de,hl ;0329 DE=start1, HL=start2 (swap for comparison loop setup)
  1098. l032ah: ; Main Memory Comparison Loop
  1099. call sub_09a3h ;032a Check for keyboard input (allows user to abort with any key)
  1100. ld a,(de) ;032d Load byte from first memory region (source)
  1101. cp (hl) ;032e Compare with byte from second memory region (destination)
  1102. call nz,sub_0338h ;032f If bytes differ, display the mismatch details
  1103. inc de ;0332 Move to next byte in first region (source++)
  1104. cpi ;0333 CP (HL), INC HL, DEC BC - auto-increment second region
  1105. ret po ;0335 Return when BC=0 (all bytes compared, PO=parity odd/BC=0)
  1106. jr l032ah ;0336 Continue comparison loop until all bytes checked
  1107. ; =============================================================================
  1108. ; MEMORY MISMATCH DISPLAY HANDLER
  1109. ; =============================================================================
  1110. ; Purpose: Display details when memory comparison finds a difference
  1111. ; Input: DE=address in first region, HL=address in second region, A=(DE), (HL) differ
  1112. ; Output: Displays both addresses and their differing byte values
  1113. ; Format: Shows both addresses with their different values for user analysis
  1114. ; Example output might be: "1000: 42 2000: 24" (showing the mismatch)
  1115. ; =============================================================================
  1116. sub_0338h: ; Memory Mismatch Display Handler
  1117. push de ;0338 Save first region address on stack
  1118. call sub_0346h ;0339 Display first address and its byte value (format: "ADDR: XX")
  1119. ex (sp),hl ;033c Swap: HL=first addr (from stack), stack=second addr
  1120. call sub_0346h ;033d Display second address and its byte value (format: "ADDR: YY")
  1121. call sub_072ah ;0340 Output formatting/newline routine (complete the line)
  1122. ex de,hl ;0343 Restore proper register order (DE=first, HL=second)
  1123. pop hl ;0344 Restore second region address from stack
  1124. ret ;0345 Return to comparison loop
  1125. ; =============================================================================
  1126. ; ADDRESS AND BYTE VALUE DISPLAY ROUTINE
  1127. ; =============================================================================
  1128. ; Purpose: Display an address followed by the byte value at that address
  1129. ; Input: HL=memory address to display
  1130. ; Output: Shows "ADDR: XX" format to console
  1131. ; Used by: Memory compare, memory dump, and other debugging commands
  1132. ; =============================================================================
  1133. sub_0346h: ; Display Address and Byte Value
  1134. push hl ;0346 Save address
  1135. ld a,(hl) ;0347 Load byte value from address
  1136. push af ;0348 Save the byte value
  1137. call sub_06b8h ;0349 Display the address (HL) in hex
  1138. ld hl,(011f2h) ;034c Get console output buffer pointer
  1139. dec hl ;034f Back up one position
  1140. ld (hl),03dh ;0350 Insert '=' character (0x3D = ASCII '=')
  1141. pop af ;0352 Restore byte value
  1142. call sub_06c6h ;0353 Display byte value in hex
  1143. pop hl ;0356 Restore address
  1144. ret ;0357 Return
  1145. call sub_0763h ;0358
  1146. jr z,l03a0h ;035b
  1147. exx ;035d
  1148. ; =============================================================================
  1149. ; MEMORY DUMP/HEX DISPLAY COMMAND - 32 Bytes per Screen
  1150. ; =============================================================================
  1151. ; Purpose: Display memory contents in hex dump format (D command)
  1152. ; Input: Starting address in HL' (alternate register set)
  1153. ; Output format: XXXX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX *................*
  1154. ; Where: XXXX = 4-digit hex address, XX = hex bytes, * = ASCII representation
  1155. ; Block size: 16 bytes per line, 2 lines = 32 bytes total per screen
  1156. ; User can press any key to abort display and return to monitor
  1157. ; =============================================================================
  1158. l035eh: ; Memory Dump Command Entry Point
  1159. call sub_09a3h ;035e Check for keyboard input/break (allows user abort)
  1160. ld hl,01104h ;0361 Point to display buffer start (console buffer workspace)
  1161. ld de,01105h ;0364 Point to buffer start + 1 (for block fill operation)
  1162. ld bc,00049h ;0367 Set count = 73 bytes to clear (full display line + margin)
  1163. ld (hl),020h ;036a Put space character (0x20) in first position of buffer
  1164. ldir ;036c Fill entire buffer with spaces (clear display workspace)
  1165. ld b,010h ;036e Set counter for 16 bytes per line (standard hex dump width)
  1166. ld hl,01139h ;0370 Point to ASCII display area in buffer (right side of display)
  1167. ld (hl),02ah ;0373 Put '*' character as left ASCII area separator
  1168. inc hl ;0375 Move to first ASCII character position
  1169. exx ;0376 Switch to alternate register set (HL'=current memory address)
  1170. call sub_06b8h ;0377 Display 4-digit hex address at line start (XXXX format)
  1171. l037ah: ; Main byte display loop (16 bytes per line)
  1172. ld a,(hl) ;037a Load byte from current memory address
  1173. exx ;037b Switch to buffer registers (for ASCII display area)
  1174. ld (hl),a ;037c Store byte in ASCII display area (right side preview)
  1175. cp 020h ;037d Compare with space character (0x20)
  1176. jr c,l0385h ;037f If < 0x20 (control character), replace with dot
  1177. cp 07fh ;0381 Compare with DEL character (0x7F)
  1178. jr c,l0387h ;0383 If < 0x7F (printable ASCII), keep original character
  1179. l0385h:
  1180. ld (hl),02eh ;0385 Replace with '.' for non-printable characters (0x00-0x1F, 0x7F-0xFF)
  1181. l0387h:
  1182. inc hl ;0387 Move to next ASCII display position
  1183. ld (hl),02ah ;0388 Put '*' separator after ASCII character
  1184. exx ;038a Switch back to memory address registers
  1185. call sub_06c6h ;038b Display the hex value of current byte (XX format)
  1186. cpi ;038e Compare and increment HL, decrement BC (built-in loop control)
  1187. exx ;0390 Switch to buffer registers for ASCII area management
  1188. inc hl ;0391 Move buffer pointer to next ASCII position
  1189. jp po,l0727h ;0392 If BC=0 (parity odd=end of block), finish and display line
  1190. djnz l039ch ;0395 Decrement line counter B, if not 0 continue current line
  1191. call l0727h ;0397 Display the completed line (16 bytes hex + ASCII representation)
  1192. jr l035eh ;039a Start next line (display next address block)
  1193. l039ch: ; Continue current line processing
  1194. dec hl ;039c Back up buffer pointer (adjust for proper spacing)
  1195. exx ;039d Switch back to memory address registers
  1196. jr l037ah ;039e Continue byte processing loop for same line
  1197. ; =============================================================================
  1198. ; MEMORY EXAMINE AND MODIFY COMMAND (M command)
  1199. ; =============================================================================
  1200. ; Purpose: Interactive memory editing - display address, show current value,
  1201. ; allow user to enter new value, store it, and continue to next address
  1202. ; Input: HL = starting memory address (parsed from user command like "M 1000")
  1203. ; Operation: Shows "ADDR: XX" format, waits for user input, stores new value
  1204. ; User Interface: Enter new hex value to change, press Enter to skip, 'Q' to quit
  1205. ; Exit: User presses 'Q' to quit and return to monitor prompt
  1206. ; Example: "1000: 42" - shows address 1000 has value 42, user can change it
  1207. ; =============================================================================
  1208. l03a0h: ; Memory Examine/Modify Entry Point
  1209. push hl ;03a0 Save current memory address on stack
  1210. call sub_06b8h ;03a1 Display address in 4-digit hex format (e.g., "1000:")
  1211. ld a,(hl) ;03a4 Load current byte value from memory at address
  1212. call sub_0708h ;03a5 Display current value and get user input (hex parser)
  1213. jr z,l03b0h ;03a8 If no input/Enter pressed, skip to next address
  1214. pop hl ;03aa Restore memory address from stack
  1215. ld (hl),a ;03ab Store new value entered by user into memory
  1216. push hl ;03ac Save address on stack again for next iteration
  1217. call sub_0662h ;03ad Call verification/display routine (confirm change)
  1218. l03b0h: ; Next address processing
  1219. pop hl ;03b0 Restore current address from stack
  1220. cp 051h ;03b1 Compare input with 'Q' (0x51 = ASCII 'Q' for quit)
  1221. ret z ;03b3 Return to monitor prompt if user pressed 'Q'
  1222. inc hl ;03b4 Move to next memory address (auto-increment)
  1223. jr l03a0h ;03b5 Loop back to examine next byte (continue editing)
  1224. ; =============================================================================
  1225. ; MEMORY FILL COMMAND (F command) - Fill Memory Block with Pattern Byte
  1226. ; =============================================================================
  1227. ; Purpose: Fill a memory region with a specified byte value using efficient block copy
  1228. ; Input: User enters "F START END VALUE" - three hex parameters on command line
  1229. ; Operation: Fills memory from START to END (inclusive) with VALUE byte
  1230. ; Example: "F 1000 10FF 42" fills addresses 1000-10FF with 0x42 (256 bytes)
  1231. ; Special case: "FO" prefix jumps to different command handler (File Operations?)
  1232. ; Technique: Uses single byte + LDIR for efficient bulk memory fill
  1233. ; =============================================================================
  1234. l03b7h: ; Memory Fill Command Entry Point
  1235. call sub_065eh ;03b7 Get next command character from input buffer (after 'F')
  1236. cp 04fh ;03ba Compare with 'O' (0x4F = ASCII 'O')
  1237. jp z,l0a74h ;03bc If 'O', jump to File Operations handler (FO command)
  1238. call sub_0760h ;03bf Parse three hex parameters: start addr→DE, end addr→HL, value→A
  1239. and a ;03c2 Clear carry flag for subtraction (prepare SBC calculation)
  1240. ld (de),a ;03c3 Store fill value at start address (seeds the pattern)
  1241. sbc hl,de ;03c4 Calculate length = end address - start address
  1242. ret z ;03c6 Return if length is zero (start=end, only one byte filled)
  1243. ld b,h ;03c7 Move length to BC register pair (16-bit byte count)
  1244. ld c,l ;03c8 BC = number of bytes remaining to fill
  1245. ld h,d ;03c9 HL = start address (source for propagating pattern)
  1246. ld l,e ;03ca Copy DE (start) to HL
  1247. inc de ;03cb DE = start address + 1 (destination for pattern copy)
  1248. ldir ;03cc Fill memory: copy (HL) to (DE), repeat BC times
  1249. ret ;03ce Return to monitor (entire memory region now filled)
  1250. ; =============================================================================
  1251. ; DEBUG/DISPLAY PARAMETER COMMAND (I command) - Configure Display Settings
  1252. ; =============================================================================
  1253. ; Purpose: Configure debugging display parameters, modes, or formatting options
  1254. ; Input: User enters "I XXXX" where XXXX is hex value for display parameter
  1255. ; Storage: Parameter value stored at memory address 0x11D6
  1256. ; Special case: "II" command jumps to specialized display handler at 0x0A86
  1257. ; Use cases: Sets display width, format modes, or debugging visualization options
  1258. ; Examples: "I 10" might set 16-byte display width, "I 01" might enable trace mode
  1259. ; =============================================================================
  1260. l03cfh: ; I Command (Debug/Display Parameter) Entry Point
  1261. call sub_065eh ;03cf Get next command character from input buffer (check for "II")
  1262. cp 044h ;03d2 Compare with 'I' (0x49 = ASCII 'I' for "II" command variant)
  1263. jp z,l0a86h ;03d4 If second 'I', jump to specialized display command handler
  1264. ld a,(011d6h) ;03d7 Load current debug display parameter from memory (0x11D6)
  1265. call sub_0708h ;03da Get hex input from user (new parameter value for I command)
  1266. ret z ;03dd Return if no input provided (display current parameter value)
  1267. ld (011d6h),a ;03de Store new debug display parameter at 0x11D6
  1268. ret ;03e1 Return to monitor (I command parameter updated)
  1269. ; =============================================================================
  1270. ; BREAKPOINT/EXECUTION COUNTER COMMAND - Program Execution Control
  1271. ; =============================================================================
  1272. ; Purpose: Set up execution counter for controlled program stepping/tracing
  1273. ; Input: User enters hex counter value (number of instructions to execute)
  1274. ; Operation: Sets up debugging counter for single-step or limited execution
  1275. ; Use case: "B 100" might execute 256 instructions then break back to monitor
  1276. ; =============================================================================
  1277. l03e2h: ; Breakpoint/Counter Command Entry Point
  1278. call sub_03f7h ;03e2 Call setup/initialization routine (clear counters?)
  1279. call sub_0680h ;03e5 Parse hex input from user (execution count)
  1280. ret z ;03e8 Return if no valid input provided (show current count?)
  1281. call sub_0405h ;03e9 Process the counter value (setup execution limit)
  1282. sub_03ech: ; Counter Processing Subroutine - EXECUTION STEP COUNTER
  1283. call sub_0680h ;03ec Parse hex input from user (step count parameter)
  1284. jr nz,l03f3h ;03ef If valid hex input provided, store it as step count
  1285. ld a,001h ;03f1 Default step count = 1 (single step if no input)
  1286. l03f3h:
  1287. ld (011d0h),a ;03f3 Store step counter at 0x11D0 (execution step limit)
  1288. ret ;03f6 Return to single-step execution controller
  1289. ; =============================================================================
  1290. ; BREAKPOINT RESTORATION ROUTINE
  1291. ; =============================================================================
  1292. ; Purpose: Restore original instruction that was replaced by breakpoint
  1293. ; Operation: Reads saved instruction byte and restores it to memory
  1294. ; Used by: Breakpoint system to clean up after break execution
  1295. ; =============================================================================
  1296. sub_03f7h: ; Breakpoint Restoration
  1297. ld hl,(011f0h) ;03f7 Load breakpoint address from memory
  1298. ld a,(011cfh) ;03fa Load saved original instruction byte
  1299. ld (hl),a ;03fd Restore original instruction at breakpoint
  1300. ld hl,l0000h ;03fe Clear breakpoint address (set to 0000)
  1301. ld (011f0h),hl ;0401 Store cleared address (no active breakpoint)
  1302. l0404h:
  1303. ret ;0404 Return
  1304. ; =============================================================================
  1305. ; BREAKPOINT INSTALLATION ROUTINE
  1306. ; =============================================================================
  1307. ; Purpose: Install breakpoint by replacing instruction with 0xFF
  1308. ; Operation: Saves original instruction and replaces it with break opcode
  1309. ; Input: HL = address where breakpoint should be set
  1310. ; Memory: 011cfh = saved original instruction, 011f0h = breakpoint address
  1311. ; =============================================================================
  1312. sub_0405h: ; Breakpoint Installation
  1313. ld a,(hl) ;0405 Load original instruction from target address
  1314. ld (011cfh),a ;0406 Save original instruction byte
  1315. ld (hl),0ffh ;0409 Replace with breakpoint opcode (0xFF = RST 38H)
  1316. ld (011f0h),hl ;040b Store breakpoint address for later restoration
  1317. ret ;040e Return
  1318. ; =============================================================================
  1319. ; INSTRUCTION ANALYSIS AND BREAKPOINT PLACEMENT
  1320. ; =============================================================================
  1321. ; Purpose: Analyze instruction stream and set appropriate breakpoints
  1322. ; Used by: Single-step debugging and program flow analysis
  1323. ; =============================================================================
  1324. l040fh: ; Instruction Analysis Entry Point
  1325. call sub_03f7h ;040f Restore any existing breakpoint first
  1326. ld hl,(011eah) ;0412 Load current execution address
  1327. call sub_0425h ;0415 Analyze instruction at current address
  1328. jp nc,l049fh ;0418 If NOT special instruction, jump to normal execution path
  1329. ld e,a ;041b Move instruction length to E register
  1330. ld d,000h ;041c Clear D register (DE = instruction length in bytes)
  1331. add hl,de ;041e Calculate next instruction address (current + length)
  1332. call sub_0405h ;041f Set breakpoint at NEXT instruction (step-over mode)
  1333. jp l044ah ;0422 Jump to execution setup and run until breakpoint hit
  1334. ; =============================================================================
  1335. ; Z80 INSTRUCTION DECODER - Analyzes instruction types for debugging
  1336. ; =============================================================================
  1337. ; Purpose: Decode Z80 instructions to determine their type and length
  1338. ; Input: HL points to instruction to analyze
  1339. ; Output: Carry set if special instruction, A = instruction length
  1340. ; Special instructions: CALL, RET, conditional jumps, block operations
  1341. ; =============================================================================
  1342. sub_0425h: ; Z80 Instruction Decoder
  1343. ld a,(hl) ;0425 Load instruction opcode
  1344. cp 0edh ;0426 Check for ED prefix (extended instructions)
  1345. jr z,l0437h ;0428 If ED prefix, handle extended instructions
  1346. cp 0cdh ;042a Check for CALL instruction (0xCD)
  1347. jr z,l0433h ;042c If CALL, return length 3
  1348. and 0c4h ;042e Mask bits to check instruction pattern
  1349. xor 0c4h ;0430 Check for conditional CALL pattern (11xxx100)
  1350. ret nz ;0432 Return if not special instruction (carry clear)
  1351. l0433h: ; CALL instruction handler
  1352. ld a,003h ;0433 CALL instructions are 3 bytes long
  1353. scf ;0435 Set carry flag (special instruction detected)
  1354. ret ;0436 Return with instruction length
  1355. l0437h: ; Extended instruction handler (ED prefix)
  1356. inc hl ;0437 Move to second byte of ED instruction
  1357. ld a,(hl) ;0438 Load second opcode byte
  1358. and 0f0h ;0439 Mask upper nibble
  1359. xor 0b0h ;043b Check for block operation pattern (10110xxx)
  1360. ret nz ;043d Return if not block operation
  1361. inc a ;043e A = 1 (ED block instructions are 2 bytes)
  1362. scf ;043f Set carry flag (special instruction detected)
  1363. ret ;0440 Return with instruction length
  1364. ; =============================================================================
  1365. ; JUMP/EXECUTE COMMAND (J command) - Execute program from specified address
  1366. ; =============================================================================
  1367. ; Purpose: Start program execution from user-specified address (same as G command)
  1368. ; Input: User provides "J XXXX" where XXXX is hex address to execute
  1369. ; Operation: Sets up execution environment and transfers control to user program
  1370. ; Note: J command shares the same implementation as G (Go) command
  1371. ; =============================================================================
  1372. l0441h: ; J Command (Jump/Execute) Entry Point
  1373. call sub_0680h ;0441 Parse hex address from user input (e.g., "J 1000")
  1374. jp z,l02d9h ;0444 Jump to error handler if invalid address entered
  1375. ld (011eah),hl ;0447 Store execution start address in system variable
  1376. ; =============================================================================
  1377. ; PROGRAM EXECUTION CONTROLLER (shared by G and J commands)
  1378. ; =============================================================================
  1379. ; Purpose: Set up CPU state and execute user program with debugging support
  1380. ; Features: Interrupt handling, breakpoint detection, register restoration
  1381. ; Used by: Both G (Go) and J (Jump) commands use this same execution logic
  1382. ; =============================================================================
  1383. l044ah: ; Program Execution Setup
  1384. ld a,(011d7h) ;044a Load interrupt vector high byte from system config
  1385. ld i,a ;044d Set Z80 interrupt vector register (for IM2 mode)
  1386. ld de,(011eah) ;044f Load target execution address from user input (G/J command)
  1387. ld hl,(011f0h) ;0453 Load current breakpoint address from active breakpoint
  1388. and a ;0456 Clear carry flag for 16-bit address comparison
  1389. sbc hl,de ;0457 Compare: is breakpoint already at execution start address?
  1390. jr z,l0472h ;0459 If breakpoint at execution address, handle as special case
  1391. ld hl,(011d6h) ;045b Load debug control parameters (interrupt/trace modes)
  1392. ld a,h ;045e Get high byte of debug params for interrupt vector
  1393. ld i,a ;045f Set interrupt vector register for user program context
  1394. di ;0461 Disable interrupts during critical register setup phase
  1395. bit 0,l ;0462 Check debug control bit 0 (interrupt enable flag for user)
  1396. jr z,l0467h ;0464 If bit 0 clear, keep interrupts disabled during execution
  1397. ei ;0466 Re-enable interrupts if user program expects them enabled
  1398. ;==============================================================================
  1399. ; USER PROGRAM EXECUTION CONTROLLER (0x0467-0x0471)
  1400. ;==============================================================================
  1401. ; Purpose: Execute user program with full debugger support and state management
  1402. ; Entry: l0467h - Called after breakpoint setup or direct execution request
  1403. ; Operation: Restore registers → Execute user code → Handle return → Process results
  1404. ;==============================================================================
  1405. l0467h: ; Execute User Program Entry Point
  1406. call sub_079bh ;0467 CRITICAL: Restore all registers and execute user code
  1407. ld hl,(011eah) ;046a Reload execution address from save area (post-execution)
  1408. push hl ;046d Save execution address on stack for debug analysis
  1409. ld hl,(011d4h) ;046e Load debug parameters/status from execution control
  1410. ret ;0471 Return to calling debugger routine with status
  1411. ;==============================================================================
  1412. ; ERROR HANDLER AND MEMORY RESTORATION (0x0472-0x0478)
  1413. ;==============================================================================
  1414. ; Purpose: Handle execution errors and restore memory state after breakpoints
  1415. ; Used by: Debugger cleanup routines and breakpoint removal system
  1416. ;==============================================================================
  1417. l0472h: ; Error Handler Entry Point
  1418. ld hl,l07efh ;0472 Load address for execution/error handler routine
  1419. l0475h: ; Memory restoration entry point (breakpoint cleanup)
  1420. ld a,(011cfh) ;0475 Load saved byte value from backup storage location
  1421. ld (de),a ;0478 Restore original byte at target address (remove breakpoint)
  1422. ;==============================================================================
  1423. ; BREAKPOINT INSTALLATION WITH MEMORY PAGING (0x0479-0x049D)
  1424. ;==============================================================================
  1425. ; Purpose: Install software breakpoint in paged memory with full state management
  1426. ; Features: Memory paging control, interrupt management, state backup/restoration
  1427. ; Operation: Enables paging → Selects memory page → Plants breakpoint → Saves state
  1428. ;
  1429. ; Memory Paging Sequence:
  1430. ; 1. Disable interrupts for atomic operation
  1431. ; 2. Configure interrupt mode 2 with vector table at 0x1300
  1432. ; 3. Enable 64KB memory card paging system via port 0x000
  1433. ; 4. Select target memory page via dual-write to port 0x003
  1434. ; 5. Install breakpoint instruction and backup original data
  1435. ;
  1436. ; Port Operations:
  1437. ; • PORT 0x000 (0x20): Memory management control - enables bank switching
  1438. ; • PORT 0x003 (0x87, 0x01): Page selection - first 0x87 (page 7 + control), then 0x01 (page 1)
  1439. ;
  1440. ; State Management:
  1441. ; • Backs up original instruction before planting 0xFB (EI) breakpoint
  1442. ; • Stores breakpoint location and original data for safe restoration
  1443. ; • Integrates with debugger's execution control and single-step system
  1444. ;==============================================================================
  1445. l0479h: ; Breakpoint Installation Entry Point
  1446. dec de ;0479 Decrement target address by 1 (align to exact breakpoint location)
  1447. di ;047a Disable interrupts for atomic breakpoint installation
  1448. ld a,013h ;047b Set interrupt vector base 0x13 (points to table at 0x1300)
  1449. ld i,a ;047d Load interrupt vector register (prepare for IM2 mode)
  1450. ld a,020h ;047f Memory control: 0x20 = Enable 64KB paging system
  1451. out (000h),a ;0481 PORT 0x000: Memory management control (activate paging)
  1452. im 2 ;0483 Set interrupt mode 2 (vectored interrupts via table)
  1453. ld a,087h ;0485 Page select: 0x87 = Page 7 + control bits active
  1454. out (003h),a ;0487 PORT 0x003: Page selection register (first selection)
  1455. ld a,001h ;0489 Page select: 0x01 = Select memory page 1 for breakpoint
  1456. out (003h),a ;048b PORT 0x003: Page selection register (second selection)
  1457. ld (01326h),hl ;048d Store HL at 0x1326 (breakpoint address for paging system)
  1458. ex de,hl ;0490 Exchange: HL=decremented target addr, DE=original addr
  1459. ld (011d1h),hl ;0491 Store active breakpoint address at 0x11D1 (current BP)
  1460. ld a,(hl) ;0494 Load original instruction byte from breakpoint location
  1461. ld (011d3h),a ;0495 Store original byte at 0x11D3 (backup for restoration)
  1462. ld (hl),0fbh ;0498 Plant breakpoint: 0xFB = EI instruction (interrupt enable)
  1463. ld (011eah),hl ;049a Store breakpoint address at 0x11EA (execution target)
  1464. jr l0467h ;049d Jump to execution preparation (complete breakpoint cycle)
  1465. ;==============================================================================
  1466. ; NORMAL INSTRUCTION EXECUTION PATH (0x049F) - NON-SPECIAL INSTRUCTIONS
  1467. ;==============================================================================
  1468. ; Purpose: Handle execution of normal instructions (not CALL/RET/conditional jumps)
  1469. ; Entry: Jumped to from 0x0418 when instruction decoder finds standard instruction
  1470. ; Operation: Set up single-step debugging environment for normal instruction execution
  1471. ; Features: Console buffer setup, memory management, step-by-step execution control
  1472. ;==============================================================================
  1473. l049fh: ; Normal Instruction Execution Entry Point
  1474. call sub_05a5h ;049f Initialize console buffer system (prepare for single-step I/O)
  1475. call sub_03ech ;04a2 Process execution counter/step parameters
  1476. l04a5h: ; Single-Step Execution Loop Controller
  1477. ld hl,(011d6h) ;04a5 Load debug control parameters from system settings
  1478. ld (011eeh),hl ;04a8 Save debug parameters for restoration after execution
  1479. ld de,(011eah) ;04ab Load current execution address (where to execute next)
  1480. ld hl,(011f0h) ;04af Load active breakpoint address from system
  1481. and a ;04b2 Clear carry flag for 16-bit address comparison
  1482. sbc hl,de ;04b3 Compare: is execution address same as breakpoint address?
  1483. ld hl,l04bch ;04b5 Load address of breakpoint handler routine
  1484. jr z,l0475h ;04b8 If at breakpoint, jump to memory restoration routine
  1485. jr l0479h ;04ba Otherwise, jump to breakpoint installation routine
  1486. l04bch:
  1487. ld (011d4h),hl ;04bc
  1488. pop hl ;04bf
  1489. ld (011ech),sp ;04c0
  1490. ld sp,01400h ;04c4
  1491. call sub_0770h ;04c7
  1492. call sub_04ebh ;04ca
  1493. ld hl,l04d3h ;04cd
  1494. push hl ;04d0
  1495. reti ;04d1
  1496. l04d3h:
  1497. ld hl,(011eeh) ;04d3
  1498. ld (011d6h),hl ;04d6
  1499. call sub_0582h ;04d9
  1500. ld hl,011d0h ;04dc
  1501. dec (hl) ;04df
  1502. jr nz,l04a5h ;04e0
  1503. inc (hl) ;04e2
  1504. call sub_080dh ;04e3
  1505. jp nz,l02c9h ;04e6
  1506. jr l04a5h ;04e9
  1507. ;==============================================================================
  1508. ; MEMORY PAGING RESTORE ROUTINE (0x04EB)
  1509. ;==============================================================================
  1510. ; Purpose: Restore default memory page and clean up paging state
  1511. ; Called during system shutdown or page switching operations
  1512. ;
  1513. ; Operation:
  1514. ; - Switches back to page 3 (likely default/system page)
  1515. ; - Restores original memory contents from backup
  1516. ; - Cleans up paging state for safe transition
  1517. ;==============================================================================
  1518. sub_04ebh:
  1519. ld a,003h ;04eb ; Load page 3 (default/system page)
  1520. out (003h),a ;04ed ; PORT 0x003: Switch to page 3
  1521. ld hl,(011d1h) ;04ef ; Load backup memory address
  1522. ld a,(011d3h) ;04f2 ; Load original byte value
  1523. ld (hl),a ;04f5 ; Restore original memory contents
  1524. ld hl,(011f0h) ;04f6 ; Load cleanup address
  1525. ld (hl),0ffh ;04f9 ; Mark memory as restored/available
  1526. ret ;04fb ; Return to caller
  1527. call sub_0760h ;04fc
  1528. jp z,l02d9h ;04ff
  1529. push hl ;0502
  1530. and a ;0503
  1531. sbc hl,de ;0504
  1532. pop hl ;0506
  1533. l0507h:
  1534. jr c,l050ch ;0507
  1535. l0509h:
  1536. ldir ;0509
  1537. ret ;050b
  1538. l050ch:
  1539. add hl,bc ;050c
  1540. dec hl ;050d
  1541. ex de,hl ;050e
  1542. add hl,bc ;050f
  1543. dec hl ;0510
  1544. ex de,hl ;0511
  1545. lddr ;0512
  1546. ret ;0514
  1547. call sub_0662h ;0515
  1548. jr z,l057fh ;0518
  1549. ld d,a ;051a
  1550. call sub_065eh ;051b
  1551. jr nz,l0522h ;051e
  1552. ld a,020h ;0520
  1553. l0522h:
  1554. ld e,a ;0522
  1555. call sub_05cch ;0523
  1556. dec hl ;0526
  1557. dec hl ;0527
  1558. jp nz,l02d9h ;0528
  1559. l052bh:
  1560. ld de,01104h ;052b
  1561. ldi ;052e
  1562. ldi ;0530
  1563. push hl ;0532
  1564. ex de,hl ;0533
  1565. ld (hl),020h ;0534
  1566. inc hl ;0536
  1567. ld (011f2h),hl ;0537
  1568. ld hl,l060eh ;053a
  1569. or a ;053d
  1570. ex de,hl ;053e
  1571. sbc hl,de ;053f
  1572. ld e,(ix+000h) ;0541
  1573. ld d,(ix+001h) ;0544
  1574. jr nc,l056ah ;0547
  1575. ld a,(de) ;0549
  1576. push de ;054a
  1577. call sub_0708h ;054b
  1578. pop de ;054e
  1579. jr z,l0555h ;054f
  1580. ld (de),a ;0551
  1581. l0552h:
  1582. call sub_0662h ;0552
  1583. l0555h:
  1584. cp 051h ;0555
  1585. pop hl ;0557
  1586. ret z ;0558
  1587. inc ix ;0559
  1588. inc ix ;055b
  1589. ex de,hl ;055d
  1590. push hl ;055e
  1591. ld hl,l0615h ;055f
  1592. or a ;0562
  1593. sbc hl,de ;0563
  1594. pop hl ;0565
  1595. ex de,hl ;0566
  1596. jr nz,l052bh ;0567
  1597. ret ;0569
  1598. l056ah:
  1599. ld a,(de) ;056a
  1600. ld l,a ;056b
  1601. inc de ;056c
  1602. ld a,(de) ;056d
  1603. ld h,a ;056e
  1604. call sub_06b8h ;056f
  1605. push de ;0572
  1606. call sub_070bh ;0573
  1607. pop de ;0576
  1608. jr z,l0555h ;0577
  1609. ex de,hl ;0579
  1610. ld (hl),d ;057a
  1611. dec hl ;057b
  1612. ld (hl),e ;057c
  1613. jr l0552h ;057d
  1614. l057fh:
  1615. call sub_05a5h ;057f
  1616. sub_0582h:
  1617. ld b,011h ;0582
  1618. ld hl,l0615h ;0584
  1619. l0587h:
  1620. ld e,(hl) ;0587
  1621. inc hl ;0588
  1622. ld d,(hl) ;0589
  1623. inc hl ;058a
  1624. ld a,(de) ;058b
  1625. call sub_06c6h ;058c
  1626. djnz l0587h ;058f
  1627. ld b,004h ;0591
  1628. l0593h:
  1629. ld e,(hl) ;0593
  1630. inc hl ;0594
  1631. ld d,(hl) ;0595
  1632. inc hl ;0596
  1633. ex de,hl ;0597
  1634. ld a,(hl) ;0598
  1635. inc hl ;0599
  1636. ld h,(hl) ;059a
  1637. ld l,a ;059b
  1638. call sub_06b8h ;059c
  1639. ex de,hl ;059f
  1640. djnz l0593h ;05a0
  1641. l05a2h:
  1642. jp sub_072ah ;05a2
  1643. ;==============================================================================
  1644. ; CONSOLE BUFFER INITIALIZATION (sub_05a5h) - REGISTER DISPLAY SETUP
  1645. ;==============================================================================
  1646. ; Purpose: Initialize console buffer system with register name templates
  1647. ; Operation: Copy register name strings from ROM table to RAM buffer area
  1648. ; Used by: Single-step debugging to prepare register display output
  1649. ; Buffer: 0x1104 = console buffer start, filled with register names for display
  1650. ;==============================================================================
  1651. sub_05a5h: ; Console Buffer Initialization Entry Point
  1652. ld de,01104h ;05a5 DE = console buffer start address (0x1104)
  1653. ld hl,l05ebh ;05a8 HL = register name table in ROM (0x05EB - register names)
  1654. ld bc,011ffh ;05ab BC = loop count (B=0x01, C=0xFF - copy parameters)
  1655. ld a,020h ;05ae A = 0x20 (space character for formatting)
  1656. l05b0h: ; Register Name Copy Loop
  1657. ldi ;05b0 Copy first character from register table to buffer
  1658. ldi ;05b2 Copy second character from register table to buffer
  1659. ld (de),a ;05b4 Store space character (0x20) after register name
  1660. inc de ;05b5 Advance buffer pointer past space
  1661. djnz l05b0h ;05b6 Loop until all register names copied (B times)
  1662. ld b,004h ;05b8 Set loop counter for additional buffer setup (4 iterations)
  1663. l05bah: ; Additional Buffer Formatting Loop
  1664. ld (de),a ;05ba Store space character in buffer
  1665. inc de ;05bb Advance buffer pointer
  1666. ldi ;05bc Copy character from register table to buffer
  1667. ldi ;05be Copy second character from register table to buffer
  1668. ld (de),a ;05c0 Store space character after register name
  1669. inc de ;05c1 Advance buffer pointer
  1670. ld (de),a ;05c2 Store another space character (double spacing)
  1671. inc de ;05c3 Advance buffer pointer
  1672. djnz l05bah ;05c4 Loop for remaining register names (4 times)
  1673. ld (011f2h),de ;05c6 Store final buffer pointer at 0x11F2 (buffer end)
  1674. jr l05a2h ;05ca Jump to buffer completion routine
  1675. sub_05cch:
  1676. ld ix,l0615h ;05cc
  1677. ld bc,l002ah ;05d0
  1678. ld hl,l05ebh ;05d3
  1679. l05d6h:
  1680. ld a,d ;05d6
  1681. cpi ;05d7
  1682. jr z,l05e5h ;05d9
  1683. xor a ;05db
  1684. cpi ;05dc
  1685. l05deh:
  1686. ret po ;05de
  1687. inc ix ;05df
  1688. inc ix ;05e1
  1689. jr l05d6h ;05e3
  1690. l05e5h:
  1691. ld a,e ;05e5
  1692. cpi ;05e6
  1693. ret z ;05e8
  1694. jr l05deh ;05e9
  1695. ;==============================================================================
  1696. ; Z80 REGISTER NAME TABLE (0x05EB+)
  1697. ; Purpose: ASCII string table of Z80 register names for display/debugging
  1698. ; Format: Space-separated register names, single quotes for alternate registers
  1699. ; Content: "A B C D E F H L I A'B'C'D'E'F'H'L'IXI..."
  1700. ;==============================================================================
  1701. ;==============================================================================
  1702. ; Z80 REGISTER NAME AND ADDRESS LOOKUP TABLE (0x05EB-0x63D)
  1703. ; Purpose: Complete register name table with corresponding addresses for debugger/monitor
  1704. ; Format: ASCII register names followed by address lookup table
  1705. ;==============================================================================
  1706. register_names_05ebh:
  1707. ; Register name strings (space-separated)
  1708. db "A B C D E F H L I A'B'C'D'E'F'H'L'IXI" ;05eb-060f
  1709. db "YPCSP" ;0610-0614 (IY, PC, SP registers)
  1710. ; Address lookup table (each entry is 0x11 followed by address)
  1711. db 0xD9, 0x11, 0xDB, 0x11, 0xDA, 0x11, 0xDD, 0x11 ;0615-061c
  1712. db 0xDC, 0x11, 0xD8, 0x11, 0xD5, 0x11, 0xD4, 0x11 ;061d-0624
  1713. db 0xD7, 0x11, 0xDF, 0x11, 0xE1, 0x11, 0xE0, 0x11 ;0625-062c
  1714. db 0xE3, 0x11, 0xE2, 0x11, 0xDE, 0x11, 0xE5, 0x11 ;062d-0634
  1715. db 0xE4, 0x11, 0xE6, 0x11, 0xE8, 0x11, 0xEA, 0x11 ;0635-063c
  1716. db 0xEC, 0x11 ;063d-063e
  1717. ;==============================================================================
  1718. ; HEX INPUT SEQUENCE (0x063F)
  1719. ;==============================================================================
  1720. ; Purpose: Parse two hexadecimal values from user input
  1721. ; Typical use: Address and data entry for memory operations
  1722. ; Example: User types "1000 FF" to store 0xFF at address 0x1000
  1723. ;==============================================================================
  1724. sub_063fh:
  1725. call sub_0680h ;063f ; Parse first hex string (e.g., address)
  1726. push hl ;0642 ; Save first parsed value on stack
  1727. call sub_0680h ;0643 ; Parse second hex string (e.g., data)
  1728. pop hl ;0646 ; Restore first value to HL
  1729. ret z ;0647 ; Return if second parse failed
  1730. ld (hl),a ;0648 ; Store second value (data) at first value (address)
  1731. inc hl ;0649 ; Increment to next address
  1732. jr $-8 ;064a ; Loop back to continue operation
  1733. sub_064ch:
  1734. ex (sp),hl ;064c
  1735. ld b,(hl) ;064d
  1736. l064eh:
  1737. inc hl ;064e
  1738. cp (hl) ;064f
  1739. inc hl ;0650
  1740. ld e,(hl) ;0651
  1741. inc hl ;0652
  1742. ld d,(hl) ;0653
  1743. jr nz,l0659h ;0654
  1744. ex de,hl ;0656
  1745. ex (sp),hl ;0657
  1746. ret ;0658
  1747. l0659h:
  1748. djnz l064eh ;0659
  1749. jp l02d9h ;065b
  1750. sub_065eh:
  1751. inc hl ;065e
  1752. ld a,(hl) ;065f
  1753. jr l0677h ;0660
  1754. ; =============================================================================
  1755. ; INPUT BUFFER SCANNER - Skip Spaces and Process Next Character
  1756. ; =============================================================================
  1757. ; Purpose: Scan input buffer, skip spaces, and return next non-space character
  1758. ; Input: (011f4h) points to current position in input buffer
  1759. ; Output: A = next non-space character, HL updated to new position
  1760. ; Used by: Memory modify command to parse user input after displaying value
  1761. ; =============================================================================
  1762. sub_0662h: ; Input Buffer Scanner
  1763. ld hl,(011f4h) ;0662 Load current input buffer pointer
  1764. l0665h: ; Space-skipping loop
  1765. ld a,(hl) ;0665 Load character from buffer
  1766. cp 00dh ;0666 Check for CR (end of line)
  1767. jr z,l0674h ;0668 If CR, finish and update pointer
  1768. cp 020h ;066a Check for space character
  1769. l066ch: ; Continue scanning loop
  1770. inc hl ;066c Move to next character
  1771. jr nz,l0665h ;066d If not space, check if still space at new position
  1772. ld a,(hl) ;066f Load next character
  1773. cp 020h ;0670 Check if it's also a space
  1774. jr z,l066ch ;0672 If space, continue skipping spaces
  1775. l0674h: ; Update buffer pointer and process character
  1776. ld (011f4h),hl ;0674 Store updated buffer pointer
  1777. l0677h: ; Character processing
  1778. bit 6,a ;0677 Check bit 6 (lowercase letter detection)
  1779. jr z,l067dh ;0679 If not set, skip case conversion
  1780. res 5,a ;067b Convert lowercase to uppercase (clear bit 5)
  1781. l067dh: ; Final character check
  1782. cp 00dh ;067d Compare with CR (carriage return)
  1783. ret ;067f Return with flags set
  1784. ;==============================================================================
  1785. ; HEXADECIMAL STRING PARSER (0x0680)
  1786. ;==============================================================================
  1787. ; Purpose: Convert ASCII hexadecimal string to binary value
  1788. ; Input: DE points to ASCII hex string (e.g. "1A2F")
  1789. ; Output: HL contains parsed binary value, A contains last character processed
  1790. ;
  1791. ; Supported formats:
  1792. ; - Digits 0-9 (ASCII 0x30-0x39)
  1793. ; - Letters A-F (ASCII 0x41-0x46, case insensitive)
  1794. ; - Automatically converts lowercase a-f to uppercase A-F
  1795. ;
  1796. ; Error handling: Invalid characters cause jump to error handler at l02d9h
  1797. ;
  1798. ; Algorithm:
  1799. ; 1. Initialize result to 0
  1800. ; 2. For each character: validate, convert, pack into result
  1801. ; 3. Use RLD instruction for efficient 4-bit nibble packing
  1802. ; 4. Continue until non-hex character found
  1803. ;==============================================================================
  1804. sub_0680h:
  1805. ld hl,l0000h ;0680 ; Initialize result to 0
  1806. push hl ;0683 ; Save initial value on stack
  1807. add hl,sp ;0684 ; HL = stack pointer (for result storage)
  1808. ex de,hl ;0685 ; DE = stack pointer, HL = string pointer
  1809. call sub_0662h ;0686 ; Get next character from input
  1810. ex de,hl ;0689 ; Restore: HL = stack pointer, DE = string pointer
  1811. jr nz,l068fh ;068a ; If character available, process it
  1812. l068ch:
  1813. pop hl ;068c ; Restore result from stack
  1814. ld a,l ;068d ; Return low byte in A
  1815. ret ;068e ; Return with parsed value
  1816. l068fh:
  1817. cp 030h ;068f ; Compare with '0' (0x30)
  1818. jp c,l02d9h ;0691 ; If < '0', invalid hex character - error
  1819. l0694h:
  1820. cp 041h ;0694 ; Compare with 'A' (0x41)
  1821. jr c,l069ah ;0696 ; If < 'A', must be digit 0-9, go convert
  1822. res 5,a ;0698 ; Convert lowercase to uppercase (clear bit 5)
  1823. l069ah:
  1824. cp 03ah ;069a ; Compare with ':' (0x3A, one past '9')
  1825. jr c,l06aah ;069c ; If < ':', it's digit 0-9, go pack
  1826. cp 041h ;069e ; Compare with 'A' (0x41)
  1827. jp c,l02d9h ;06a0 ; If between ':' and 'A', invalid - error
  1828. cp 047h ;06a3 ; Compare with 'G' (0x47, one past 'F')
  1829. jp nc,l02d9h ;06a5 ; If >= 'G', invalid hex letter - error
  1830. sub 007h ;06a8 ; Convert A-F to 10-15 (subtract 7)
  1831. l06aah:
  1832. rld ;06aa ; Rotate left digit: pack nibble into (HL)
  1833. inc hl ;06ac ; Point to next byte for multi-byte values
  1834. rld ;06ad ; Pack into high nibble of next byte
  1835. dec hl ;06af ; Return to original position
  1836. inc de ;06b0 ; Point to next character in input string
  1837. ld a,(de) ;06b1 ; Load next character
  1838. cp 030h ;06b2 ; Check if >= '0'
  1839. jr nc,l0694h ;06b4 ; If valid hex character, continue parsing
  1840. jr l068ch ;06b6 ; Otherwise, end parsing and return result
  1841. sub_06b8h:
  1842. ld a,h ;06b8
  1843. call sub_06c6h ;06b9
  1844. push hl ;06bc
  1845. ld hl,(011f2h) ;06bd
  1846. dec hl ;06c0
  1847. ld (011f2h),hl ;06c1
  1848. pop hl ;06c4
  1849. ld a,l ;06c5
  1850. sub_06c6h:
  1851. push hl ;06c6
  1852. ld hl,(011f2h) ;06c7
  1853. call sub_06d6h ;06ca
  1854. inc hl ;06cd
  1855. ld (hl),020h ;06ce
  1856. inc hl ;06d0
  1857. ld (011f2h),hl ;06d1
  1858. pop hl ;06d4
  1859. ret ;06d5
  1860. sub_06d6h:
  1861. push af ;06d6
  1862. rra ;06d7
  1863. rra ;06d8
  1864. rra ;06d9
  1865. rra ;06da
  1866. call sub_06e0h ;06db
  1867. inc hl ;06de
  1868. pop af ;06df
  1869. sub_06e0h:
  1870. and 00fh ;06e0
  1871. cp 00ah ;06e2
  1872. jr c,l06e8h ;06e4
  1873. add a,007h ;06e6
  1874. l06e8h:
  1875. add a,030h ;06e8
  1876. ld (hl),a ;06ea
  1877. ret ;06eb
  1878. l06ech:
  1879. inc hl ;06ec
  1880. ld c,002h ;06ed
  1881. call sub_0111h ;06ef
  1882. l06f2h:
  1883. ld a,(hl) ;06f2
  1884. cp 020h ;06f3
  1885. jr z,l0702h ;06f5
  1886. cp 00dh ;06f7
  1887. jr z,l0702h ;06f9
  1888. ld c,a ;06fb
  1889. call sub_0111h ;06fc
  1890. inc hl ;06ff
  1891. jr l06f2h ;0700
  1892. l0702h:
  1893. ld c,003h ;0702
  1894. call sub_0111h ;0704
  1895. ret ;0707
  1896. sub_0708h:
  1897. call sub_06c6h ;0708
  1898. sub_070bh:
  1899. call sub_0745h ;070b
  1900. ld a,020h ;070e
  1901. ld (011f6h),a ;0710
  1902. call sub_080dh ;0713
  1903. cp 051h ;0716
  1904. ret z ;0718
  1905. ld hl,01168h ;0719
  1906. ld (hl),020h ;071c
  1907. ld (011f4h),hl ;071e
  1908. push de ;0721
  1909. call sub_0680h ;0722
  1910. pop de ;0725
  1911. ret ;0726
  1912. l0727h:
  1913. ld (011f2h),hl ;0727
  1914. sub_072ah:
  1915. push hl ;072a
  1916. push de ;072b
  1917. push bc ;072c
  1918. ld hl,(011f2h) ;072d
  1919. ld (hl),00dh ;0730
  1920. inc hl ;0732
  1921. ld (hl),00ah ;0733
  1922. inc hl ;0735
  1923. l0736h:
  1924. ld (hl),000h ;0736
  1925. ld hl,01104h ;0738
  1926. ld (011f2h),hl ;073b
  1927. call l00fdh ;073e
  1928. pop bc ;0741
  1929. pop de ;0742
  1930. pop hl ;0743
  1931. ret ;0744
  1932. sub_0745h:
  1933. push hl ;0745
  1934. push de ;0746
  1935. push bc ;0747
  1936. ld hl,(011f2h) ;0748
  1937. jr l0736h ;074b
  1938. sub_074dh:
  1939. push hl ;074d
  1940. push de ;074e
  1941. ld hl,(011f2h) ;074f
  1942. ld (hl),000h ;0752
  1943. ld hl,01104h ;0754
  1944. ld (011f2h),hl ;0757
  1945. call l0107h ;075a
  1946. pop de ;075d
  1947. pop hl ;075e
  1948. ret ;075f
  1949. sub_0760h:
  1950. call sub_0680h ;0760
  1951. sub_0763h:
  1952. push hl ;0763
  1953. call sub_0680h ;0764
  1954. push hl ;0767
  1955. call sub_0680h ;0768
  1956. ld b,h ;076b
  1957. ld c,l ;076c
  1958. pop hl ;076d
  1959. pop de ;076e Restore DE register
  1960. ret ;076f Return to caller
  1961. ;==============================================================================
  1962. ; COMPLETE CPU STATE SAVE ROUTINE (sub_0770h) - CRITICAL SYSTEM COMPONENT
  1963. ;==============================================================================
  1964. ; Purpose: Save ALL Z80 CPU registers and set up interrupt environment
  1965. ; Used by: Interrupt handlers, breakpoint system, system initialization
  1966. ; Operation: Saves complete CPU state to stack area, sets up IM2 interrupts
  1967. ; Stack Layout: Creates complete register save area for later restoration
  1968. ;
  1969. ; REGISTER SAVE ORDER (pushed to stack at 0x011EC):
  1970. ; 1. HL (main register pair)
  1971. ; 2. IY (index register Y)
  1972. ; 3. IX (index register X)
  1973. ; 4. HL' (alternate HL)
  1974. ; 5. DE' (alternate DE)
  1975. ; 6. BC' (alternate BC)
  1976. ; 7. AF' (alternate accumulator/flags)
  1977. ; 8. DE (main register pair)
  1978. ; 9. BC (main register pair)
  1979. ; 10. AF (main accumulator/flags)
  1980. ; 11. I register status (interrupt vector register)
  1981. ;==============================================================================
  1982. sub_0770h: ; Complete CPU State Save Entry Point
  1983. ld sp,011ech ;0770 Set stack pointer to register save area (0x011EC)
  1984. push hl ;0773 Save main HL register pair
  1985. push iy ;0774 Save IY index register (2 bytes)
  1986. push ix ;0776 Save IX index register (2 bytes)
  1987. exx ;0778 Switch to alternate register set
  1988. push hl ;0779 Save alternate HL register pair
  1989. push de ;077a Save alternate DE register pair
  1990. push bc ;077b Save alternate BC register pair
  1991. ex af,af' ;077c Switch to alternate AF register
  1992. push af ;077d Save alternate AF (accumulator/flags)
  1993. exx ;077e Switch back to main register set
  1994. ex af,af' ;077f Switch back to main AF register
  1995. push de ;0780 Save main DE register pair
  1996. push bc ;0781 Save main BC register pair
  1997. push af ;0782 Save main AF (accumulator/flags)
  1998. ld a,i ;0783 Load interrupt vector register into A
  1999. push af ;0785 Save interrupt vector register status
  2000. ld a,(011d6h) ;0786 Load debug control parameters from system config
  2001. rra ;0789 Rotate right bit 0 (extract interrupt enable flag)
  2002. rra ;078a Rotate right bit 1 (extract trace mode flag)
  2003. and 001h ;078b Mask to keep only bit 0 (interrupt enable for user)
  2004. ld (011d6h),a ;078d Store processed debug control back to memory
  2005. ld a,013h ;0790 Load interrupt vector base 0x13 (table at 0x1300)
  2006. ld i,a ;0792 Set interrupt vector register for system operation
  2007. im 2 ;0794 Set interrupt mode 2 (vectored interrupts)
  2008. ei ;0796 Enable interrupts for system operation
  2009. ld sp,013feh ;0797 Set stack pointer to system stack area (near top of RAM)
  2010. ret ;079a Return with complete CPU state saved
  2011. ; =============================================================================
  2012. ; USER PROGRAM EXECUTION ENGINE
  2013. ; =============================================================================
  2014. ; Purpose: Restore all CPU registers and transfer control to user program
  2015. ; Operation: Complete register restoration from saved state, then execute
  2016. ; Input: Stack contains saved register state, execution address in HL
  2017. ;==============================================================================
  2018. ; USER PROGRAM EXECUTION ENGINE (sub_079bh) - CRITICAL SYSTEM COMPONENT
  2019. ;==============================================================================
  2020. ; Purpose: Complete register restoration and user program execution
  2021. ; Entry: Called from 0x0467 after breakpoint installation or direct execution
  2022. ; Operation: Restore ALL CPU state → Execute user program → Handle completion
  2023. ; Exit: User program runs with full CPU state, returns via interrupt/break
  2024. ;
  2025. ; EXECUTION FLOW:
  2026. ; 1. Breakpoint installed at target address (0x0479)
  2027. ; 2. Control transferred to execution controller (0x0467)
  2028. ; 3. Register restoration engine called (sub_079bh)
  2029. ; 4. Complete CPU state restored from memory (0x011D8)
  2030. ; 5. User program executed via jp (hl)
  2031. ; 6. Returns via interrupt handler when breakpoint hit or error occurs
  2032. ;
  2033. ; REGISTER SAVE AREA LAYOUT (0x011D8):
  2034. ; • AF (main accumulator/flags)
  2035. ; • BC (main register pair)
  2036. ; • DE (main register pair)
  2037. ; • AF' (alternate accumulator/flags)
  2038. ; • BC' (alternate register pair)
  2039. ; • DE' (alternate register pair)
  2040. ; • HL' (alternate register pair)
  2041. ; • IX (index register X)
  2042. ; • IY (index register Y)
  2043. ; • SP (user stack pointer at 0x011EC)
  2044. ;==============================================================================
  2045. sub_079bh: ; User Program Execution Engine - Heart of debugger execution
  2046. pop hl ;079b Get return address from stack (user program target address)
  2047. ld sp,011d8h ;079c Point stack pointer to register save area at 0x011D8
  2048. pop af ;079f Restore main AF (accumulator + flags) from save area
  2049. pop bc ;07a0 Restore main BC register pair from save area
  2050. pop de ;07a1 Restore main DE register pair from save area
  2051. ex af,af' ;07a2 Switch to alternate AF register (prepare for alt restore)
  2052. exx ;07a3 Switch to alternate BC,DE,HL registers (prepare for alt restore)
  2053. pop af ;07a4 Restore alternate AF register from save area
  2054. pop bc ;07a5 Restore alternate BC register from save area
  2055. pop de ;07a6 Restore alternate DE register from save area
  2056. pop hl ;07a7 Restore alternate HL register (contains execution target!)
  2057. pop ix ;07a8 Restore IX index register from save area
  2058. pop iy ;07aa Restore IY index register from save area
  2059. exx ;07ac Switch back to main register set (BC,DE,HL)
  2060. ex af,af' ;07ad Switch back to main AF register
  2061. ld sp,(011ech) ;07ae Restore user's original stack pointer from 0x011EC
  2062. jp (hl) ;07b2 EXECUTE USER PROGRAM! Jump to target in HL register
  2063. ;==============================================================================
  2064. ; INTERRUPT/ERROR HANDLER (0x07B3)
  2065. ;==============================================================================
  2066. ; Purpose: Handle interrupts or errors, display "BREAK AT " message
  2067. ; Called from 0x0066 during error conditions or break events
  2068. ; This appears to be a debugging/error reporting mechanism
  2069. ;==============================================================================
  2070. l07b3h:
  2071. ld (011d4h),hl ;07b3 ; Save HL register for later restoration
  2072. ld hl,l07c1h ;07b6 ; Load address of continuation routine
  2073. ex (sp),hl ;07b9 ; Exchange with return address on stack
  2074. retn ;07ba ; Return from interrupt/error
  2075. l07bch:
  2076. ld (011d4h),hl ;07bc ; Save HL register to memory location 011d4h
  2077. pop hl ;07bf ; Restore HL from stack
  2078. dec hl ;07c0 ; Decrement HL (adjust return address or pointer)
  2079. l07c1h:
  2080. ld (011ech),sp ;07c1 ; Save current stack pointer to 011ech
  2081. ld sp,01400h ;07c5 ; Set new stack pointer to 0x1400 (top of RAM)
  2082. call sub_0770h ;07c8 ; Save all CPU registers and setup interrupt mode 2
  2083. ld de,(011eah) ;07cb ; Load DE from memory pointer 011eah
  2084. ld hl,(011f0h) ;07cf ; Load HL from memory pointer 011f0h
  2085. and a ;07d2 ; Clear carry flag for subtraction
  2086. sbc hl,de ;07d3 ; Compare HL and DE (subtract DE from HL)
  2087. jr nz,l07dfh ;07d5 ; If not equal, jump to boot message routine
  2088. ld hl,011d0h ;07d7 ; Point to counter at 011d0h
  2089. dec (hl) ;07da ; Decrement the counter
  2090. jp nz,l0472h ;07db ; If counter not zero, jump to l0472h
  2091. inc (hl) ;07de ; Restore counter (prevent underflow)
  2092. ; =============================================================================
  2093. ; BREAKPOINT/INTERRUPT HANDLER - Display Break Location=
  2094. ; ============================================================================
  2095. ; Purpose: Display "BREAK AT XXXX" message when execution is interrupted
  2096. ; Input: DE contains the address where the break/interrupt occurred
  2097. ; Output: "BREAK AT XXXX" followed by return to monitor prompt
  2098. ; Used by: Breakpoint system, interrupt handlers, debugging features
  2099. ; =============================================================================
  2100. l07dfh: ; Breakpoint Display Handler
  2101. ld hl,l0803h ;07df ; Point to "BREAK AT " message string
  2102. call l00fdh ;07e2 ; Output the "BREAK AT " text to console
  2103. ex de,hl ;07e5 ; DE = address where break occurred
  2104. call sub_06b8h ;07e6 ; Display break address in 4-digit hex (XXXX format)
  2105. call sub_072ah ;07e9 ; Output newline/formatting
  2106. jp l02cch ;07ec ; Return to monitor command prompt
  2107. l07efh:
  2108. push hl ;07ef
  2109. push af ;07f0
  2110. call sub_04ebh ;07f1
  2111. ld hl,(011d6h) ;07f4
  2112. ld a,h ;07f7
  2113. ld i,a ;07f8
  2114. bit 0,l ;07fa
  2115. jr z,l07ffh ;07fc
  2116. ei ;07fe
  2117. l07ffh:
  2118. pop af ;07ff
  2119. pop hl ;0800
  2120. reti ;0801
  2121. l0803h:
  2122. defb 0x42 ;0803 B
  2123. defb 0x52 ;0804 R
  2124. defb 0x45 ;0805 E
  2125. defb 0x41 ;0806 A
  2126. defb 0x4b ;0807 K
  2127. defb 0x20 ;0808
  2128. defb 0x41 ;0809 A
  2129. defb 0x54 ;080a T
  2130. defb 0x20 ;080b
  2131. defb 0x00 ;080c
  2132. sub_080dh:
  2133. ld a,(011f6h) ;080d
  2134. ld c,a ;0810
  2135. call sub_0127h ;0811
  2136. ld hl,01169h ;0814
  2137. ld (011f4h),hl ;0817
  2138. ld b,0ffh ;081a
  2139. l081ch:
  2140. push bc ;081c ; Save character count and buffer position
  2141. call l0134h ;081d ; Get character from input (blocks until available)
  2142. cp 020h ;0820 ; Compare with space character (0x20)
  2143. jr c,l083ch ;0822 ; Jump to special character handler if control character (0x00-0x1F)
  2144. cp 07fh ;0824 ; Compare with DEL character (0x7F)
  2145. jr z,l083ch ;0826 ; Jump to special character handler for DEL (same as backspace)
  2146. ld (hl),a ;0828
  2147. inc hl ;0829
  2148. call sub_0127h ;082a
  2149. pop bc ;082d
  2150. djnz l081ch ;082e
  2151. l0830h:
  2152. ld (hl),00dh ;0830
  2153. call sub_0862h ;0832
  2154. ld hl,01169h ;0835
  2155. ld a,(hl) ;0838
  2156. jp l0677h ;0839
  2157. l083ch:
  2158. pop bc ;083c
  2159. cp 00dh ;083d ; Check for CR (Carriage Return, Enter key)
  2160. jr z,l0830h ;083f ; Jump to end input processing if Enter pressed
  2161. cp 008h ;0841 ; Check for Backspace (0x08) - character deletion
  2162. jr z,l0849h ;0843 ; Jump to backspace handler
  2163. cp 07fh ;0845 ; Check for DEL (0x7F) - alternative character deletion
  2164. jr nz,l081ch ;0847 ; Continue input loop if not a special character
  2165. ;==============================================================================
  2166. ; BACKSPACE/DELETE CHARACTER HANDLER (0x0849)
  2167. ;==============================================================================
  2168. ; Purpose: Handle character deletion during input (Backspace 0x08 or DEL 0x7F)
  2169. ; Implements standard line editing by removing the previous character from both
  2170. ; the input buffer and the terminal display.
  2171. ;
  2172. ; Visual Feedback Sequence:
  2173. ; 1. Send Backspace (0x08) - moves cursor left one position
  2174. ; 2. Send Space (0x20) - overwrites the character with blank
  2175. ; 3. Send Backspace (0x08) - moves cursor back to correct position
  2176. ;
  2177. ; Buffer Management:
  2178. ; - Decrements buffer pointer (HL) to remove character
  2179. ; - Increments remaining character count (B)
  2180. ; - Prevents deletion beyond start of input buffer
  2181. ;
  2182. ; This provides standard terminal line editing behavior expected by users.
  2183. ;==============================================================================
  2184. l0849h:
  2185. ld a,b ;0849 ; Load remaining character count
  2186. inc a ;084a ; Increment to test for buffer underflow
  2187. l084bh:
  2188. jr z,l081ch ;084b ; Jump back to input loop if at buffer start (no chars to delete)
  2189. push bc ;084d ; Save character count and position
  2190. ld c,008h ;084e ; Load Backspace character (0x08)
  2191. call sub_0127h ;0850 ; Send backspace to terminal (move cursor left)
  2192. ld c,020h ;0853 ; Load Space character (0x20)
  2193. call sub_0127h ;0855 ; Send space to terminal (erase character)
  2194. ld c,008h ;0858 ; Load Backspace character (0x08) again
  2195. call sub_0127h ;085a ; Send backspace to terminal (reposition cursor)
  2196. pop bc ;085d ; Restore character count and position
  2197. inc b ;085e ; Increment remaining character count (one less char used)
  2198. dec hl ;085f ; Move buffer pointer back one position
  2199. jr l081ch ;0860 ; Return to main input loop
  2200. sub_0862h:
  2201. push bc ;0862
  2202. ld c,00dh ;0863
  2203. call sub_0127h ;0865
  2204. ld c,00ah ;0868
  2205. call sub_0127h ;086a
  2206. pop bc ;086d
  2207. ret ;086e
  2208. call sub_065eh ;086f
  2209. cp 055h ;0872
  2210. jp z,l0924h ;0874
  2211. call sub_0680h ;0877
  2212. ld c,l ;087a
  2213. ld b,h ;087b
  2214. in a,(c) ;087c
  2215. push bc ;087e
  2216. call sub_0708h ;087f
  2217. pop bc ;0882
  2218. ret z ;0883
  2219. out (c),a ;0884
  2220. ret ;0886
  2221. call sub_0680h ;0887
  2222. push hl ;088a
  2223. call sub_0680h ;088b
  2224. ex de,hl ;088e
  2225. pop hl ;088f
  2226. push hl ;0890
  2227. add hl,de ;0891
  2228. call sub_06b8h ;0892
  2229. pop hl ;0895
  2230. or a ;0896
  2231. sbc hl,de ;0897
  2232. call sub_06b8h ;0899
  2233. jp sub_072ah ;089c
  2234. ; =============================================================================
  2235. ; LOAD/TRANSFER DATA COMMAND (L command) - Memory/Storage Operations
  2236. ; =============================================================================
  2237. ; Purpose: Load or transfer data between memory locations or external storage
  2238. ; Input: User enters "L" optionally followed by hex parameters
  2239. ; Operation: Performs data loading/transfer operations with address management
  2240. ; Storage: Working address stored at 0x11CD for ongoing operations
  2241. ; Features: Supports data checksums, address calculations, and transfer loops
  2242. ; =============================================================================
  2243. l089fh: ; L Command (Load/Transfer) Entry Point
  2244. call sub_0917h ;089f Initialize/setup routine for load operations
  2245. call sub_0680h ;08a2 Parse optional hex parameter from user input
  2246. jr nz,l08aah ;08a5 If parameter provided, use it as load address
  2247. ld hl,l0000h ;08a7 Default to address 0x0000 if no parameter given
  2248. l08aah: ; Load operation setup
  2249. ld (011cdh),hl ;08aa Store load/transfer address at 0x11CD (working address)
  2250. ld c,011h ;08ad Set up for operations (C=0x11, might be record type/command)
  2251. call sub_099dh ;08af Call data processing routine (load/transfer handler)
  2252. l08b2h: ; Data format parsing loop
  2253. call sub_0997h ;08b2 Get next character from input stream
  2254. cp 03ah ;08b5 Compare with ':' character (0x3A = ASCII colon)
  2255. jr nz,l08b2h ;08b7 Keep searching until ':' found (record start marker)
  2256. call sub_08f8h ;08b9 Parse hex byte (record length/type)
  2257. ld b,a ;08bc Store parsed value in B register (byte count)
  2258. call sub_08efh ;08bd Parse hex address (load destination address)
  2259. or h ;08c0 Check if address is valid (OR with H register)
  2260. or b ;08c1 Check if byte count is valid (OR with B register)
  2261. jr nz,l08cch ;08c2
  2262. call sub_08efh ;08c4
  2263. ld a,013h ;08c7
  2264. jp sub_099dh ;08c9
  2265. l08cch:
  2266. call sub_08f8h ;08cc
  2267. add a,b ;08cf
  2268. add a,l ;08d0
  2269. add a,h ;08d1
  2270. ld c,a ;08d2
  2271. ld de,(011cdh) ;08d3
  2272. add hl,de ;08d7
  2273. l08d8h:
  2274. call sub_08f8h ;08d8
  2275. ld (hl),a ;08db
  2276. inc hl ;08dc
  2277. add a,c ;08dd
  2278. ld c,a ;08de
  2279. djnz l08d8h ;08df
  2280. call sub_08f8h ;08e1
  2281. add a,c ;08e4
  2282. jr z,l08b2h ;08e5
  2283. ld c,013h ;08e7
  2284. call sub_099dh ;08e9
  2285. jp l02d9h ;08ec
  2286. sub_08efh:
  2287. call sub_08f8h ;08ef
  2288. ld h,a ;08f2
  2289. call sub_08f8h ;08f3
  2290. ld l,a ;08f6
  2291. ret ;08f7
  2292. sub_08f8h:
  2293. push bc ;08f8
  2294. call sub_0907h ;08f9
  2295. add a,a ;08fc
  2296. add a,a ;08fd
  2297. add a,a ;08fe
  2298. add a,a ;08ff
  2299. ld b,a ;0900
  2300. call sub_0907h ;0901
  2301. add a,b ;0904
  2302. pop bc ;0905
  2303. ret ;0906
  2304. sub_0907h:
  2305. call sub_0997h ;0907
  2306. cp 030h ;090a
  2307. jr c,sub_0907h ;090c
  2308. cp 03ah ;090e
  2309. jr c,l0914h ;0910
  2310. sub 007h ;0912
  2311. l0914h:
  2312. and 00fh ;0914
  2313. ret ;0916
  2314. sub_0917h:
  2315. call sub_0662h ;0917
  2316. cp 040h ;091a
  2317. jp z,l06ech ;091c
  2318. dec hl ;091f
  2319. ld (011f4h),hl ;0920
  2320. ret ;0923
  2321. l0924h:
  2322. call sub_0917h ;0924
  2323. call sub_0680h ;0927
  2324. push hl ;092a
  2325. call sub_0680h ;092b
  2326. ld c,012h ;092e
  2327. call sub_099dh ;0930
  2328. pop de ;0933
  2329. l0934h:
  2330. push hl ;0934
  2331. ld b,010h ;0935
  2332. and a ;0937
  2333. sbc hl,de ;0938
  2334. jr c,l0970h ;093a
  2335. ld a,h ;093c
  2336. and a ;093d
  2337. jr nz,l0946h ;093e
  2338. ld a,l ;0940
  2339. cp b ;0941
  2340. jr nc,l0946h ;0942
  2341. ld b,a ;0944
  2342. inc b ;0945
  2343. l0946h:
  2344. ex de,hl ;0946
  2345. ld c,03ah ;0947
  2346. call sub_099dh ;0949
  2347. ld a,b ;094c
  2348. call sub_0985h ;094d
  2349. add a,h ;0950
  2350. add a,l ;0951
  2351. ld c,a ;0952
  2352. call sub_098ah ;0953
  2353. sub a ;0956
  2354. call sub_0985h ;0957
  2355. l095ah:
  2356. ld a,(hl) ;095a
  2357. call sub_0985h ;095b
  2358. add a,c ;095e
  2359. ld c,a ;095f
  2360. inc hl ;0960
  2361. djnz l095ah ;0961
  2362. ld a,c ;0963
  2363. neg ;0964
  2364. call sub_0985h ;0966
  2365. pop de ;0969
  2366. ex de,hl ;096a
  2367. call sub_074dh ;096b
  2368. jr l0934h ;096e
  2369. l0970h:
  2370. pop hl ;0970
  2371. ld c,03ah ;0971
  2372. call sub_099dh ;0973
  2373. ld b,00ah ;0976
  2374. ld c,030h ;0978
  2375. l097ah:
  2376. call sub_099dh ;097a
  2377. djnz l097ah ;097d
  2378. ld c,014h ;097f
  2379. call sub_099dh ;0981
  2380. ret ;0984
  2381. sub_0985h:
  2382. call sub_06c6h ;0985
  2383. jr l098dh ;0988
  2384. sub_098ah:
  2385. call sub_06b8h ;098a
  2386. l098dh:
  2387. push hl ;098d
  2388. ld hl,(011f2h) ;098e
  2389. dec hl ;0991
  2390. ld (011f2h),hl ;0992
  2391. pop hl ;0995
  2392. ret ;0996
  2393. sub_0997h:
  2394. push bc ;0997
  2395. call l0116h ;0998
  2396. pop bc ;099b
  2397. ret ;099c
  2398. sub_099dh:
  2399. push bc ;099d
  2400. call sub_0111h ;099e
  2401. pop bc ;09a1 ; Restore BC register
  2402. ret ;09a2 ; Return from routine
  2403. ;==============================================================================
  2404. ; CONTROL CHARACTER HANDLER (0x09A3)
  2405. ;==============================================================================
  2406. ; Purpose: Process special control characters during input operations
  2407. ; This routine intercepts and handles special ASCII control characters that
  2408. ; require immediate system response, providing user control over operations.
  2409. ;
  2410. ; Control Characters Handled:
  2411. ; • 0x03 (ETX, Ctrl+C): Emergency system reset/break
  2412. ; • 0x13 (XOFF, Ctrl+S): Pause/resume output flow control
  2413. ;
  2414. ; Character Processing:
  2415. ; 1. Call input routine (sub_013ah) to get character
  2416. ; 2. Check for zero (no character available)
  2417. ; 3. Compare with control character codes
  2418. ; 4. Take appropriate action or return
  2419. ;
  2420. ; Flow Control Features:
  2421. ; • Preserves BC register across all operations
  2422. ; • Returns immediately if no character or unrecognized character
  2423. ; • Provides clean stack management for nested calls
  2424. ; • Implements standard terminal control behavior
  2425. ;==============================================================================
  2426. sub_09a3h:
  2427. push bc ;09a3 ; Save BC register
  2428. call sub_013ah ;09a4 ; Get character from input (keyboard/serial)
  2429. pop bc ;09a7 ; Restore BC register
  2430. ret z ;09a8 ; Return if no character available (zero flag set)
  2431. cp 003h ;09a9 ; Check for ETX (End of Text, Ctrl+C)
  2432. jr z,l09b6h ;09ab ; Jump to system reset if Ctrl+C pressed
  2433. cp 013h ;09ad ; Check for XOFF (Ctrl+S, pause)
  2434. ret nz ;09af ; Return if not XOFF
  2435. push bc ;09b0 ; Save BC register
  2436. call l0134h ;09b1 ; Handle XOFF (pause/resume) functionality
  2437. pop bc ;09b4 ; Restore BC register
  2438. ret ;09b5 ; Return from XOFF handling
  2439. ;==============================================================================
  2440. ; SYSTEM RESET/RESTART ROUTINE (0x09B6)
  2441. ;==============================================================================
  2442. ; Purpose: Emergency system reset triggered by Ctrl+C (ETX) character
  2443. ; This routine provides a clean way to abort current operations and return
  2444. ; to the monitor command prompt, similar to a "break" or "interrupt" function.
  2445. ;
  2446. ; Trigger Conditions:
  2447. ; • Ctrl+C (0x03 ETX character) received during input processing
  2448. ; • User wants to abort current operation and return to monitor
  2449. ; • Emergency recovery from hung or problematic operations
  2450. ;
  2451. ; Reset Actions:
  2452. ; 1. Reset stack pointer to system default (0x1400)
  2453. ; 2. Jump directly to main monitor command loop
  2454. ; 3. Bypass any pending operations or nested calls
  2455. ;
  2456. ; Technical Details:
  2457. ; • Stack Reset: SP = 0x1400 (top of RAM workspace)
  2458. ; • Direct Jump: Goes to 0x02CC (main monitor loop entry)
  2459. ; • Clean State: Abandons current call stack and variables
  2460. ; • User Interface: Provides immediate return to ">" prompt
  2461. ;
  2462. ; This implements a standard "Ctrl+C break" behavior found in many systems
  2463. ; of this era, allowing users to escape from problematic situations.
  2464. ;==============================================================================
  2465. l09b6h:
  2466. ld sp,01400h ;09b6 ; Reset stack pointer to system default
  2467. jp l02cch ;09b9 ; Jump to main monitor command loop (clean restart)
  2468. call sub_065eh ;09bc
  2469. cp 053h ;09bf
  2470. jp z,l0fbbh ;09c1
  2471. call sub_0680h ;09c4
  2472. cp 004h ;09c7 Check if drive number >= 4 (invalid)
  2473. jp nc,l02d9h ;09c9 Jump to error if invalid drive number
  2474. ld (01202h),a ;09cc Store valid drive number (0-3)
  2475. call sub_0680h ;09cf Get next parameter from input
  2476. cp 002h ;09d2 Check if parameter < 2 (valid range)
  2477. jp nc,l02d9h ;09d4 Jump to error if parameter >= 2
  2478. ld (01226h),a ;09d7 Store drive control flags (0 or 1)
  2479. call sub_0680h ;09da Get next parameter from input
  2480. cp 04dh ;09dd Check if parameter < 77 (0x4D, max tracks)
  2481. jp nc,l02d9h ;09df Jump to error if parameter >= 77
  2482. ld (01225h),a ;09e2 Store drive parameter/selector (track/cylinder)
  2483. call sub_0680h ;09e5
  2484. or a ;09e8
  2485. jp z,l02d9h ;09e9
  2486. cp 01bh ;09ec
  2487. jp nc,l02d9h ;09ee
  2488. ld (01227h),a ;09f1
  2489. ld a,0ffh ;09f4
  2490. ld (011f7h),a ;09f6
  2491. ret ;09f9
  2492. ld hl,l0db1h ;09fa
  2493. jr l0a0ah ;09fd
  2494. call sub_065eh ;09ff
  2495. cp 04fh ;0a02
  2496. jp z,l089fh ;0a04
  2497. ld hl,l0dabh ;0a07
  2498. l0a0ah:
  2499. ld (011f8h),hl ;0a0a
  2500. ld hl,011f7h ;0a0d
  2501. xor a ;0a10
  2502. cp (hl) ;0a11
  2503. jp z,l02d9h ;0a12
  2504. ld (hl),a ;0a15
  2505. call l0de0h ;0a16
  2506. call sub_0680h ;0a19
  2507. push hl ;0a1c
  2508. call sub_0680h ;0a1d
  2509. pop de ;0a20
  2510. ex de,hl ;0a21
  2511. l0a22h:
  2512. or a ;0a22
  2513. inc hl ;0a23
  2514. sbc hl,de ;0a24
  2515. add hl,de ;0a26
  2516. dec hl ;0a27
  2517. ret nc ;0a28
  2518. ld (01200h),hl ;0a29
  2519. push de ;0a2c
  2520. push hl ;0a2d
  2521. ld hl,(011f8h) ;0a2e
  2522. call sub_0da8h ;0a31
  2523. jr nc,l0a60h ;0a34
  2524. ld hl,01227h ;0a36
  2525. ld a,(hl) ;0a39
  2526. inc a ;0a3a
  2527. ld (hl),a ;0a3b
  2528. cp 01bh ;0a3c
  2529. jr c,l0a51h ;0a3e
  2530. ld (hl),001h ;0a40
  2531. ld a,(01203h) ;0a42
  2532. or a ;0a45
  2533. jr z,l0a4dh ;0a46
  2534. dec hl ;0a48
  2535. ld a,(hl) ;0a49
  2536. xor 001h ;0a4a
  2537. ld (hl),a ;0a4c
  2538. l0a4dh:
  2539. jr nz,l0a51h ;0a4d
  2540. dec hl ;0a4f
  2541. inc (hl) ;0a50
  2542. l0a51h:
  2543. ld a,(01203h) ;0a51
  2544. or a ;0a54
  2545. ld hl,l0080h ;0a55
  2546. jr z,l0a5bh ;0a58
  2547. add hl,hl ;0a5a
  2548. l0a5bh:
  2549. pop de ;0a5b
  2550. add hl,de ;0a5c
  2551. pop de ;0a5d
  2552. jr l0a22h ;0a5e
  2553. l0a60h:
  2554. ld hl,l0a69h ;0a60
  2555. call l00fdh ;0a63
  2556. jp l02d9h ;0a66
  2557. l0a69h:
  2558. defb 0x44 ;0a69 D
  2559. defb 0x49 ;0a6a I
  2560. defb 0x53 ;0a6b S
  2561. defb 0x4b ;0a6c K
  2562. defb 0x20 ;0a6d
  2563. defb 0x45 ;0a6e E
  2564. defb 0x52 ;0a6f R
  2565. defb 0x52 ;0a70 R
  2566. defb 0x0d ;0a71 RETURN
  2567. defb 0x0a ;0a72 RETURN
  2568. defb 0x00 ;0a73
  2569. l0a74h:
  2570. call sub_0680h ;0a74 Get drive number parameter from user input
  2571. cp 004h ;0a77 Check if drive number >= 4 (invalid)
  2572. jp nc,l02d9h ;0a79 Jump to error if invalid drive number
  2573. ld (01202h),a ;0a7c Store current drive number (0-3)
  2574. call sub_0e43h ;0a7f Execute drive operation
  2575. jp nc,l0a60h ;0a82
  2576. ret ;0a85
  2577. l0a86h:
  2578. call sub_0acbh ;0a86
  2579. jp nc,l0a60h ;0a89
  2580. ret ;0a8c
  2581. ;==============================================================================
  2582. ; FLOPPY DISK CONTROLLER INITIALIZATION (0x0A8D)
  2583. ;==============================================================================
  2584. ; Purpose: Initialize NEC µPD765A floppy disk controller and Z80 DMA
  2585. ; Hardware Setup:
  2586. ; - Port 0xF6: FDC command/status register
  2587. ; - Port 0xF3: Z80 DMA control register (standalone Z80 DMA chip)
  2588. ; - Port 0xF0: FDC main status register
  2589. ; - Port 0xF4: FDC data register
  2590. ;
  2591. ; Z80 DMA Operations:
  2592. ; - Value 0x08: Enable Z80 DMA for floppy transfers
  2593. ; - Clear bit 3: Modify DMA transfer mode parameters
  2594. ; - Two-stage DMA setup for optimal transfer configuration
  2595. ;
  2596. ; Operation:
  2597. ; 1. Send "specify" command to FDC (configure timing parameters)
  2598. ; 2. Enable Z80 DMA controller for high-speed transfers
  2599. ; 3. Modify DMA transfer mode settings
  2600. ; 4. Wait for FDC ready status
  2601. ; 5. Configure for read/write operations
  2602. ;
  2603. ; Returns: Carry flag set if successful, clear if failed
  2604. ;==============================================================================
  2605. sub_0a8dh:
  2606. ld a,003h ;0a8d ; Load FDC "specify" command
  2607. out (0f6h),a ;0a8f ; Send command to FDC command register
  2608. ld a,008h ;0a91 ; Load Z80 DMA enable control value
  2609. out (0f3h),a ;0a93 ; Enable Z80 DMA for floppy operations
  2610. res 3,a ;0a95 ; Clear bit 3 (modify DMA transfer mode)
  2611. out (0f3h),a ;0a97 ; Update Z80 DMA control register
  2612. ld bc,sub_011ch ;0a99 ; Load timeout/delay parameters
  2613. call sub_0f0ah ;0a9c ; Call delay/timing routine
  2614. ld bc,l0000h ;0a9f ; Initialize counter for FDC status polling
  2615. ;==============================================================================
  2616. ; FDC STATUS POLLING LOOP (0x0AA2)
  2617. ;==============================================================================
  2618. ; Purpose: Wait for FDC to become ready for command execution
  2619. ; This is a critical timing loop that ensures the NEC µPD765A FDC is ready
  2620. ; to accept commands before proceeding with disk operations.
  2621. ;
  2622. ; Hardware Details:
  2623. ; - Port 0xF0: NEC µPD765A Main Status Register (MSR)
  2624. ; - Bit 7 of MSR: RQM (Request for Master) - indicates FDC ready state
  2625. ; - Timeout: 65536 iterations (BC=0x0000 decrements to 0xFFFF then 0x0000)
  2626. ;
  2627. ; Status Register Interpretation:
  2628. ; - MSR bit 7 = 1: FDC ready to accept commands/data
  2629. ; - MSR bit 7 = 0: FDC busy, continue polling
  2630. ; - Expected final state: MSR = 0x80 (only RQM bit set)
  2631. ;
  2632. ; Returns:
  2633. ; - Zero flag set: Timeout occurred (FDC never became ready)
  2634. ; - Zero flag clear: FDC ready, A contains final status
  2635. ; - A = 0x00: Success (MSR was exactly 0x80)
  2636. ; - A ≠ 0x00: Warning (MSR had unexpected bits set)
  2637. ;==============================================================================
  2638. l0aa2h:
  2639. dec bc ;0aa2 ; Decrement timeout counter
  2640. ld a,b ;0aa3 ; Load high byte of counter
  2641. or c ;0aa4 ; OR with low byte to test for zero
  2642. ret z ;0aa5 ; Return if timeout (BC reached 0x0000)
  2643. in a,(0f0h) ;0aa6 ; Read FDC Main Status Register
  2644. bit 7,a ;0aa8 ; Test RQM bit (Request for Master)
  2645. jr z,l0aa2h ;0aaa ; Loop if FDC not ready (RQM=0)
  2646. xor 080h ;0aac ; XOR with 0x80 to check if only RQM bit set
  2647. ret nz ;0aae ; Return if other status bits are set (error condition)
  2648. ;==============================================================================
  2649. ; FDC COMMAND EXECUTION SETUP (0x0AAF)
  2650. ;==============================================================================
  2651. ; Purpose: Execute FDC commands and wait for completion status
  2652. ; This section handles the command execution phase after the FDC is ready.
  2653. ; It coordinates with the NEC µPD765A to execute disk operations.
  2654. ;
  2655. ; Operation Flow:
  2656. ; 1. Call command execution routine (sub_0ba7h)
  2657. ; 2. Wait for command completion (status 0x80)
  2658. ; 3. Setup interrupt vector for DMA completion
  2659. ; 4. Configure DMA parameters for data transfer
  2660. ;
  2661. ; Hardware Configuration:
  2662. ; - Port 0xF4: FDC data register for parameter passing
  2663. ; - Port 0xF6: FDC command register
  2664. ; - Memory 0x1334: Interrupt vector table pointer
  2665. ; - Address 0x0BB6: DMA completion handler
  2666. ;
  2667. ; Status Codes:
  2668. ; - 0x80: Command execution successful, ready for data transfer
  2669. ; - Other: Command still executing or error condition
  2670. ;==============================================================================
  2671. l0aafh:
  2672. call sub_0ba7h ;0aaf ; Execute FDC command sequence
  2673. ret nc ;0ab2 ; Return if command execution failed
  2674. cp 080h ;0ab3 ; Check if execution status is 0x80 (success)
  2675. jr nz,l0aafh ;0ab5 ; Loop until command completes successfully
  2676. ld hl,l0bb6h ;0ab7 ; Load address of DMA completion handler
  2677. ld (01334h),hl ;0aba ; Store in interrupt vector table
  2678. ld a,030h ;0abd ; Load DMA control parameter
  2679. out (0f4h),a ;0abf ; Send to FDC data register
  2680. ld a,0d7h ;0ac1 ; Load FDC command parameter
  2681. out (0f6h),a ;0ac3 ; Send command to FDC
  2682. ld a,001h ;0ac5 ; Load final parameter
  2683. out (0f6h),a ;0ac7 ; Complete command sequence
  2684. scf ;0ac9 ; Set carry flag (success indicator)
  2685. ret ;0aca ; Return with success status
  2686. ;==============================================================================
  2687. ; FLOPPY DISK CONTROLLER MASTER ROUTINE (0x0ACB)
  2688. ;==============================================================================
  2689. ; Purpose: Main floppy disk operation coordinator
  2690. ; Calls sub-routines to:
  2691. ; 1. Initialize FDC and prepare for disk operations (sub_0a8dh)
  2692. ; 2. Execute disk read/write operations (sub_0ae9h)
  2693. ; 3. Loop through multiple drives/sectors (0x0AD8 loop)
  2694. ;
  2695. ; This appears to be the high-level disk I/O interface that coordinates
  2696. ; multiple disk operations across different drives (up to 4 drives/sectors)
  2697. ;==============================================================================
  2698. sub_0acbh:
  2699. call sub_0a8dh ;0acb ; Initialize FDC hardware and prepare for operation
  2700. ret nc ;0ace ; Return if FDC initialization failed
  2701. call sub_0ae9h ;0acf ; Execute the actual disk operation
  2702. ret nc ;0ad2
  2703. xor a ;0ad3 A = 0 (start with drive 0)
  2704. ld iy,01205h ;0ad4 Point IY to drive status table
  2705. l0ad8h: ; Multi-drive boot loop (tries drives 0,1,2,3)
  2706. push af ;0ad8 Save current drive number
  2707. ld (01202h),a ;0ad9 Set current drive number (0x1202)
  2708. call sub_0b1ch ;0adc Attempt to read boot sector from current drive
  2709. pop af ;0adf Restore drive number
  2710. inc iy ;0ae0 Move to next drive status entry
  2711. inc a ;0ae2 Next drive number (0→1→2→3)
  2712. cp 004h ;0ae3 Check if tried all 4 drives
  2713. jr nz,l0ad8h ;0ae5 Loop until all drives tested
  2714. scf ;0ae7 Set carry flag (success)
  2715. ret ;0ae8 Return from multi-drive scan
  2716. ;==============================================================================
  2717. ; FDC COMMAND PARAMETER SETUP (0x0AE9)
  2718. ;==============================================================================
  2719. ; Purpose: Prepare NEC µPD765A command parameters for disk operations
  2720. ; This routine builds the command parameter block required by the FDC to
  2721. ; execute read/write operations. The parameters are assembled from various
  2722. ; system variables and formatted according to NEC µPD765A specifications.
  2723. ;
  2724. ; Memory Layout - FDC Parameter Block (0x1213-0x1215):
  2725. ; • 0x1213: Command byte (0x03 = READ DATA command)
  2726. ; • 0x1214: Drive/Head selection and DMA control
  2727. ; • 0x1215: Track/Sector/Size parameters
  2728. ;
  2729. ; Parameter Sources:
  2730. ; • 0x120B: Drive geometry configuration (tracks, density)
  2731. ; • 0x120A: Head selection and DMA mode settings
  2732. ; • 0x1209: Sector size and format parameters
  2733. ; • 0x1204: Additional drive control flags
  2734. ;
  2735. ; NEC µPD765A Command Format:
  2736. ; - Bit manipulation ensures proper head selection and DMA modes
  2737. ; - Rotation and masking operations format multi-bit parameters
  2738. ; - Command block follows standard FDC command structure
  2739. ;==============================================================================
  2740. sub_0ae9h:
  2741. ld hl,01213h ;0ae9 ; Point to FDC command parameter block
  2742. push hl ;0aec ; Save base pointer for later use
  2743. ld (hl),003h ;0aed ; Set command byte: 0x03 = READ DATA command
  2744. inc hl ;0aef ; Move to drive/head parameter byte (0x1214)
  2745. ld a,(0120bh) ;0af0 ; Load drive geometry configuration
  2746. and 00fh ;0af3 ; Mask to get lower 4 bits (drive geometry)
  2747. sub 011h ;0af5 ; Subtract 0x11 (adjust for FDC format)
  2748. cpl ;0af7 ; Complement bits (invert for FDC encoding)
  2749. rlca ;0af8 ; Rotate left 4 positions to place in upper nibble
  2750. rlca ;0af9 ; (4 rotations = shift left 4 bits)
  2751. rlca ;0afa
  2752. rlca ;0afb
  2753. ld (hl),a ;0afc ; Store formatted drive parameter
  2754. ld a,(0120ah) ;0afd ; Load head selection and DMA mode
  2755. rrca ;0b00 ; Rotate right 4 positions to get lower nibble
  2756. rrca ;0b01 ; (4 rotations = shift right 4 bits)
  2757. rrca ;0b02
  2758. rrca ;0b03
  2759. and 00fh ;0b04 ; Mask to keep only lower 4 bits
  2760. or (hl) ;0b06 ; Combine with previous drive parameter
  2761. ld (hl),a ;0b07 ; Store combined drive/head parameter
  2762. inc hl ;0b08 ; Move to track/sector parameter byte (0x1215)
  2763. ld a,(01209h) ;0b09 ; Load sector size and format configuration
  2764. res 0,a ;0b0c ; Clear bit 0 (modify sector size encoding)
  2765. ld (hl),a ;0b0e ; Store modified sector parameter
  2766. ld a,(01204h) ;0b0f ; Load additional drive control flags
  2767. and 001h ;0b12 ; Mask to get only bit 0 (specific control flag)
  2768. or (hl) ;0b14 ; Combine with sector parameter
  2769. ld (hl),a ;0b15 ; Store final track/sector/control parameter
  2770. ld b,003h ;0b16 ; Set parameter count: 3 bytes for FDC command
  2771. pop hl ;0b18 ; Restore base pointer to command block
  2772. jp l0b64h ;0b19 ; Jump to FDC command execution routine
  2773. ;==============================================================================
  2774. ; FDC OPERATION WITH PARAMETER MANAGEMENT (0x0B1C)
  2775. ;==============================================================================
  2776. ; Purpose: Execute FDC operations with automatic parameter backup/restore
  2777. ; This routine provides a higher-level interface for disk operations that
  2778. ; temporarily modifies drive parameters and ensures they are properly restored
  2779. ; even if the operation fails.
  2780. ;
  2781. ; Operation Flow:
  2782. ; 1. Prepare FDC for operation (sub_0b33h)
  2783. ; 2. Backup current drive parameter (0x1225)
  2784. ; 3. Set temporary parameter (0x03) for operation
  2785. ; 4. Execute disk operation (sub_0c05h)
  2786. ; 5. Restore original parameter regardless of operation result
  2787. ; 6. Verify FDC is ready for next operation
  2788. ;
  2789. ; Memory Locations:
  2790. ; • 0x1225: Drive parameter/selector register (backed up and restored)
  2791. ; • Value 0x03: Temporary parameter setting for this operation type
  2792. ;
  2793. ; Error Handling:
  2794. ; - Parameters are restored even if disk operation fails
  2795. ; - Final FDC readiness check before returning
  2796. ; - Carry flag indicates overall operation success
  2797. ;==============================================================================
  2798. sub_0b1ch:
  2799. call sub_0b33h ;0b1c ; Prepare FDC for operation
  2800. ld a,(01225h) ;0b1f ; Load current drive parameter
  2801. push af ;0b22 ; Save parameter on stack for restoration
  2802. ld a,003h ;0b23 ; Load temporary parameter value
  2803. ld (01225h),a ;0b25 ; Set temporary drive parameter
  2804. call sub_0c05h ;0b28 ; Execute disk operation
  2805. pop af ;0b2b ; Restore original parameter from stack
  2806. ld (01225h),a ;0b2c ; Write back original drive parameter
  2807. call sub_0b33h ;0b2f ; Verify FDC readiness after operation
  2808. ret c ;0b32 ; Return if FDC is ready (carry set = success)
  2809. ;==============================================================================
  2810. ; FDC RECALIBRATE AND COMMAND SETUP (0x0B33)
  2811. ;==============================================================================
  2812. ; Purpose: Prepare FDC for operation with recalibrate command and status monitoring
  2813. ; This routine initializes the FDC to a known state and sets up a recalibrate
  2814. ; command for reliable disk operations. Used both before and after disk operations
  2815. ; to ensure the drive heads are properly positioned.
  2816. ;
  2817. ; Operation Sequence:
  2818. ; 1. Set drive status to 0xFF (reset/initialize state)
  2819. ; 2. Execute low-level FDC preparation (sub_0d2eh)
  2820. ; 3. Clear status flags and setup command parameters
  2821. ; 4. Configure RECALIBRATE command (0x07) with drive parameters
  2822. ; 5. Wait for command completion with timeout monitoring
  2823. ;
  2824. ; Hardware Details:
  2825. ; • IY register: Points to drive status table (set by caller)
  2826. ; • 0x1226: Drive control flags cleared during operation
  2827. ; • 0x1213: Command byte set to 0x07 (RECALIBRATE command)
  2828. ; • 0x124A: Operation completion status flag
  2829. ;
  2830. ; NEC µPD765A RECALIBRATE Command:
  2831. ; - Moves drive head to track 0 (cylinder 0)
  2832. ; - Establishes known reference position for subsequent seeks
  2833. ; - Required after drive selection or power-on sequences
  2834. ;==============================================================================
  2835. sub_0b33h:
  2836. ld (iy+000h),0ffh ;0b33 ; Set drive status to 0xFF (initialize state)
  2837. call sub_0d2eh ;0b37 ; Execute low-level FDC preparation routine
  2838. ret nc ;0b3a ; Return if FDC preparation failed (carry clear)
  2839. xor a ;0b3b ; Load 0 into accumulator
  2840. ld (01226h),a ;0b3c ; Clear drive control flags
  2841. ld hl,01213h ;0b3f ; Point to FDC command parameter block
  2842. push hl ;0b42 ; Save command block pointer
  2843. ld (hl),007h ;0b43 ; Set command byte: 0x07 = RECALIBRATE command
  2844. inc hl ;0b45 ; Move to next parameter byte
  2845. call sub_0b7bh ;0b46 ; Setup additional command parameters
  2846. xor a ;0b49 ; Load 0 into accumulator
  2847. ld (0124ah),a ;0b4a ; Clear operation completion status flag
  2848. ld b,002h ;0b4d ; Set parameter count: 2 bytes for RECALIBRATE command
  2849. pop hl ;0b4f ; Restore command block pointer
  2850. call l0b64h ;0b50 ; Send command to FDC
  2851. ret nc ;0b53 ; Return if command transmission failed
  2852. ;==============================================================================
  2853. ; RECALIBRATE COMPLETION MONITORING (0x0B54)
  2854. ;==============================================================================
  2855. ; Purpose: Wait for RECALIBRATE command completion with status validation
  2856. ; Monitors the operation completion flag and validates the final status to
  2857. ; ensure the recalibrate operation completed successfully.
  2858. ;
  2859. ; Status Monitoring:
  2860. ; • 0x124A: Operation completion flag (0=pending, non-zero=completed)
  2861. ; • Expected completion status: 0x20 (recalibrate successful)
  2862. ; • Status mask 0xF8: Checks upper 5 bits for error conditions
  2863. ;
  2864. ; Returns:
  2865. ; • Carry set: RECALIBRATE completed successfully, drive ready
  2866. ; • Carry clear: RECALIBRATE failed or timeout occurred
  2867. ;==============================================================================
  2868. l0b54h:
  2869. ld a,(0124ah) ;0b54 ; Read operation completion status
  2870. or a ;0b57 ; Test if operation completed (non-zero)
  2871. jr z,l0b54h ;0b58 ; Loop until completion flag is set
  2872. and 0f8h ;0b5a ; Mask upper 5 bits to check for errors
  2873. xor 020h ;0b5c ; Compare with expected success status (0x20)
  2874. ret nz ;0b5e ; Return with carry clear if not successful
  2875. ld (iy+000h),a ;0b5f ; Store final status (0x00) in drive status
  2876. scf ;0b62 ; Set carry flag (operation successful)
  2877. ret ;0b63 ; Return with success status
  2878. ;==============================================================================
  2879. ; FLOPPY CONTROLLER I/O TRANSMITTER (0x0B64)
  2880. ;==============================================================================
  2881. ; Purpose: Send command bytes to floppy controller via I/O ports
  2882. ; Input: HL = pointer to command buffer, B = byte count, C = 0xF1 (data port)
  2883. ;
  2884. ; Function:
  2885. ; - Waits for floppy controller ready (port 0xF0 status bits 0-4 clear)
  2886. ; - Polls bit 7 of status port 0xF0 for command ready signal
  2887. ; - Checks bit 6 for transfer direction/error condition
  2888. ; - Uses OUTI instruction to send bytes from buffer to port 0xF1
  2889. ; - Continues until all B bytes transmitted
  2890. ;
  2891. ; Ports Used:
  2892. ; - 0xF0: Floppy controller status register
  2893. ; - Bit 7: Command ready flag
  2894. ; - Bit 6: Direction/error flag
  2895. ; - Bits 0-4: Various status conditions
  2896. ; - 0xF1: Floppy controller data register (via register C)
  2897. ;
  2898. ; Returns: Carry set on success, clear on error
  2899. ;==============================================================================
  2900. l0b64h:
  2901. ld c,0f1h ;0b64
  2902. l0b66h:
  2903. in a,(0f0h) ;0b66
  2904. and 01fh ;0b68
  2905. jr nz,l0b66h ;0b6a
  2906. l0b6ch:
  2907. in a,(0f0h) ;0b6c
  2908. bit 7,a ;0b6e
  2909. jr z,l0b6ch ;0b70
  2910. and 040h ;0b72
  2911. ret nz ;0b74
  2912. outi ;0b75
  2913. jr nz,l0b6ch ;0b77
  2914. scf ;0b79
  2915. ret ;0b7a
  2916. ;==============================================================================
  2917. ; FLOPPY COMMAND PARAMETER BUILDER (0x0B7B)
  2918. ;==============================================================================
  2919. ; Purpose: Build drive and head selection parameters for floppy commands
  2920. ; Input: HL = pointer to command buffer (pointing to parameter byte location)
  2921. ;
  2922. ; Function:
  2923. ; - Reads drive selection from 0x1226, rotates left twice (bits 6-7 → bits 0-1)
  2924. ; - Combines with head selection from 0x1202
  2925. ; - Stores combined drive/head parameter in command buffer
  2926. ; - Advances buffer pointer for next parameter
  2927. ;
  2928. ; Parameter Format:
  2929. ; - Bits 0-1: Drive number (from 0x1226 bits 6-7)
  2930. ; - Other bits: Head selection and flags (from 0x1202)
  2931. ;
  2932. ; Memory Locations:
  2933. ; - 0x1226: Drive selection (bits 6-7 used)
  2934. ; - 0x1202: Head selection and other flags
  2935. ;==============================================================================
  2936. sub_0b7bh:
  2937. ld a,(01226h) ;0b7b
  2938. rlca ;0b7e
  2939. rlca ;0b7f
  2940. ld (hl),a ;0b80
  2941. ld a,(01202h) ;0b81
  2942. or (hl) ;0b84
  2943. ld (hl),a ;0b85
  2944. inc hl ;0b86
  2945. ret ;0b87
  2946. ;==============================================================================
  2947. ; FLOPPY CONTROLLER RESPONSE READER (0x0B88)
  2948. ;==============================================================================
  2949. ; Purpose: Read response bytes from floppy controller after command execution
  2950. ;
  2951. ; Function:
  2952. ; - Sets up response buffer at 0x121C
  2953. ; - Uses port 0xF1 for data input (via register C)
  2954. ; - Implements timeout loop (B=0x0A iterations)
  2955. ; - Polls port 0xF0 status register:
  2956. ; - Bit 4: Data ready flag
  2957. ; - Bit 7: Controller ready flag
  2958. ; - Bit 6: Direction/completion flag
  2959. ; - Uses INI instruction to read bytes from port 0xF1 to buffer
  2960. ; - Returns first response byte and sets carry on success
  2961. ;
  2962. ; Timeout Handling:
  2963. ; - 10-iteration outer loop for timeout protection
  2964. ; - Inner delay loop for timing control
  2965. ; - Returns with carry clear on timeout
  2966. ;
  2967. ; Returns: A = first response byte, Carry = success flag
  2968. ;==============================================================================
  2969. l0b88h:
  2970. ld hl,0121ch ;0b88
  2971. ld c,0f1h ;0b8b
  2972. l0b8dh:
  2973. ld b,00ah ;0b8d
  2974. l0b8fh:
  2975. djnz l0b8fh ;0b8f
  2976. l0b91h:
  2977. in a,(0f0h) ;0b91
  2978. bit 4,a ;0b93
  2979. jr z,l0ba2h ;0b95
  2980. bit 7,a ;0b97
  2981. jr z,l0b91h ;0b99
  2982. and 040h ;0b9b
  2983. ret z ;0b9d
  2984. ini ;0b9e
  2985. jr l0b8dh ;0ba0
  2986. l0ba2h:
  2987. ld a,(0121ch) ;0ba2
  2988. scf ;0ba5
  2989. ret ;0ba6
  2990. sub_0ba7h:
  2991. ld hl,01213h ;0ba7
  2992. ld (hl),008h ;0baa
  2993. ld bc,001f1h ;0bac
  2994. call l0b6ch ;0baf
  2995. ret nc ;0bb2
  2996. jp l0b88h ;0bb3
  2997. l0bb6h:
  2998. push af ;0bb6
  2999. push hl ;0bb7
  3000. push bc ;0bb8
  3001. push de ;0bb9
  3002. l0bbah:
  3003. in a,(0f0h) ;0bba
  3004. bit 7,a ;0bbc
  3005. jr z,l0bbah ;0bbe
  3006. bit 6,a ;0bc0
  3007. jr z,l0bd9h ;0bc2
  3008. ld a,083h ;0bc4
  3009. out (0fch),a ;0bc6
  3010. ld (0124bh),a ;0bc8
  3011. call l0b88h ;0bcb
  3012. ld hl,0121ch ;0bce
  3013. ld de,0120ch ;0bd1
  3014. ld bc,00007h ;0bd4
  3015. ldir ;0bd7
  3016. l0bd9h:
  3017. call sub_0ba7h ;0bd9
  3018. jr nc,l0bebh ;0bdc
  3019. cp 080h ;0bde
  3020. jr z,l0bebh ;0be0
  3021. bit 5,a ;0be2
  3022. jr z,l0bd9h ;0be4
  3023. ld (0124ah),a ;0be6
  3024. jr l0bd9h ;0be9
  3025. l0bebh:
  3026. pop de ;0beb
  3027. pop bc ;0bec
  3028. pop hl ;0bed
  3029. pop af ;0bee
  3030. ei ;0bef
  3031. reti ;0bf0
  3032. ;==============================================================================
  3033. ; FLOPPY CONTROLLER COMMAND SENDER (0x0BF2)
  3034. ;==============================================================================
  3035. ; Purpose: Send command 0x04 to floppy disk controller and read response
  3036. ;
  3037. ; Function:
  3038. ; - Sets up command buffer at 0x1213 with command 0x04
  3039. ; - Calls sub_0b7bh to add drive/head parameters
  3040. ; - Calls l0b64h to transmit command via ports 0xF0/0xF1
  3041. ; - Returns status in accumulator for caller processing
  3042. ;
  3043. ; Command 0x04: Likely "Read Status" or "Sense Drive Status" command
  3044. ; Buffer: 0x1213 = command, 0x1214+ = parameters
  3045. ; I/O Ports: 0xF0 (status check), 0xF1 (data transfer)
  3046. ;==============================================================================
  3047. sub_0bf2h:
  3048. ld hl,01213h ;0bf2
  3049. push hl ;0bf5
  3050. ld (hl),004h ;0bf6
  3051. inc hl ;0bf8
  3052. call sub_0b7bh ;0bf9
  3053. pop hl ;0bfc
  3054. ld b,002h ;0bfd
  3055. call l0b64h ;0bff
  3056. jp l0b88h ;0c02
  3057. sub_0c05h:
  3058. call sub_0d2eh ;0c05
  3059. ret nc ;0c08
  3060. ld hl,01213h ;0c09
  3061. push hl ;0c0c
  3062. ld (hl),00fh ;0c0d
  3063. inc hl ;0c0f
  3064. call sub_0b7bh ;0c10
  3065. ld a,(01225h) ;0c13
  3066. ld (hl),a ;0c16
  3067. xor a ;0c17
  3068. ld (0124ah),a ;0c18
  3069. pop hl ;0c1b
  3070. ld b,003h ;0c1c
  3071. call l0b64h ;0c1e
  3072. ret nc ;0c21
  3073. l0c22h:
  3074. ld a,(0124ah) ;0c22
  3075. or a ;0c25
  3076. jr z,l0c22h ;0c26
  3077. and 0f8h ;0c28
  3078. xor 020h ;0c2a
  3079. ret nz ;0c2c
  3080. scf ;0c2d
  3081. ret ;0c2e
  3082. l0c2fh:
  3083. ld a,006h ;0c2f
  3084. jr l0c35h ;0c31
  3085. l0c33h:
  3086. ld a,005h ;0c33
  3087. l0c35h:
  3088. ld hl,01223h ;0c35
  3089. ld (hl),a ;0c38
  3090. ld a,(01204h) ;0c39
  3091. or a ;0c3c
  3092. ret nz ;0c3d
  3093. call sub_0c6ah ;0c3e
  3094. call sub_0b7bh ;0c41
  3095. call sub_0c73h ;0c44
  3096. call sub_0d2eh ;0c47
  3097. ret nc ;0c4a
  3098. call sub_0ca0h ;0c4b
  3099. call sub_0cc5h ;0c4e
  3100. xor a ;0c51
  3101. ld (0124bh),a ;0c52
  3102. ld hl,01223h ;0c55
  3103. ld b,009h ;0c58
  3104. call l0b64h ;0c5a
  3105. ret nc ;0c5d
  3106. call sub_0d11h ;0c5e
  3107. ret nc ;0c61
  3108. ld a,(0120ch) ;0c62
  3109. and 0c0h ;0c65
  3110. ret nz ;0c67
  3111. scf ;0c68
  3112. ret ;0c69
  3113. sub_0c6ah:
  3114. ld a,(01203h) ;0c6a
  3115. rrca ;0c6d
  3116. rrca ;0c6e
  3117. or (hl) ;0c6f
  3118. ld (hl),a ;0c70
  3119. inc hl ;0c71
  3120. ret ;0c72
  3121. sub_0c73h:
  3122. ld hl,l0c90h ;0c73
  3123. ld de,01228h ;0c76
  3124. jr l0c81h ;0c79
  3125. sub_0c7bh:
  3126. ld hl,l0c98h ;0c7b
  3127. ld de,01215h ;0c7e
  3128. l0c81h:
  3129. ld a,(01203h) ;0c81
  3130. add a,a ;0c84
  3131. add a,a ;0c85
  3132. ld c,a ;0c86
  3133. ld b,000h ;0c87
  3134. add hl,bc ;0c89
  3135. ld bc,00004h ;0c8a
  3136. ldir ;0c8d
  3137. ret ;0c8f
  3138. l0c90h:
  3139. nop ;0c90
  3140. ld a,(de) ;0c91
  3141. rlca ;0c92
  3142. add a,b ;0c93
  3143. ld bc,l0e1ah ;0c94
  3144. rst 38h ;0c97
  3145. l0c98h:
  3146. nop ;0c98
  3147. ld a,(de) ;0c99
  3148. dec de ;0c9a
  3149. push hl ;0c9b
  3150. ld bc,0361ah ;0c9c
  3151. push hl ;0c9f
  3152. sub_0ca0h:
  3153. ld a,(01203h) ;0ca0
  3154. inc a ;0ca3
  3155. ld d,a ;0ca4
  3156. ld e,080h ;0ca5
  3157. jr l0cadh ;0ca7
  3158. sub_0ca9h:
  3159. ld d,001h ;0ca9
  3160. ld e,068h ;0cab
  3161. l0cadh:
  3162. ld c,0f4h ;0cad
  3163. ld a,047h ;0caf
  3164. out (c),a ;0cb1
  3165. out (c),d ;0cb3
  3166. inc c ;0cb5
  3167. ld a,057h ;0cb6
  3168. out (c),a ;0cb8
  3169. out (c),e ;0cba
  3170. ret ;0cbc
  3171. sub_0cbdh:
  3172. ld hl,00068h ;0cbd
  3173. ld a,(01213h) ;0cc0
  3174. jr l0cd2h ;0cc3
  3175. sub_0cc5h:
  3176. ld hl,l0080h ;0cc5
  3177. ld a,(01203h) ;0cc8
  3178. or a ;0ccb
  3179. jr z,l0ccfh ;0ccc
  3180. add hl,hl ;0cce
  3181. l0ccfh:
  3182. ld a,(01223h) ;0ccf
  3183. l0cd2h:
  3184. dec hl ;0cd2
  3185. push hl ;0cd3
  3186. ld hl,l0cfbh ;0cd4
  3187. ld de,0122ch ;0cd7
  3188. ld bc,00016h ;0cda
  3189. ldir ;0cdd
  3190. pop hl ;0cdf
  3191. ld (01235h),hl ;0ce0
  3192. ld hl,(01200h) ;0ce3
  3193. ld (01233h),hl ;0ce6
  3194. and 001h ;0ce9
  3195. rlca ;0ceb
  3196. rlca ;0cec
  3197. ld hl,0123eh ;0ced
  3198. or (hl) ;0cf0
  3199. ld (hl),a ;0cf1
  3200. ld hl,0122ch ;0cf2
  3201. ld bc,016fch ;0cf5
  3202. otir ;0cf8
  3203. ret ;0cfa
  3204. l0cfbh:
  3205. jp 0c3c3h ;0cfb
  3206. jp 0c3c3h ;0cfe
  3207. ld a,c ;0d01
  3208. nop ;0d02
  3209. nop ;0d03
  3210. nop ;0d04
  3211. nop ;0d05
  3212. inc d ;0d06
  3213. jr z,$-121 ;0d07
  3214. jp p,08382h ;0d09
  3215. rst 8 ;0d0c
  3216. ld bc,0cf83h ;0d0d
  3217. add a,a ;0d10
  3218. sub_0d11h:
  3219. ld bc,l0000h ;0d11
  3220. l0d14h:
  3221. ld a,(0124bh) ;0d14
  3222. or a ;0d17
  3223. scf ;0d18
  3224. ret nz ;0d19
  3225. ld a,(0124bh) ;0d1a
  3226. or a ;0d1d
  3227. scf ;0d1e
  3228. ret nz ;0d1f
  3229. djnz l0d14h ;0d20
  3230. dec c ;0d22
  3231. jr nz,l0d14h ;0d23
  3232. ld a,083h ;0d25
  3233. out (0fch),a ;0d27
  3234. call sub_0a8dh ;0d29
  3235. or a ;0d2c
  3236. ret ;0d2d
  3237. sub_0d2eh:
  3238. call sub_0bf2h ;0d2e
  3239. ret nc ;0d31
  3240. and 020h ;0d32
  3241. ret z ;0d34
  3242. scf ;0d35
  3243. ret ;0d36
  3244. sub_0d37h:
  3245. call sub_0d2eh ;0d37
  3246. ret nc ;0d3a
  3247. ld b,002h ;0d3b
  3248. l0d3dh:
  3249. push bc ;0d3d
  3250. call sub_0c05h ;0d3e
  3251. pop bc ;0d41
  3252. ld a,(01225h) ;0d42
  3253. ld (iy+000h),a ;0d45
  3254. ret c ;0d48
  3255. push bc ;0d49
  3256. call sub_0b1ch ;0d4a
  3257. pop bc ;0d4d
  3258. ret nc ;0d4e
  3259. ld hl,(01244h) ;0d4f
  3260. inc hl ;0d52
  3261. ld (01244h),hl ;0d53
  3262. djnz l0d3dh ;0d56
  3263. or a ;0d58
  3264. ret ;0d59
  3265. l0d5ah:
  3266. call sub_0bf2h ;0d5a
  3267. xor 020h ;0d5d
  3268. and 060h ;0d5f
  3269. ret nz ;0d61
  3270. ld hl,l0c33h ;0d62
  3271. jr l0d6eh ;0d65
  3272. l0d67h:
  3273. call sub_0d2eh ;0d67
  3274. ret nc ;0d6a
  3275. ld hl,l0c2fh ;0d6b
  3276. l0d6eh:
  3277. ld b,005h ;0d6e
  3278. l0d70h:
  3279. push bc ;0d70
  3280. push hl ;0d71
  3281. call sub_0da8h ;0d72
  3282. pop hl ;0d75
  3283. pop bc ;0d76
  3284. ret c ;0d77
  3285. push hl ;0d78
  3286. ld a,(0120dh) ;0d79
  3287. and 085h ;0d7c
  3288. jr z,l0d87h ;0d7e
  3289. ld hl,(01246h) ;0d80
  3290. inc hl ;0d83
  3291. ld (01246h),hl ;0d84
  3292. l0d87h:
  3293. ld a,(0120dh) ;0d87
  3294. and 020h ;0d8a
  3295. jr z,l0d95h ;0d8c
  3296. ld hl,(01248h) ;0d8e
  3297. inc hl ;0d91
  3298. ld (01248h),hl ;0d92
  3299. l0d95h:
  3300. ld a,(0120eh) ;0d95
  3301. and 012h ;0d98
  3302. jr z,l0da3h ;0d9a
  3303. ld hl,(01244h) ;0d9c
  3304. inc hl ;0d9f
  3305. ld (01244h),hl ;0da0
  3306. l0da3h:
  3307. pop hl ;0da3
  3308. djnz l0d70h ;0da4
  3309. or a ;0da6
  3310. ret ;0da7
  3311. sub_0da8h:
  3312. jp (hl) ;0da8
  3313. sub_0da9h:
  3314. jp (ix) ;0da9
  3315. l0dabh:
  3316. ld ix,l0d67h ;0dab
  3317. jr l0db5h ;0daf
  3318. l0db1h:
  3319. ld ix,l0d5ah ;0db1
  3320. l0db5h:
  3321. ld iy,01205h ;0db5
  3322. ld b,002h ;0db9
  3323. ld a,(01202h) ;0dbb
  3324. ld e,a ;0dbe
  3325. ld d,000h ;0dbf
  3326. add iy,de ;0dc1
  3327. ld a,(01225h) ;0dc3
  3328. cp (iy+000h) ;0dc6
  3329. jr z,l0dd1h ;0dc9
  3330. l0dcbh:
  3331. push bc ;0dcb
  3332. call sub_0d37h ;0dcc
  3333. pop bc ;0dcf
  3334. ret nc ;0dd0
  3335. l0dd1h:
  3336. push bc ;0dd1
  3337. call sub_0da9h ;0dd2
  3338. pop bc ;0dd5
  3339. ret c ;0dd6
  3340. push bc ;0dd7
  3341. call sub_0b1ch ;0dd8
  3342. pop bc ;0ddb
  3343. djnz l0dcbh ;0ddc
  3344. or a ;0dde
  3345. ret ;0ddf
  3346. ;==============================================================================
  3347. ; FLOPPY DISK STATUS CHECK ROUTINE (0x0DE0)
  3348. ;==============================================================================
  3349. ; Purpose: Check floppy disk controller status and extract specific status bit
  3350. ; Entry Point: l0de0h - Called from interrupt vector 0x000B and multiple locations
  3351. ;
  3352. ; Function:
  3353. ; - Sends status command (0x04) to floppy controller via ports 0xF0/0xF1
  3354. ; - Reads back status response from floppy controller
  3355. ; - Extracts bit 3 from status (rotated to bit 0)
  3356. ; - Stores extracted bit at memory location 0x1203
  3357. ;
  3358. ; Status bit extracted (original bit 3) likely indicates:
  3359. ; - Drive ready condition, Track 0 detection, Write protect, or Index pulse
  3360. ;
  3361. ; Used as: Interrupt service routine and general floppy status check
  3362. ; Ports: 0xF0 (status), 0xF1 (data), 0xFC (control)
  3363. ;==============================================================================
  3364. l0de0h:
  3365. call sub_0bf2h ;0de0
  3366. rrca ;0de3
  3367. rrca ;0de4
  3368. rrca ;0de5
  3369. and 001h ;0de6
  3370. ld (01203h),a ;0de8
  3371. ret ;0deb
  3372. sub_0dech:
  3373. call sub_0d2eh ;0dec
  3374. ret nc ;0def
  3375. ld hl,01213h ;0df0
  3376. ld (hl),00dh ;0df3
  3377. call sub_0c6ah ;0df5
  3378. call sub_0b7bh ;0df8
  3379. call sub_0e1fh ;0dfb
  3380. call sub_0ca9h ;0dfe
  3381. call sub_0cbdh ;0e01
  3382. xor a ;0e04
  3383. ld (0124bh),a ;0e05
  3384. ld hl,01213h ;0e08
  3385. ld b,006h ;0e0b
  3386. call l0b64h ;0e0d
  3387. ret nc ;0e10
  3388. l0e11h:
  3389. ld a,(0124bh) ;0e11
  3390. or a ;0e14
  3391. jr z,l0e11h ;0e15
  3392. ld a,(0120ch) ;0e17
  3393. l0e1ah:
  3394. and 0c0h ;0e1a
  3395. ret nz ;0e1c
  3396. scf ;0e1d
  3397. ret ;0e1e
  3398. sub_0e1fh:
  3399. call sub_0c7bh ;0e1f
  3400. ld a,(01203h) ;0e22
  3401. ld e,a ;0e25
  3402. ld a,(01226h) ;0e26
  3403. ld c,a ;0e29
  3404. ld a,(01225h) ;0e2a
  3405. ld d,001h ;0e2d
  3406. ld b,01ah ;0e2f
  3407. ld hl,01000h ;0e31
  3408. ld (01200h),hl ;0e34
  3409. l0e37h:
  3410. ld (hl),a ;0e37
  3411. inc hl ;0e38
  3412. ld (hl),c ;0e39
  3413. inc hl ;0e3a
  3414. ld (hl),d ;0e3b
  3415. inc hl ;0e3c
  3416. ld (hl),e ;0e3d
  3417. inc hl ;0e3e
  3418. inc d ;0e3f
  3419. djnz l0e37h ;0e40
  3420. ret ;0e42
  3421. sub_0e43h:
  3422. call sub_0bf2h ;0e43
  3423. xor 020h ;0e46
  3424. and 060h ;0e48
  3425. ret nz ;0e4a
  3426. call l0de0h ;0e4b
  3427. ld iy,01205h ;0e4e
  3428. ld a,(01202h) ;0e52
  3429. ld e,a ;0e55
  3430. ld d,000h ;0e56
  3431. add iy,de ;0e58
  3432. call sub_0b1ch ;0e5a
  3433. ret nc ;0e5d
  3434. xor a ;0e5e
  3435. ld (01225h),a ;0e5f
  3436. l0e62h:
  3437. call sub_0c05h ;0e62
  3438. ret nc ;0e65
  3439. xor a ;0e66
  3440. ld (01226h),a ;0e67
  3441. call sub_0dech ;0e6a
  3442. ret nc ;0e6d
  3443. ld a,(01203h) ;0e6e
  3444. or a ;0e71
  3445. jr z,l0e7bh ;0e72
  3446. ld (01226h),a ;0e74
  3447. call sub_0dech ;0e77
  3448. ret nc ;0e7a
  3449. l0e7bh:
  3450. ld a,(01225h) ;0e7b
  3451. inc a ;0e7e
  3452. ld (01225h),a ;0e7f
  3453. cp 04dh ;0e82
  3454. jr c,l0e62h ;0e84
  3455. jp sub_0b1ch ;0e86
  3456. sub_0e89h:
  3457. call sub_0ea2h ;0e89
  3458. call nz,sub_0f6bh ;0e8c
  3459. call sub_0ecch ;0e8f
  3460. call nz,sub_0f70h ;0e92
  3461. call sub_0a8dh ;0e95
  3462. call nc,sub_0f75h ;0e98
  3463. call sub_0f10h ;0e9b
  3464. call nz,sub_0f7ah ;0e9e
  3465. ret ;0ea1
  3466. ;==============================================================================
  3467. ; RAM MEMORY DIAGNOSTIC TEST (0x0EA2)
  3468. ;==============================================================================
  3469. ; Purpose: Test available RAM and detect memory boundaries
  3470. ; Tests RAM starting from 0x1400 (above 1KB base RAM) to find additional memory
  3471. ;
  3472. ; Memory Layout:
  3473. ; - 0x0000-0x0FFF: EPROM
  3474. ; - 0x1000-0x13FF: Base 1KB RAM (1024 bytes)
  3475. ; - 0x1400+: Extended RAM (if present) or memory-mapped I/O
  3476. ;
  3477. ; Test Method:
  3478. ; - Starts at 0x1400 (beyond base 1KB RAM)
  3479. ; - Tests each location with multiple bit patterns
  3480. ; - Continues until test fails (indicating no more RAM or different hardware)
  3481. ;
  3482. ; Purpose: Detect extended RAM modules or memory-mapped peripherals
  3483. ; Returns: Z flag clear when memory boundary reached, Z set if wraps to 0x0000
  3484. ;==============================================================================
  3485. sub_0ea2h:
  3486. ld de,l0000h ;0ea2 Set DE = 0x0000 (used for wrap-around detection)
  3487. ld hl,01400h ;0ea5 Start at 0x1400 (beyond 1KB base RAM)
  3488. ; Test each memory location above base RAM
  3489. l0ea8h:
  3490. call sub_0eb3h ;0ea8 Test current memory location with bit patterns
  3491. ret nz ;0eab Return when memory test fails (found boundary)
  3492. inc hl ;0eac Move to next higher memory address
  3493. sbc hl,de ;0ead Check if wrapped around to 0x0000
  3494. add hl,de ;0eaf Restore HL value
  3495. jr nz,l0ea8h ;0eb0 Continue until boundary found or wrap-around
  3496. ret ;0eb2 Return if wrapped to 0x0000 (full 64K RAM)
  3497. ;==============================================================================
  3498. ; MEMORY LOCATION TEST WITH MULTIPLE PATTERNS (0x0EB3)
  3499. ;==============================================================================
  3500. ; Purpose: Test single memory location with all bit patterns from test table
  3501. ; Input: HL = memory address to test
  3502. ;
  3503. ; Test Procedure:
  3504. ; 1. Load test pattern count and pattern table pointer
  3505. ; 2. For each pattern: save original data, write pattern, read back, verify
  3506. ; 3. Restore original data, test next pattern
  3507. ; 4. Interrupts disabled during write/read/verify to prevent corruption
  3508. ;
  3509. ; Test Patterns (at l0ec9h):
  3510. ; - 0x02: Tests bit 1 (and others as 0)
  3511. ; - 0x55: Alternating bits pattern (01010101)
  3512. ; - 0xAA: Inverse alternating pattern (10101010)
  3513. ;
  3514. ; Returns: Z flag set if all patterns pass, Z clear if any pattern fails
  3515. ;==============================================================================
  3516. sub_0eb3h:
  3517. ld ix,l0ec9h ;0eb3 Point IX to test pattern table
  3518. ld b,(ix+000h) ;0eb7 Load pattern count (first byte of table)
  3519. ; Test each pattern from the table
  3520. l0ebah:
  3521. inc ix ;0eba Advance to next test pattern
  3522. ld a,(ix+000h) ;0ebc Load test pattern into A
  3523. ld c,(hl) ;0ebf Save original memory contents in C
  3524. di ;0ec0 Disable interrupts (critical memory test section)
  3525. ld (hl),a ;0ec1 Write test pattern to memory
  3526. cp (hl) ;0ec2 Read back and compare with expected pattern
  3527. ld (hl),c ;0ec3 Restore original memory contents
  3528. ei ;0ec4 Re-enable interrupts
  3529. ret nz ;0ec5 Return with error if pattern didn't match
  3530. djnz l0ebah ;0ec6 Test next pattern (decrement B, loop if not zero)
  3531. ret ;0ec8 Return with Z set (all patterns passed)
  3532. ;==============================================================================
  3533. ; MEMORY TEST PATTERN TABLE (0x0EC9)
  3534. ;==============================================================================
  3535. ; Format: [Count] [Pattern1] [Pattern2] [Pattern3]
  3536. ; These patterns test different failure modes:
  3537. ; - 0x02: Tests if bit 1 can be set while others stay clear
  3538. ; - 0x55: Tests alternating bit pattern (detects adjacent bit interference)
  3539. ; - 0xAA: Inverse alternating (detects stuck bits and crosstalk)
  3540. ;==============================================================================
  3541. l0ec9h:
  3542. defb 0x03 ;0ec9 Number of test patterns
  3543. defb 0x55 ;0eca Test pattern 1: 01010101 (alternating bits)
  3544. defb 0xAA ;0ecb Test pattern 2: 10101010 (inverse alternating)
  3545. sub_0ecch:
  3546. ld hl,l0f3fh ;0ecc
  3547. ld (01330h),hl ;0ecf
  3548. ld (01332h),hl ;0ed2
  3549. ld (01334h),hl ;0ed5
  3550. ld (01336h),hl ;0ed8
  3551. ld c,0f4h ;0edb
  3552. ld a,030h ;0edd
  3553. out (c),a ;0edf
  3554. l0ee1h:
  3555. ld a,0ffh ;0ee1
  3556. ld (01103h),a ;0ee3
  3557. call sub_0f01h ;0ee6
  3558. push bc ;0ee9
  3559. ld bc,01900h ;0eea
  3560. call sub_0f0ah ;0eed
  3561. pop bc ;0ef0
  3562. ld a,003h ;0ef1
  3563. out (c),a ;0ef3
  3564. ld a,(01103h) ;0ef5
  3565. or a ;0ef8
  3566. ret nz ;0ef9
  3567. inc c ;0efa
  3568. ld a,c ;0efb
  3569. cp 0f8h ;0efc
  3570. jr nz,l0ee1h ;0efe
  3571. ret ;0f00
  3572. sub_0f01h:
  3573. ld a,028h ;0f01
  3574. ld b,087h ;0f03
  3575. out (c),b ;0f05
  3576. out (c),a ;0f07
  3577. ret ;0f09
  3578. ; --------------------------------------------------
  3579. ; sub_0f0ah: Wait/Delay Loop
  3580. ; Burns CPU cycles by decrementing BC until it reaches zero.
  3581. ; Used for timing delays, hardware settling, or pacing init steps.
  3582. ; Entry: BC = delay value
  3583. ; Exit: BC = 0
  3584. ; No hardware interaction, pure software delay
  3585. ; --------------------------------------------------
  3586. sub_0f0ah:
  3587. dec bc ;0f0a
  3588. ld a,c ;0f0b
  3589. or b ;0f0c
  3590. jr nz,sub_0f0ah ;0f0d
  3591. ret ;0f0f
  3592. ; --------------------------------------------------
  3593. ; sub_0f10h: Hardware Initialization & Memory Test Routine
  3594. ; - Clears accumulator and sets 01204h to 0
  3595. ; - Sets up pointer at 0133ch to l0f48h
  3596. ; - Sets 01103h to 0xFF (error/status flag)
  3597. ; - Calls setup_peripherals_01a1h with l0f55h (hardware setup)
  3598. ; - Waits using delay loop (sub_0f0ah, BC=0x1000)
  3599. ; - If 01103h is nonzero, returns (error)
  3600. ; - If zero, compares 256 bytes at HL and DE (memory test), returns if mismatch
  3601. ; Called at 0e9b as part of system initialization/test sequence
  3602. ; --------------------------------------------------
  3603. sub_0f10h:
  3604. xor a ;0f10
  3605. ld (01204h),a ;0f11
  3606. ld hl,l0f48h ;0f14
  3607. ld (0133ch),hl ;0f17
  3608. dec a ;0f1a
  3609. ld (01103h),a ;0f1b
  3610. ld hl,l0f55h ;0f1e
  3611. call setup_peripherals_01a1h ;0f21
  3612. ld bc,01000h ;0f24
  3613. call sub_0f0ah ;0f27
  3614. ld a,(01103h) ;0f2a
  3615. or a ;0f2d
  3616. ret nz ;0f2e
  3617. ld hl,01000h ;0f2f
  3618. ld de,l0000h ;0f32
  3619. ld b,0ffh ;0f35
  3620. l0f37h:
  3621. ld a,(de) ;0f37
  3622. cp (hl) ;0f38
  3623. ret nz ;0f39
  3624. inc hl ;0f3a
  3625. inc de ;0f3b
  3626. djnz l0f37h ;0f3c
  3627. ret ;0f3e
  3628. l0f3fh:
  3629. push af ;0f3f
  3630. l0f40h:
  3631. xor a ;0f40
  3632. ld (01103h),a ;0f41
  3633. pop af ;0f44
  3634. ei ;0f45
  3635. reti ;0f46
  3636. l0f48h:
  3637. push af ;0f48
  3638. push bc ;0f49
  3639. ld a,0c3h ;0f4a
  3640. ld b,006h ;0f4c
  3641. l0f4eh:
  3642. out (0fch),a ;0f4e
  3643. djnz l0f4eh ;0f50
  3644. pop bc ;0f52
  3645. jr l0f40h ;0f53
  3646. l0f55h:
  3647. inc de ;0f55
  3648. call m,sub_0079h ;0f56
  3649. nop ;0f59
  3650. cp 000h ;0f5a
  3651. djnz $+22 ;0f5c
  3652. cp l ;0f5e
  3653. nop ;0f5f
  3654. djnz $+20 ;0f60
  3655. inc a ;0f62
  3656. add a,d ;0f63
  3657. rst 8 ;0f64
  3658. dec b ;0f65
  3659. rst 8 ;0f66
  3660. or e ;0f67
  3661. xor e ;0f68
  3662. add a,a ;0f69
  3663. nop ;0f6a
  3664. sub_0f6bh:
  3665. ld hl,l0f85h ;0f6b
  3666. jr l0f82h ;0f6e
  3667. sub_0f70h:
  3668. ld hl,l0f8fh ;0f70
  3669. jr l0f82h ;0f73
  3670. sub_0f75h:
  3671. ld hl,l0fa0h ;0f75
  3672. jr l0f82h ;0f78
  3673. sub_0f7ah:
  3674. ld a,001h ;0f7a
  3675. ld (01204h),a ;0f7c
  3676. ld hl,l0fb1h ;0f7f
  3677. l0f82h:
  3678. jp l00fdh ;0f82
  3679. l0f85h:
  3680. defb 0x4d ;0f85 'M'
  3681. defb 0x45 ;0f86 'E'
  3682. defb 0x4d ;0f87 'M'
  3683. defb 0x20 ;0f88 ' '
  3684. defb 0x45 ;0f89 'E'
  3685. defb 0x52 ;0f8a 'R'
  3686. defb 0x52 ;0f8b 'R'
  3687. defb 0x0d ;0f8c <CR>
  3688. defb 0x0a ;0f8d <LF>
  3689. defb 0x00 ;0f8e <NUL>
  3690. l0f8fh:
  3691. defb 0x46 ;0f8f 'F'
  3692. defb 0x4c ;0f90 'L'
  3693. defb 0x4f ;0f91 'O'
  3694. defb 0x50 ;0f92 'P'
  3695. defb 0x50 ;0f93 'P'
  3696. defb 0x59 ;0f94 'Y'
  3697. defb 0x20 ;0f95 ' '
  3698. defb 0x43 ;0f96 'C'
  3699. defb 0x54 ;0f97 'T'
  3700. defb 0x43 ;0f98 'C'
  3701. defb 0x20 ;0f99 ' '
  3702. defb 0x45 ;0f9a 'E'
  3703. defb 0x52 ;0f9b 'R'
  3704. defb 0x52 ;0f9c 'R'
  3705. defb 0x0d ;0f9d <CR>
  3706. defb 0x0a ;0f9e <LF>
  3707. defb 0x00 ;0f9f <NUL>
  3708. l0fa0h:
  3709. defb 0x46 ;0fa0 'F'
  3710. defb 0x4c ;0fa1 'L'
  3711. defb 0x4f ;0fa2 'O'
  3712. defb 0x50 ;0fa3 'P'
  3713. defb 0x50 ;0fa4 'P'
  3714. defb 0x59 ;0fa5 'Y'
  3715. defb 0x20 ;0fa6 ' '
  3716. defb 0x4e ;0fa7 'N'
  3717. defb 0x45 ;0fa8 'E'
  3718. defb 0x43 ;0fa9 'C'
  3719. defb 0x20 ;0faa ' '
  3720. defb 0x45 ;0fab 'E'
  3721. defb 0x52 ;0fac 'R'
  3722. defb 0x52 ;0fad 'R'
  3723. defb 0x0d ;0fae <CR>
  3724. defb 0x0a ;0faf <LF>
  3725. defb 0x00 ;0fb0 <NUL>
  3726. l0fb1h:
  3727. defb 0x44 ;0fb1 'D'
  3728. defb 0x4d ;0fb2 'M'
  3729. defb 0x41 ;0fb3 'A'
  3730. defb 0x20 ;0fb4 ' '
  3731. defb 0x45 ;0fb5 'E'
  3732. defb 0x52 ;0fb6 'R'
  3733. defb 0x52 ;0fb7 'R'
  3734. defb 0x0d ;0fb8 <CR>
  3735. defb 0x0a ;0fb9 <LF>
  3736. defb 0x00 ;0fba <NUL>
  3737. l0fbbh:
  3738. xor a ;0fbb Clear A register (A = 0)
  3739. ld (01226h),a ;0fbc Clear drive control flags
  3740. ld (01202h),a ;0fbf Set current drive number to 0 (start with drive A:)
  3741. ld (01225h),a ;0fc2 Clear drive parameter/selector
  3742. inc a ;0fc5 A = 1
  3743. ld (01227h),a ;0fc6 Enable drive operations (1 = enabled)
  3744. call l0de0h ;0fc9 Initialize FDC and check drive status
  3745. ld hl,01000h ;0fcc Set boot load address to 0x1000
  3746. l0fcfh:
  3747. ld (01200h),hl ;0fcf Store DMA/buffer address for disk operations
  3748. call l0dabh ;0fd2 Attempt to read boot sector from current drive
  3749. jp c,01000h ;0fd5
  3750. ld hl,l0fe1h ;0fd8
  3751. call l00fdh ;0fdb
  3752. jp l0294h ;0fde
  3753. l0fe1h:
  3754. defb 0x42 ;0fe1 'B'
  3755. defb 0x4f ;0fe2 'O'
  3756. defb 0x4f ;0fe3 'O'
  3757. defb 0x54 ;0fe4 'T'
  3758. defb 0x20 ;0fe5 ' '
  3759. defb 0x45 ;0fe6 'E'
  3760. defb 0x52 ;0fe7 'R'
  3761. defb 0x52 ;0fe8 'R'
  3762. defb 0x0d ;0fe9 <CR>
  3763. defb 0x0a ;0fea <LF>
  3764. defb 0x00 ;0feb <NUL>
  3765. l0fec: ; VERSION STRING - Displayed during system initialization
  3766. defb 0x56 ;0fec 'V'
  3767. defb 0x65 ;0fed 'e'
  3768. defb 0x72 ;0fee 'r'
  3769. defb 0x20 ;0fef ' '
  3770. defb 0x31 ;0ff0 '1'
  3771. l0ff1h:
  3772. defb 0x2e ;0ff1 '.'
  3773. defb 0x32 ;0ff2 '2'
  3774. defb 0x78 ;0ff3 'x'
  3775. defb 0x20 ;0ff4 ' '
  3776. defb 0x38 ;0ff5 '8'
  3777. l0ff6h:
  3778. defb 0x20 ;0ff6 ' '
  3779. defb 0x69 ;0ff7 'i'
  3780. defb 0x6e ;0ff8 'n'
  3781. defb 0x63 ;0ff9 'c'
  3782. defb 0x68 ;0ffa 'h'
  3783. l0ffbh:
  3784. defb 0x0d ;0ffb <CR>
  3785. defb 0x0a ;0ffc <LF>
  3786. defb 0x0a ;0ffd <LF>
  3787. defb 0x00 ;0ffe <NUL> - String terminator for print routine
  3788. defb 0xff ;0ffe FF