/
minecraft.dasm16
663 lines (522 loc) · 13.8 KB
/
minecraft.dasm16
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
; minecraft.dasm16
; v1.5
; Created by Pseudox/trevs231
SET PC, main_inits
;Program start -------------------------------
:main_inits
SET C, [KBRD_BUFFER];clearing keyboard buffer
SET [c], 0
;Main loop-----------------------
:main
JSR MINECRAFT_init
:EXIT
SET J, 0x40
:crash
SET PC, crash
; end of main section-----------------
;********************************************************
;COPY CODE BELOW into your OS AND CALL 'JSR MINECRAFT_init'
;vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
;Static Variables and data-------------------------
; (this is in my main code, but you will
; need it for the program to run)
:SCRN_START DAT 0x8000 ;first screen location
:SCRN_END DAT 0x81FF ;last screen location
:KBRD_BUFFER DAT 0x9000 ;where inputs are read from keyboard
:HEAPARR DAT 0x9100 ; that starting pointer for the heap for malloc
:HEAP DAT 0x9200 ; start of the stack of memory addresses
;===============================================================
;MEMORY ALLOCATION SUBROUTINES
;===============================================================
; https://github.com/ryban/DCPPU16
; Author: Ryban
; Returns pointers to 64 word chunks
; maintain a 256 word list of empty 64 word chunks
; when you need a new one you loop through the 256 word list until you find an empty one
; Multiply the index of that marker (from the 256 word array) by 64 to get the address
; the value returned in A is a pointer to the start of the memory
:malloc
SET PUSH, B
SET PUSH, C
SET A, 0
SET B, [HEAPARR]
:maloc_l4
SET C, [B]
IFE C, 0
SET PC, maloc_found
IFG A, 0xff ; A > 255
SET PC, maloc_full
ADD A, 1
ADD B, 1
SET PC, maloc_l4
:maloc_found
; ADD B, A
SET [B], 0xffff ; mark chunk taken
MUL A, 0x40 ; A * 64
SET B, [HEAP]
ADD A, B
SET C, POP
SET B, POP
SET PC, POP
:maloc_full
SET A, 0x2000 ; A hopefully harmless area...
SET C, POP
SET B, POP
SET PC, POP
;=================================================================
; Author: Ryban
; releases memory given by malloc.
; This IS NOT automatically done, be sure to free memory when you are done
; A is the pointer to your memory chunk to be freed
:free
SET PUSH, B
SET B, [HEAP]
SUB A, B
DIV A, 64
SET B, [HEAPARR]
ADD A, B
SET [A], 0
SET B, POP
SET PC, POP
;==================================================================
;============================================================
;MINECRAFT GAME FOR DCPU-16 v1.5 by Pseudox/trevs231
;============================================================
;A= used in maloc and free
;B= prevents other buttons from messing up input
;C= falling counter
;X= helper
;Y= water flow timer?
;Z= input buffer
;I= helper
;J= helper
;====================================================
;Variables and constants
:MC_PLAYER_POS RESERVE 1
:MC_CURRENT_BLOCK RESERVE 1
:MC_SKY_CHAR DAT 0x0B00
:MC_WATER_CHAR DAT 0x0100
:MC_BLOCK_CHARS DAT 0x182A, 0x862A, 0x0200, 0x0800, 0x0600
DAT 0x782A, 0xE82A, 0xC82A, 0xB82A, 0
; water source, dirt, grass, rock, cobblestone, wood, gold deposit
; redstone, diamond,
:MC_PLAYER_CHAR DAT 0x4058
:MC_FALL_TIME DAT 0x0500
:MC_up_key DAT 0x0003 ;up arrow
:MC_left_key DAT 0x0001 ;left arrow
:MC_right_key DAT 0x0002 ;right arrow
:MC_do_up_key DAT 0x0077 ;w
:MC_do_down_key DAT 0x0073 ;s
:MC_do_left_key DAT 0x0061 ;a
:MC_do_right_key DAT 0x0064 ;d
:MC_swap_key DAT 0x0065 ;e
:MC_reset_key DAT 0x006F ;o
:MC_quit_key DAT 0x0070 ;p
;=====================================================
:MINECRAFT_init
SET PUSH, B
SET PUSH, C
SET PUSH, X
SET PUSH, Y
SET PUSH, Z
SET PUSH, I
SET PUSH, J
:MC_reset_point
SET z, [KBRD_BUFFER] ;initialize input buffer
SET [z], 0
SET x, [SCRN_START]
SET C, [MC_FALL_TIME] ;for fall speed
SET Y, [MC_FALL_TIME] ;for water flow speed
SET J, x
ADD j, 0x100
:MINECRAFT_init_loop1
SET [x], [MC_SKY_CHAR]
ADD x, 1
IFG j, x
SET PC, MINECRAFT_init_loop1
ADD j, 0x20
SET i, 2
:MINECRAFT_init_loop2
SET [x], [MC_BLOCK_CHARS+i]
ADD x, 1
IFG j, x
SET PC, MINECRAFT_init_loop2
ADD j, 0x40
SET i, 1
:MINECRAFT_init_loop3
SET [x], [MC_BLOCK_CHARS+i]
ADD x, 1
IFG j, x
SET PC, MINECRAFT_init_loop3
SET j, [SCRN_END]
ADD j, 0x1
SET i, 3
:MINECRAFT_init_loop4
SET [x], [MC_BLOCK_CHARS+i]
ADD x, 1
IFG j, x
SET PC, MINECRAFT_init_loop4
SET i, 1
SET [MC_CURRENT_BLOCK], [MC_BLOCK_CHARS+i] ;show current block
SET x, [SCRN_START]
SET [x], [MC_CURRENT_BLOCK]
;initialize player
SET [MC_PLAYER_POS], [SCRN_START]
ADD [MC_PLAYER_POS], 0xF0
JSR MC_print_player
SET B, 0x100
;===========================================================
:MC_game_loop
JSR MC_in_air_check
JSR MC_water_flow
IFE [z], [MC_up_key]
JSR MC_jump
IFE [z], [MC_left_key]
JSR MC_move_left
IFE [z], [MC_right_key]
JSR MC_move_right
IFE [z], [MC_do_up_key]
JSR MC_do_up
IFE [z], [MC_do_down_key]
JSR MC_do_down
IFE [z], [MC_do_left_key]
JSR MC_do_left
IFE [z], [MC_do_right_key]
JSR MC_do_right
IFE [z], [MC_swap_key]
JSR MC_swap_item
IFE [z], [MC_reset_key]
SET PC, MC_reset_game
IFE [z], [MC_quit_key]
SET PC, MC_game_exit
SUB B, 1
IFE B, 0
JSR MC_reset_input
SET PC, MC_game_loop
;===================================================
:MC_game_exit
SET [z], 0
SET J, POP
SET I, POP
SET Z, POP
SET Y, POP
SET X, POP
SET C, POP
SET B, POP
SET PC, POP
;=====================================================
;prevents input buffer from getting full
;while preventing issues with input
:MC_reset_input
SET [z], 0
SET B, 0x100
SET PC, POP
;=====================================================
:MC_jump
SET [z], 0
:MC_jump_water
SET J, [MC_PLAYER_POS]
SET X, [MC_PLAYER_POS]
SUB J, 0x20
IFG [SCRN_START], j ;at the top?
SET PC, POP
IFE [j], [MC_WATER_CHAR]
SET PC, MC_jump2
IFN [j], [MC_SKY_CHAR] ;block above?
SET PC, POP
IFG [MC_FALL_TIME], C ;can't if in the air
SET PC, POP
:MC_jump2
AND [X], 0x0F00
SET [MC_PLAYER_POS], j
JSR MC_print_player
SUB C, 1
SET PC, POP
;=====================================================
:MC_move_left
SET [z], 0
SET j, [MC_PLAYER_POS]
SET x, [MC_PLAYER_POS]
MOD j, 0x20
IFE j, 0
SET PC, MC_wrap_left ;at left edge?
SET J, [MC_PLAYER_POS]
SUB j, 1
IFE [j], [MC_SKY_CHAR] ;block above?
SET PC, MC_move_left2
IFN [j], [MC_WATER_CHAR]
SET PC, POP
:MC_move_left2
AND [X], 0x0F00
SET [MC_PLAYER_POS], j
JSR MC_print_player
SET PC, POP
:MC_wrap_left
SET J, [MC_PLAYER_POS]
ADD J, 0x1F
IFE [j], [MC_SKY_CHAR] ;block above?
SET PC, MC_move_leftw2
IFN [j], [MC_WATER_CHAR]
SET PC, POP
:MC_move_leftw2
AND [X], 0x0F00
SET [MC_PLAYER_POS], j
JSR MC_print_player
SET PC, POP
;====================================================
:MC_move_right
SET [z], 0
SET x, [MC_PLAYER_POS]
SET j, [MC_PLAYER_POS]
MOD j, 0x20
IFE j, 0x1F ;at right edge?
SET PC, MC_wrap_right
SET J, [MC_PLAYER_POS]
ADD j, 1
IFE [j], [MC_SKY_CHAR] ;block above?
SET PC, MC_move_right2
IFN [j], [MC_WATER_CHAR]
SET PC, POP
:MC_move_right2
AND [X], 0x0F00
SET [MC_PLAYER_POS], j
JSR MC_print_player
SET PC, POP
:MC_wrap_right
SET J, [MC_PLAYER_POS]
SUB J, 0x1F
IFE [j], [MC_SKY_CHAR] ;block above?
SET PC, MC_move_rightw2
IFN [j], [MC_WATER_CHAR]
SET PC, POP
:MC_move_rightw2
AND [X], 0x0F00
SET [MC_PLAYER_POS], j
JSR MC_print_player
SET PC, POP
;======================================================
:MC_do_up
SET [z], 0
SET J, [MC_PLAYER_POS]
SUB J, 0x20
IFG [SCRN_START], j ;at the top?
SET PC, POP
IFE [SCRN_START], j ;current block?
SET PC, POP
IFN [j], [MC_SKY_CHAR] ;is it a block?
SET PC, MC_do_is_block
SET [j], [MC_CURRENT_BLOCK]
SET PC, POP
;======================================================
:MC_do_down
SET [z], 0
SET J, [MC_PLAYER_POS]
ADD J, 0x20
IFG J, [SCRN_END] ;at the bottom?
SET PC, POP
IFN [j], [MC_SKY_CHAR] ;is it a block?
SET PC, MC_do_is_block
SET [j], [MC_CURRENT_BLOCK]
SET PC, POP
;======================================================
:MC_do_left
SET [z], 0
SET j, [MC_PLAYER_POS]
MOD j, 0x20
IFE j, 0x0 ;at left edge?
SET PC, MC_do_wrap_left
SET J, [MC_PLAYER_POS]
SUB j, 1
IFE j, [SCRN_START] ;current block?
SET PC, POP
IFN [j], [MC_SKY_CHAR] ;block there?
SET PC, MC_do_is_block
SET [j], [MC_CURRENT_BLOCK]
SET PC, POP
:MC_do_wrap_left
SET J, [MC_PLAYER_POS]
ADD J, 0x1F
IFN [J], [MC_SKY_CHAR] ;block there?
SET PC, MC_do_is_block
SET [j], [MC_CURRENT_BLOCK]
SET PC, POP
;======================================================
:MC_do_right
SET [z], 0
SET j, [MC_PLAYER_POS]
MOD j, 0x20
IFE j, 0x1F ;at right edge?
SET PC, MC_do_wrap_right
SET J, [MC_PLAYER_POS]
ADD j, 1
IFN [j], [MC_SKY_CHAR] ;block there?
SET PC, MC_do_is_block
SET [j], [MC_CURRENT_BLOCK]
SET PC, POP
:MC_do_wrap_right
SET J, [MC_PLAYER_POS]
SUB J, 0x1F
IFE j, [SCRN_START] ;current block?
SET PC, POP
IFN [J], [MC_SKY_CHAR] ;block there?
SET PC, MC_do_is_block
SET [j], [MC_CURRENT_BLOCK]
SET PC, POP
;======================================================
:MC_do_is_block
SET [j], [MC_SKY_CHAR]
SET PC, POP
;======================================================
:MC_swap_item
SET [z], 0
SET j, MC_BLOCK_CHARS
:MC_swap_item_loop
IFE [MC_CURRENT_BLOCK], [j]
SET PC, MC_swap_item_x
ADD j, 1
SET PC, MC_swap_item_loop
:MC_swap_item_x
ADD j, 1
IFE [j], 0
SET PC, MC_swap_item_reset
SET [MC_CURRENT_BLOCK], [j]
SET x, [SCRN_START]
SET [x], [MC_CURRENT_BLOCK]
SET PC, POP
:MC_swap_item_reset
SET [MC_CURRENT_BLOCK], [MC_BLOCK_CHARS]
SET x, [SCRN_START]
SET [x], [MC_CURRENT_BLOCK]
SET PC, POP
;======================================================
:MC_reset_game
SET [z], 0
SET PC, MC_reset_point
;======================================================
;checks if player is in the air
:MC_in_air_check
SET X, [MC_PLAYER_POS]
ADD X, 0x20
IFG x, [SCRN_END] ;at bottom?
SET PC, POP
IFE [x], [MC_SKY_CHAR] ;ground below?
SET PC, MC_in_air
IFE [x], [MC_WATER_CHAR]
SET PC, MC_in_air
SET C, [MC_FALL_TIME]
SET PC, pop
:MC_in_air
SUB C, 1
IFN C, 0 ;time up?
SET PC, POP
;dont put anything here
:MC_fall
SET x, [MC_PLAYER_POS]
AND [x], 0x0F00
ADD [MC_PLAYER_POS], 0x20
SET C, [MC_FALL_TIME]
JSR MC_print_player
SET PC, POP
:MC_in_air_check_on_ground
SET C, [MC_FALL_TIME]
SET PC, POP
;======================================================
:MC_print_player
SET X, [MC_PLAYER_POS]
BOR [x], [MC_PLAYER_CHAR]
SET PC, POP
;======================================================
:MC_water_flow
IFE Y, 0
SET PC, MC_water_check
SUB Y, 1 ;only does this every so often
SET PC, POP
:MC_water_check
JSR malloc
SET j, a
SET PUSH, a ;a contains pointer to dynamic mem
SET i, [SCRN_START]
ADD i, 1
:MC_water_check_loop
IFE [i], [MC_BLOCK_CHARS] ;are there any sources or water?
JSR MC_flow
SET a, [i]
AND a, 0x0F00
IFE a, [MC_WATER_CHAR]
JSR MC_flow
ADD i, 1
IFG [SCRN_END], i
SET PC, MC_water_check_loop
SET [j], 0
SET A, POP ;making sure last entry is zero
SET j, a
:MC_water_print_loop
SET i, [j]
IFE i, 0
SET PC, MC_flow_done
AND [i], 0xF0FF
BOR [i], [MC_WATER_CHAR]
ADD j, 1
SET PC, MC_water_print_loop
:MC_flow_done
JSR free
SET Y, [MC_FALL_TIME]
SET PC, POP
:MC_flow_save
SET [j], x ;saves location on screen
ADD j, 1
SET PC, POP
;=====================================================
:MC_flow
SET x, i
ADD x, 0x20
IFG x, [SCRN_END] ;at bottom?
SET PC, MC_flow_left
SET a, [x]
AND a, 0x0F00
IFE a, [MC_SKY_CHAR] ;block below?
JSR MC_flow_save
:MC_flow_left
SET x, i
MOD x, 0x20
IFE x, 0x0 ;at left edge?
SET PC, MC_flow_wrap_left
SET x, i
SUB x, 1
IFE x, [SCRN_START] ;current block?
SET PC, MC_flow_right
SET a, [x]
AND a, 0x0F00
IFE a, [MC_SKY_CHAR] ;block below?
JSR MC_flow_save
SET PC, MC_flow_right
:MC_flow_wrap_left
SET x, i
ADD x, 0x1F
SET a, [x]
AND a, 0x0F00
IFE a, [MC_SKY_CHAR] ;block below?
JSR MC_flow_save
:MC_flow_right
SET x, i
MOD x, 0x20
IFE x, 0x1F ;at right edge?
SET PC, MC_flow_wrap_right
SET x, i
ADD x, 1
SET a, [x]
AND a, 0x0F00
IFE a, [MC_SKY_CHAR] ;block below?
JSR MC_flow_save
SET PC, POP
:MC_flow_wrap_right
SET x, i
SUB x, 0x1F
IFE x, [SCRN_START] ;current block?
SET PC, POP
SET a, [x]
AND a, 0x0F00
IFE a, [MC_SKY_CHAR] ;block below?
JSR MC_flow_save
SET PC, POP
;==============================================================