-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmalyon.el
3343 lines (2919 loc) · 123 KB
/
malyon.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; malyon.el --- mode to execute Z-code files version 3, 5, 8
;; Author: Peter Ilberg <[email protected]>, Christopher Madsen <[email protected]>, Erik Selberg <[email protected]>
;; Maintainer: Christopher Madsen <[email protected]>, Erik Selberg <[email protected]>
;; Version: 20161204
;; Package-Requires: ((cl-lib "0.5"))
;; Keywords: games, emulations
;; URL: https://github.com/speedenator/malyon
;; Old-Maintainer: Peter Ilberg <[email protected]>
;; (I am unable to continue supporting malyon.el. Please send me an
;; email if you are interested in taking over the project. Thanks.)
;; Copyright (C) 1999-2016 Peter Ilberg, Christopher Madsen, Erik Selberg
;; Permission is hereby granted, free of charge, to any person obtaining a
;; copy of this software and associated documentation files (the "Software"),
;; to deal in the Software without restriction, including without limitation
;; the rights to use, copy, modify, merge, publish, distribute, sublicense,
;; and/or sell copies of the Software, and to permit persons to whom the
;; Software is furnished to do so, subject to the following conditions:
;; The above copyright notice and this permission notice shall be included in
;; all copies or substantial portions of the Software.
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
;; DEALINGS IN THE SOFTWARE.
;;; Credits:
;; The author would like to thank the following people for reporting
;; bugs, testing, suggesting and/or contributing improvements:
;; Bernhard Barde, Jonathan Craven, Alberto Petrofsky, Alan Shutko
;;; Commentary:
;; This package provides a basic interpreter for version 3, 5, 8 Z-code
;; story files as generated by Inform (C) Graham Nelson and Infocom.
;; If you encounter a bug please send a report to Peter Ilberg at
;; [email protected]. Thank you!
;; To play a story file simply type M-x malyon and enter the path to the
;; story file. If anything goes wrong and you want to manually clean
;; up type M-x malyon-quit. In addition, you can switch back to a game in
;; progress by typing M-x malyon-restore.
;; A note on the format of saved game states:
;; As of version 1.0, Malyon supports the quetzal file format for saved
;; games. Support for this format required changes to several internal
;; data structures (stack frames and catch-throw) that are incompatible
;; with the old implementation. Unfortunately, the old file format for
;; saved games cannot be converted into quetzal.
;; For backwards compatibility, however, Malyon still supports the old
;; file format. And you can continue to play your old game states.
;; Because of the incompatibility of the two file formats, Malyon now
;; runs, as follows, in either of two modes: quetzal and compatibility.
;; - in quetzal mode, game states are saved in quetzal format
;; - in compatibility mode, games states are saved in the old format
;; - loading a game state in quetzal format switches to quetzal mode
;; - loading an old game state switches to compatibility mode
;; - quetzal mode is the default setting
;; In other words, Malyon will only use the old file format if you've
;; restored a game state saved in the old file format.
;; Enjoy!
;;; Code:
;; global variables - moved here to appease the byte-code compiler
;; requirements
(require 'cl-lib)
(require 'bindat)
(defconst malyon-version "1.2" "Malyon version number")
(defgroup malyon nil
"Play Z-machine interactive fiction games."
:prefix "malyon-"
:group 'games)
;; customizable variables
(defcustom malyon-stories-directory nil
"The default directory to look for story files.
Nil means use the buffer's default directory."
:type '(choice (const nil) directory)
:group 'malyon)
;; story file information
(defvar malyon-story-file-name nil
"The name of the story file being executed.")
(defvar malyon-story-file nil
"The story file which is currently being run.")
(defvar malyon-story-version nil
"The story file version.")
(defvar malyon-supported-versions '(3 5 8)
"A list of supported story file versions.")
;; status and transcript buffers
(defvar malyon-transcript-buffer nil
"The main transcript buffer of the story file execution.")
(defvar malyon-transcript-buffer-buffered nil
"Is output in the transcript buffer buffered?")
(defvar malyon-status-buffer nil
"The status bar buffer of the story file execution.")
(defvar malyon-status-buffer-lines nil
"The number of lines in the status bar buffer.")
(defvar malyon-status-buffer-delayed-split nil
"If the number of lines in the status buffer is reduced,
the window configuration is not changed immediately. It
is changed after the next turn (read or read_char).")
(defvar malyon-status-buffer-point nil
"The point location in the status bar buffer.")
(defvar malyon-max-column 72
"Maximum column for text display.")
;; window management
(defvar malyon-window-configuration nil
"The current window configuration of the malyon interpreter.")
(defvar malyon-current-window nil
"The currently active window for text output.")
;; z machine registers
(defvar malyon-stack nil
"The stack of the z machine.")
(defvar malyon-stack-pointer nil
"The stack pointer of the z machine.")
(defvar malyon-frame-pointer nil
"The frame pointer of the z machine.")
(defvar malyon-instruction-pointer nil
"The instruction pointer of the z machine.")
;; game file related global variables
(defvar malyon-score-game nil
"A flag indicating whether this story uses score or time.")
(defvar malyon-packed-multiplier nil
"The amount by which packed addresses are multiplied to get byte
addresses.")
(defvar malyon-global-variables nil
"A pointer to the global variable section in the story file.")
(defvar malyon-abbreviations nil
"A pointer to the abbreviations in the story file.")
(defvar malyon-alphabet nil
"The z machine's text alphabet.")
(defvar malyon-whitespace nil
"A string of whitespace characters recognized by the interpreter.")
;; object tables
(defvar malyon-object-table nil
"A pointer to the object table in the story file.")
(defvar malyon-object-table-entry-size nil
"The size of one entry in the object table.")
(defvar malyon-object-properties nil
"The number of properties per object minus one.")
(defvar malyon-object-property-offset nil
"The byte offset of the properties table in the object.")
;; dictionaries
(defvar malyon-dictionary nil
"A pointer to the dictionary of the story file.")
(defvar malyon-dictionary-entry-length nil
"The length of a dictionary entry.")
(defvar malyon-dictionary-num-entries nil
"The number of dictionary entries.")
(defvar malyon-dictionary-entries nil
"A pointer to the first dictionary entry.")
(defvar malyon-dictionary-word-length nil
"The length of a dictionary word.")
;; game state information
(defvar malyon-game-state-restart nil
"The machine state for implementing restart.")
(defvar malyon-game-state-undo nil
"The machine state for implementing undo.")
(defvar malyon-game-state-quetzal t
"Store game state information for quetzal.")
;; various
(defvar malyon-current-face nil
"The current face in which to display text.")
(defvar malyon-last-cursor-position-after-input nil
"The last cursor position after reading input from the keyboard.")
;; error trapping
(defmacro malyon-hide-internal-errors (message bodyform)
"Evaluate BODYFORM, substituting MESSAGE for any error."
(declare (indent 1))
`(condition-case nil
,bodyform
(error
(malyon-fatal-error ,message))))
;; when debugging, uncomment this NOP version of malyon-hide-internal-errors
;(defmacro malyon-hide-internal-errors (msg body) (declare (indent 1)) body)
;; interactive functions
;;;###autoload
(defun malyon (file-name)
"Play a Z-machine interactive fiction game.
If a game is in progress, restores the game's window configuration.
Otherwise, you are prompted for a story file to load, which may be
either a raw Z-code file or one packaged in a Blorb file.
Z-code versions 3, 5, and 8 are supported."
(interactive
(list
(if malyon-story-file nil
(read-file-name "Story file name: " malyon-stories-directory nil t))))
(if malyon-story-file
(if file-name
(error "You are already playing a game.")
(malyon-restore))
;; Otherwise, we're starting a new game:
(cond
((string-match "\\.\\(?:z?blorb\\|blb\\|zlb\\)$" file-name)
(malyon-load-blorb-file file-name))
((string-match "\\.z[358]$" file-name)
(malyon-hide-internal-errors "loading of story file failed."
(malyon-load-story-file file-name)))
(t
(error "%s is not a version 3, 5, or 8 story file." file-name)))
(setq malyon-story-version (aref malyon-story-file 0))
(cond ((memq malyon-story-version malyon-supported-versions)
(malyon-hide-internal-errors "initialization of interpreter failed."
(malyon-initialize))
(malyon-interpreter))
(t
(message "%s is not a version 3, 5, or 8 story file." file-name)
(malyon-cleanup)))))
(defun malyon-restore ()
"Restore the save window configuration for the interpreter."
(interactive)
(malyon-hide-internal-errors "restoring window configuration failed."
(progn
(malyon-restore-window-configuration)
(malyon-adjust-transcript))))
(defun malyon-quit ()
"Exit the malyon interpreter."
(interactive)
(if malyon-story-file
(progn
(malyon-restore)
(if (malyon-yes-or-no-p-minibuf "Do you really want to quit? ")
(malyon-cleanup)))))
;;;###autoload
(defun malyon-mode ()
"This mode provides a basic interpreter for version 3, 5, 8 Z-code
story files as generated by Inform (C) Graham Nelson and Infocom.
Note that this package is by no means complete and bug free.
If you encounter a bug please send a report to Peter Ilberg at
[email protected]. Thank you!
To play a story file simple type M-x malyon and enter the path to the
story file. If anything goes wrong and you want to manually clean
up type M-x malyon-quit. In addition, you can switch back to a game in
progress by typing M-x malyon-restore.
The author would like to thank the following people for reporting
bugs, testing, suggesting and/or contributing improvements:
Bernhard Barde, Jonathan Craven, Alberto Petrofsky, Alan Shutko"
(message "Use M-x malyon if you want to play a zcode game."))
;; compatibility functions for GNU emacs
;; nuked 12/4/16 by [email protected] --- these aren't needed
;; (if (fboundp 'cadr)
;; (defalias 'malyon-cadr 'cadr)
;; (defun malyon-cadr (list)
;; "Take the cadr of the list."
;; (car (cdr list))))
;; (if (fboundp 'caddr)
;; (defalias 'malyon-caddr 'caddr)
;; (defun malyon-caddr (list)
;; "Take the caddr of the list."
;; (car (cdr (cdr list)))))
;; (if (fboundp 'cdddr)
;; (defalias 'malyon-cdddr 'cdddr)
;; (defun malyon-cdddr (list)
;; "Take the cdddr of the list."
;; (cdr (cdr (cdr list)))))
;; (if (fboundp 'char-before)
;; (defalias 'malyon-char-before 'char-before)
;; (defun malyon-char-before ()
;; "Return the character before the point."
;; (char-after (- (point) 1))))
(defun malyon-char-to-int (c)
"Convert a character into an integer."
(if (fboundp 'char-to-int)
(char-to-int c)
c))
;; (if (fboundp 'characterp)
;; (defalias 'malyon-characterp 'characterp)
;; (defun malyon-characterp (x)
;; "Test for a character."
;; (and (numberp x) (<= 0 x) (< x 256))))
(defun malyon-disable-multibyte ()
"Disable multibyte support in the current buffer."
(condition-case nil (set-buffer-multibyte nil) (error)))
(defun malyon-erase-buffer (&optional buffer)
"Erase the given buffer."
(save-excursion
(if buffer (set-buffer buffer))
(if (and buffer (eq buffer malyon-transcript-buffer))
(malyon-begin-section)
(erase-buffer))))
(defun malyon-int-to-char (i)
"Convert an integer into a character."
(if (fboundp 'int-to-char)
(int-to-char i)
i))
;; (if (fboundp 'mapc)
;; (defalias 'malyon-mapc 'mapc)
;; (defun malyon-mapc (function list)
;; "Apply fun to every element of args ignoring the results."
;; (if (null list)
;; '()
;; (funcall function (car list))
;; (malyon-mapc function (cdr list)))))
;; (if (fboundp 'mapcan)
;; (defalias 'malyon-mapcan 'mapcan)
;; (defun malyon-mapcan (function list)
;; "Apply fun to every element of args nconc'ing the result."
;; (if (null list)
;; '()
;; (nconc (funcall function (car list))
;; (malyon-mapcan function (cdr list))))))
; Do not use the built-in conversion via 'multibyte-char-to-unibyte.
(defun malyon-multibyte-char-to-unibyte (char)
"Convert a multibyte character to unibyte."
char)
(defun malyon-point-max (&optional buffer)
"Get the point-max of the given buffer."
(save-excursion
(if buffer (set-buffer buffer))
(point-max)))
(defun malyon-redisplay-frame (frame &rest ignore)
"Redisplay the given frame."
(if (fboundp 'redisplay-frame)
(redisplay-frame frame ignore)))
;; (if (fboundp 'remove)
;; (defalias 'malyon-remove 'remove)
;; (defun malyon-remove (element list)
;; "Remove the element from the list."
;; (cond ((null list)
;; '())
;; ((eq element (car list))
;; (malyon-remove element (cdr list)))
;; ((equal element (car list))
;; (malyon-remove element (cdr list)))
;; (t
;; (cons (car list)
;; (malyon-remove element (cdr list)))))))
;; legacy thing... normally this is a noop for FSF Emacs
(defun malyon-set-keymap-name (keymap name)
"Set the name of the keymap."
(if (fboundp 'set-keymap-name)
(set-keymap-name keymap name)))
;; (if (fboundp 'string-to-list)
;; (defalias 'malyon-string-to-list 'string-to-list)
;; (defun malyon-string-to-list (s)
;; "Convert a string into a list of characters."
;; (let ((i (- (length s) 1)) (l '()))
;; (while (<= 0 i)
;; (setq l (cons (aref s i) l)
;; i (- i 1)))
;; l)))
;; (if (fboundp 'string-to-vector)
;; (defalias 'malyon-string-to-vector 'string-to-vector)
;; (defun malyon-string-to-vector (s)
;; "Convert a string into a vector of characters."
;; (let* ((i 0) (l (length s)) (v (make-vector l 0)))
;; (while (< i l)
;; (aset v i (aref s i))
;; (setq i (+ 1 i)))
;; v)))
; Do not use the built-in conversion via 'unibyte-char-to-multibyte.
(defun malyon-unibyte-char-to-multibyte (char)
"Convert a unibyte character to multibyte."
char)
(defun malyon-vector-to-list (v begin end)
"Return a list of elements in v in the range [begin, end)."
(let ((result '()))
(while (< begin end)
(setq result (cons (aref v begin) result))
(setq begin (+ 1 begin)))
(reverse result)))
(defun malyon-window-displayed-height (&optional window)
"Get the height of the window's displayed region."
(if (fboundp 'window-displayed-height)
(window-displayed-height window)
(- (window-height) 1)))
(defun malyon-yes-or-no-p-minibuf (prompt)
"Ask a yes or no question."
(if (fboundp 'yes-or-no-p-minibuf)
(yes-or-no-p-minibuf prompt)
(yes-or-no-p prompt)))
;; global variables for the malyon mode
(defvar malyon-syntax-table nil
"Syntax table used while in malyon mode (same as in text-mode).")
(if malyon-syntax-table
'()
(setq malyon-syntax-table (make-syntax-table))
(modify-syntax-entry ?\" ". " malyon-syntax-table)
(modify-syntax-entry ?\\ ". " malyon-syntax-table)
(modify-syntax-entry ?' "w " malyon-syntax-table))
(defvar malyon-keymap-read nil
"Keymap for malyon mode for reading input into a buffer.")
(defvar malyon-history-saved-up nil
"The saved binding for the up arrow key.")
(defvar malyon-history-saved-down nil
"The saved binding for the down arrow key.")
(if malyon-keymap-read
'()
(setq malyon-keymap-read (make-sparse-keymap))
(malyon-set-keymap-name malyon-keymap-read 'malyon-keymap-read)
(setq malyon-history-saved-up (global-key-binding [up]))
(setq malyon-history-saved-down (global-key-binding [down]))
(define-key malyon-keymap-read "\r" 'malyon-end-input)
(define-key malyon-keymap-read [up] 'malyon-history-previous-char)
(define-key malyon-keymap-read [down] 'malyon-history-next-char)
(define-key malyon-keymap-read "\M-p" 'malyon-history-previous-char)
(define-key malyon-keymap-read "\M-n" 'malyon-history-next-char)
(define-key malyon-keymap-read "\C-a" 'malyon-beginning-of-line)
(define-key malyon-keymap-read "\C-w" 'malyon-kill-region)
(define-key malyon-keymap-read "\C-k" 'malyon-kill-line)
(define-key malyon-keymap-read "\M-d" 'malyon-kill-word)
(define-key malyon-keymap-read "\C-y" 'malyon-yank)
(define-key malyon-keymap-read "\M-y" 'malyon-yank-pop)
(define-key malyon-keymap-read "\C-d" 'malyon-delete-char)
(define-key malyon-keymap-read "\d" 'malyon-backward-delete-char)
(define-key malyon-keymap-read [del] 'malyon-delete-char)
(define-key malyon-keymap-read [backspace] 'malyon-backward-delete-char)
(substitute-key-definition (lookup-key (current-global-map) "a")
'malyon-self-insert-command
malyon-keymap-read (current-global-map)))
(defvar malyon-keymap-readchar nil
"Keymap for malyon mode for waiting for input.")
(if malyon-keymap-readchar
'()
(setq malyon-keymap-readchar (make-sparse-keymap))
(malyon-set-keymap-name malyon-keymap-readchar 'malyon-keymap-readchar)
(define-key malyon-keymap-readchar "\r" 'malyon-wait-char)
(substitute-key-definition (lookup-key (current-global-map) "a")
'malyon-wait-char
malyon-keymap-readchar (current-global-map)))
(defvar malyon-keymap-more nil
"Keymap for malyon mode for browsing through text.")
(if malyon-keymap-more
'()
(setq malyon-keymap-more (make-sparse-keymap))
(malyon-set-keymap-name malyon-keymap-more 'malyon-keymap-more)
(define-key malyon-keymap-more "\r" 'malyon-more-char)
(substitute-key-definition (lookup-key (current-global-map) "a")
'malyon-more-char
malyon-keymap-more (current-global-map)))
(defvar malyon-keymap-more-status nil
"Keymap for malyon mode for browsing through the status buffer.")
(if malyon-keymap-more-status
'()
(setq malyon-keymap-more-status (make-sparse-keymap))
(malyon-set-keymap-name malyon-keymap-more-status 'malyon-keymap-more-status)
(define-key malyon-keymap-more-status "\r" 'malyon-more-char-status)
(substitute-key-definition (lookup-key (current-global-map) "a")
'malyon-more-char-status
malyon-keymap-more-status (current-global-map)))
(defface malyon-face-plain
'((t :inherit default))
"Basic face for game text."
:group 'malyon)
(defface malyon-face-bold
'((t :inherit bold))
"Bold face for game text."
:group 'malyon)
(defface malyon-face-error
'((t :inherit error))
"Face for game errors."
:group 'malyon)
(defface malyon-face-italic
'((t :inherit italic))
"Italic face for game text."
:group 'malyon)
(defface malyon-face-reverse
'((t :inherit default :inverse-video t))
"Face for reverse-video text."
:group 'malyon)
(defvar malyon-faces
'((0 . malyon-face-plain)
(1 . malyon-face-reverse)
(2 . malyon-face-bold)
(4 . malyon-face-italic)
(8 . malyon-face-plain))
"An association list of text faces used by the malyon mode.")
(defvar malyon-print-separator nil
"A flag indicating whether to print the * * * separator.")
(defun malyon-begin-section ()
"Print a section divider and begin a new section."
(if malyon-print-separator
(progn
(mapc 'malyon-putchar-transcript '(?\n ?\n ?* ? ?* ? ?*))
(center-line)
(mapc 'malyon-putchar-transcript '(?\n ?\n))
(setq malyon-print-separator nil)))
(narrow-to-region (point-max) (point-max)))
(if malyon-whitespace
'()
(setq malyon-whitespace (list (malyon-char-to-int ? )
(malyon-char-to-int ?\t)
(malyon-char-to-int ?\n)
(malyon-char-to-int ?\r))))
;; memory utilities
(defsubst malyon-read-byte (address)
"Read a byte at address in the story file."
(if (<= 0 address)
(aref malyon-story-file address)
(aref malyon-story-file (+ 65536 address))))
(defsubst malyon-store-byte (address value)
"Store a byte at address in the story file."
(if (<= 0 address)
(aset malyon-story-file address (logand 255 value))
(aset malyon-story-file (+ 65536 address) (logand 255 value))))
(defsubst malyon-read-word (address)
"Read a word at address in the story file."
(if (<= 0 address)
(logior (lsh (aref malyon-story-file address) 8)
(aref malyon-story-file (+ 1 address)))
(logior (lsh (aref malyon-story-file (+ 65536 address)) 8)
(aref malyon-story-file (+ 65537 address)))))
(defsubst malyon-store-word (address value)
"Store a word at address in the story file."
(if (<= 0 address)
(progn
(aset malyon-story-file address (logand 255 (lsh value -8)))
(aset malyon-story-file (+ 1 address) (logand 255 value)))
(aset malyon-story-file (+ 65536 address) (logand 255 (lsh value -8)))
(aset malyon-story-file (+ 65537 address) (logand 255 value))))
(defsubst malyon-read-code-byte ()
"Read the next byte at the program counter location."
(setq malyon-instruction-pointer (+ malyon-instruction-pointer 1))
(malyon-read-byte (- malyon-instruction-pointer 1)))
(defsubst malyon-read-code-word ()
"Read the next word at the program counter location."
(setq malyon-instruction-pointer (+ malyon-instruction-pointer 2))
(malyon-read-word (- malyon-instruction-pointer 2)))
(defsubst malyon-pop-stack ()
"Pop a value off the stack."
(if (> 0 malyon-stack-pointer)
(malyon-fatal-error "stack underflow."))
(setq malyon-stack-pointer (- malyon-stack-pointer 1))
(aref malyon-stack (+ malyon-stack-pointer 1)))
(defsubst malyon-read-local-variable (variable)
"Read a local variable."
(aref malyon-stack (+ variable malyon-frame-pointer)))
(defsubst malyon-read-global-variable (variable)
"Read a global variable."
(malyon-read-word (+ malyon-global-variables (* 2 variable))))
(defsubst malyon-read-variable (variable)
"Read a variable."
(cond ((= variable 0) (malyon-pop-stack))
((< variable 16) (malyon-read-local-variable variable))
(t (malyon-read-global-variable (- variable 16)))))
(defsubst malyon-push-stack (value)
"Push a value onto the stack."
(setq malyon-stack-pointer (+ malyon-stack-pointer 1))
(aset malyon-stack malyon-stack-pointer value))
(defsubst malyon-store-local-variable (variable value)
"Store a value in a local variable."
(aset malyon-stack (+ variable malyon-frame-pointer) value))
(defsubst malyon-store-global-variable (variable value)
"Store a value in a global variable."
(malyon-store-word (+ malyon-global-variables (* 2 variable)) value))
(defsubst malyon-store-variable (var value)
"Store the value in a variable."
(setq value (logand 65535 value))
(cond ((= var 0) (malyon-push-stack value))
((< var 16) (malyon-store-local-variable var value))
(t (malyon-store-global-variable (- var 16) value))))
;; list of opcodes
(defvar malyon-opcodes
[malyon-opcode-nop
malyon-opcode-je malyon-opcode-jl
malyon-opcode-jg malyon-opcode-dec-chk
malyon-opcode-inc-chk malyon-opcode-jin
malyon-opcode-test malyon-opcode-or
malyon-opcode-and malyon-opcode-test-attr
malyon-opcode-set-attr malyon-opcode-clear-attr
malyon-opcode-store malyon-opcode-insert-obj
malyon-opcode-loadw malyon-opcode-loadb
malyon-opcode-get-prop malyon-opcode-get-prop-addr
malyon-opcode-get-next-prop malyon-opcode-add
malyon-opcode-sub malyon-opcode-mul
malyon-opcode-div malyon-opcode-mod
malyon-opcode-calls malyon-opcode-calln
malyon-opcode-set-color malyon-opcode-throw
malyon-opcode-nop malyon-opcode-nop
malyon-opcode-nop malyon-opcode-nop
malyon-opcode-je malyon-opcode-jl
malyon-opcode-jg malyon-opcode-dec-chk
malyon-opcode-inc-chk malyon-opcode-jin
malyon-opcode-test malyon-opcode-or
malyon-opcode-and malyon-opcode-test-attr
malyon-opcode-set-attr malyon-opcode-clear-attr
malyon-opcode-store malyon-opcode-insert-obj
malyon-opcode-loadw malyon-opcode-loadb
malyon-opcode-get-prop malyon-opcode-get-prop-addr
malyon-opcode-get-next-prop malyon-opcode-add
malyon-opcode-sub malyon-opcode-mul
malyon-opcode-div malyon-opcode-mod
malyon-opcode-calls malyon-opcode-calln
malyon-opcode-set-color malyon-opcode-throw
malyon-opcode-nop malyon-opcode-nop
malyon-opcode-nop malyon-opcode-nop
malyon-opcode-je malyon-opcode-jl
malyon-opcode-jg malyon-opcode-dec-chk
malyon-opcode-inc-chk malyon-opcode-jin
malyon-opcode-test malyon-opcode-or
malyon-opcode-and malyon-opcode-test-attr
malyon-opcode-set-attr malyon-opcode-clear-attr
malyon-opcode-store malyon-opcode-insert-obj
malyon-opcode-loadw malyon-opcode-loadb
malyon-opcode-get-prop malyon-opcode-get-prop-addr
malyon-opcode-get-next-prop malyon-opcode-add
malyon-opcode-sub malyon-opcode-mul
malyon-opcode-div malyon-opcode-mod
malyon-opcode-calls malyon-opcode-calln
malyon-opcode-set-color malyon-opcode-throw
malyon-opcode-nop malyon-opcode-nop
malyon-opcode-nop malyon-opcode-nop
malyon-opcode-je malyon-opcode-jl
malyon-opcode-jg malyon-opcode-dec-chk
malyon-opcode-inc-chk malyon-opcode-jin
malyon-opcode-test malyon-opcode-or
malyon-opcode-and malyon-opcode-test-attr
malyon-opcode-set-attr malyon-opcode-clear-attr
malyon-opcode-store malyon-opcode-insert-obj
malyon-opcode-loadw malyon-opcode-loadb
malyon-opcode-get-prop malyon-opcode-get-prop-addr
malyon-opcode-get-next-prop malyon-opcode-add
malyon-opcode-sub malyon-opcode-mul
malyon-opcode-div malyon-opcode-mod
malyon-opcode-calls malyon-opcode-calln
malyon-opcode-set-color malyon-opcode-throw
malyon-opcode-nop malyon-opcode-nop
malyon-opcode-nop malyon-opcode-jz
malyon-opcode-get-sibling malyon-opcode-get-child
malyon-opcode-get-parent malyon-opcode-get-prop-len
malyon-opcode-inc malyon-opcode-dec
malyon-opcode-print-addr malyon-opcode-calls
malyon-opcode-remove-obj malyon-opcode-print-obj
malyon-opcode-ret malyon-opcode-jump
malyon-opcode-print-paddr malyon-opcode-load
malyon-opcode-calln malyon-opcode-jz
malyon-opcode-get-sibling malyon-opcode-get-child
malyon-opcode-get-parent malyon-opcode-get-prop-len
malyon-opcode-inc malyon-opcode-dec
malyon-opcode-print-addr malyon-opcode-calls
malyon-opcode-remove-obj malyon-opcode-print-obj
malyon-opcode-ret malyon-opcode-jump
malyon-opcode-print-paddr malyon-opcode-load
malyon-opcode-calln malyon-opcode-jz
malyon-opcode-get-sibling malyon-opcode-get-child
malyon-opcode-get-parent malyon-opcode-get-prop-len
malyon-opcode-inc malyon-opcode-dec
malyon-opcode-print-addr malyon-opcode-calls
malyon-opcode-remove-obj malyon-opcode-print-obj
malyon-opcode-ret malyon-opcode-jump
malyon-opcode-print-paddr malyon-opcode-load
malyon-opcode-calln malyon-opcode-rtrue
malyon-opcode-rfalse malyon-opcode-print
malyon-opcode-print-ret malyon-opcode-nop
malyon-opcode-illegal malyon-opcode-illegal
malyon-opcode-restart malyon-opcode-ret-popped
malyon-opcode-catch malyon-opcode-quit
malyon-opcode-new-line malyon-opcode-illegal
malyon-opcode-verify malyon-opcode-illegal
malyon-opcode-piracy malyon-opcode-nop
malyon-opcode-je malyon-opcode-jl
malyon-opcode-jg malyon-opcode-dec-chk
malyon-opcode-inc-chk malyon-opcode-jin
malyon-opcode-test malyon-opcode-or
malyon-opcode-and malyon-opcode-test-attr
malyon-opcode-set-attr malyon-opcode-clear-attr
malyon-opcode-store malyon-opcode-insert-obj
malyon-opcode-loadw malyon-opcode-loadb
malyon-opcode-get-prop malyon-opcode-get-prop-addr
malyon-opcode-get-next-prop malyon-opcode-add
malyon-opcode-sub malyon-opcode-mul
malyon-opcode-div malyon-opcode-mod
malyon-opcode-calls malyon-opcode-calln
malyon-opcode-set-color malyon-opcode-throw
malyon-opcode-nop malyon-opcode-nop
malyon-opcode-nop malyon-opcode-calls
malyon-opcode-storew malyon-opcode-storeb
malyon-opcode-put-prop malyon-opcode-aread
malyon-opcode-print-char malyon-opcode-print-num
malyon-opcode-random malyon-opcode-push
malyon-opcode-pull malyon-opcode-split-window
malyon-opcode-set-window malyon-opcode-calls
malyon-opcode-erase-window malyon-opcode-erase-line
malyon-opcode-set-cursor malyon-opcode-get-cursor
malyon-opcode-set-text-style malyon-opcode-buffer-mode
malyon-opcode-output-stream malyon-opcode-input-stream
malyon-opcode-nop malyon-opcode-read-char
malyon-opcode-scan-table malyon-opcode-not
malyon-opcode-calln malyon-opcode-calln
malyon-opcode-tokenise malyon-opcode-encode-text
malyon-opcode-copy-table malyon-opcode-print-table
malyon-opcode-check-arg-count malyon-opcode-save
malyon-opcode-restore malyon-opcode-log-shift
malyon-opcode-art-shift malyon-opcode-set-font
malyon-opcode-illegal malyon-opcode-illegal
malyon-opcode-illegal malyon-opcode-illegal
malyon-opcode-save-undo malyon-opcode-restore-undo
malyon-opcode-print-unicode malyon-opcode-check-unicode
malyon-opcode-nop malyon-opcode-nop
malyon-opcode-nop]
"A vector of all known legal Z-code opcodes.")
;; initialization
(defun malyon-load-story-from-buffer (min max)
"Load a Z-code story into an internal vector."
(setq malyon-story-file (string-to-vector
(buffer-substring-no-properties min max)))
(if (not (eq ?\^A 1))
(let ((i 0))
(while (< i (length malyon-story-file))
(aset malyon-story-file
i
(malyon-char-to-int (aref malyon-story-file i)))
(setq i (+ 1 i)))))
)
(defun malyon-load-file (file-name)
"Load a binary file into the current buffer."
(malyon-disable-multibyte)
(malyon-erase-buffer)
(let ((coding-system-for-read 'binary))
(insert-file-contents file-name)))
(defconst malyon-iff-chunk-spec
'((:id str 4)
(:length u32))
"Description of an IFF chunk header.")
(defconst malyon-RIdx-spec
'((:num u32)
(:resources repeat (:num) (:usage str 4) (:number u32) (:start u32)))
"Description of the RIdx chunk.")
(defun malyon-unpack (spec start length)
"Unpack LENGTH bytes at 0-based offset START according to SPEC."
(bindat-unpack spec (buffer-substring-no-properties (1+ start)
(+ 1 start length))))
(defun malyon-load-blorb-file (file-name)
"Load a Z-code Blorb file into an internal vector."
(require 'bindat)
(require 'cl-lib)
(with-temp-buffer
(malyon-load-file file-name)
;; Check the signature. Must be an IFF file of type IFRS
;; starting with an RIdx chunk.
(or (and (string= "FORM" (buffer-substring-no-properties 1 5))
(string= "IFRSRIdx" (buffer-substring-no-properties 9 17)))
(error "Not a Blorb file"))
;; Unpack the RIdx chunk, find the Exec chunck, and unpack its header.
(let* ((ridx-header (malyon-unpack malyon-iff-chunk-spec 12 8))
(ridx (malyon-unpack malyon-RIdx-spec 20
(cdr (assq :length ridx-header))))
(exec (cl-find-if
(function
(lambda (r)
(and (= 0 (cdr (assq :number r)))
(string= "Exec" (cdr (assq :usage r))))))
(cdr (assq :resources ridx))))
(story-start (cdr (assq :start exec)))
(exec-header (malyon-unpack malyon-iff-chunk-spec story-start 8)))
;; Ensure that the Exec chunk is Z-code.
(or (string= "ZCOD" (cdr (assq :id exec-header)))
(error "Not a Z-code Blorb file"))
;; Load the Z-code from the chunk.
(setq malyon-story-file-name file-name)
(malyon-load-story-from-buffer (+ 9 story-start)
(+ 9 story-start
(cdr (assq :length exec-header)))))))
(defun malyon-load-story-file (file-name)
"Load a Z-code story file into an internal vector."
(with-temp-buffer
(malyon-load-file file-name)
(setq malyon-story-file-name file-name)
(malyon-load-story-from-buffer (point-min) (point-max))))
(defun malyon-initialize ()
"Initialize the Z-code interpreter."
; (malyon-trace-file)
(setq malyon-game-state-quetzal t)
(malyon-initialize-status)
(malyon-initialize-transcript)
(malyon-initialize-windows)
(malyon-initialize-story-header)
(malyon-initialize-registers)
(malyon-initialize-opcodes)
(malyon-history-clear)
(setq malyon-game-state-restart (malyon-current-game-state))
(malyon-print-header))
(defun malyon-initialize-status ()
"Initialize the status buffer."
(setq malyon-status-buffer (get-buffer-create "Malyon Status"))
(switch-to-buffer malyon-status-buffer)
(malyon-erase-buffer)
(kill-all-local-variables)
(setq cursor-in-non-selected-windows nil)
(setq mode-line-format nil)
(setq malyon-status-buffer-point (point))
(setq malyon-status-buffer-lines 0)
(setq malyon-status-buffer-delayed-split nil)
(use-local-map malyon-keymap-read)
(set-syntax-table malyon-syntax-table)
(setq mode-name "Malyon")
(setq major-mode 'malyon-mode)
(run-hooks 'malyon-mode-hook))
(defun malyon-initialize-transcript ()
"Initialize the transcript buffer."
(setq malyon-transcript-buffer (get-buffer-create "Malyon Transcript"))
(switch-to-buffer malyon-transcript-buffer)
(malyon-erase-buffer)
(kill-all-local-variables)
(setq malyon-last-cursor-position-after-input
(malyon-point-max malyon-transcript-buffer))
(use-local-map malyon-keymap-read)
(set-syntax-table malyon-syntax-table)
(setq fill-column malyon-max-column)
(auto-fill-mode 1)
(setq mode-name "Malyon")
(setq major-mode 'malyon-mode)
(run-hooks 'malyon-mode-hook))
(defun malyon-initialize-windows ()
"Initialize the window configuration for the z machine."
(setq malyon-transcript-buffer-buffered t)
(malyon-set-window-configuration 0)
(malyon-opcode-set-window 0))
(defun malyon-initialize-story-header ()
"Initializes the header section of the story file."
(malyon-store-byte 1
(if (>= malyon-story-version 5)
28
(logior #x20 (logand #b111 (malyon-read-byte 1)))))
(malyon-store-byte 16 (logand 440 (malyon-read-byte 16)))
(malyon-store-byte 30 1)
(malyon-store-byte 31 65)
(malyon-store-byte 32 255)
(malyon-store-byte 33 (- malyon-max-column 1))
(malyon-store-word 34 (- malyon-max-column 1))
(malyon-store-word 36 255)
(malyon-store-word 38 1)
(malyon-store-word 39 1)
(malyon-store-byte 44 0)
(malyon-store-byte 45 0)
(malyon-store-byte 50 1)
(malyon-store-byte 51 0))
(defun malyon-initialize-registers ()
"Initialize the interpreter's internal registers."
(setq malyon-stack (make-vector 1024 0))
(setq malyon-stack-pointer -1)
(malyon-push-initial-frame)
(setq malyon-frame-pointer malyon-stack-pointer)
(setq malyon-instruction-pointer (malyon-read-word 6))
(setq malyon-global-variables (malyon-read-word 12))
(setq malyon-object-table (malyon-read-word 10))
(cond ((< malyon-story-version 5)
(setq malyon-object-table-entry-size 9)
(setq malyon-object-properties 31)
(setq malyon-object-property-offset 7))
(t
(setq malyon-object-table-entry-size 14)
(setq malyon-object-properties 63)