rev: tip vgmplay-msx/src/Application.asm -rw-r--r-- 7.7 KiB View raw Log this file
e8a0ea05ce5aLaurens Holst Added tag release-1.3 for changeset d53f33dc6397 3 months ago
                                                                                
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
;
; Top-level application program class
;
Application_VGMREADER_BASE_ADDRESS: equ 8000H

Application: MACRO
	freebieSegment:
		MapperSegment
	cli:
		CLI
	mappedBufferFactory:
		StaticFactory MappedBuffer_instance, MappedBuffer_Construct, MappedBuffer_Destruct
	loader:
		MappedBufferLoader
	reader:
		MappedReader
	drivers:
		Drivers
	vgm:
		VGM
	vgmFactory:
		StaticFactory vgm, VGM_Construct, VGM_Destruct
	player:
		Player
	playerFactory:
		StaticFactory player, Player_Construct, Player_Destruct
	_size:
	ENDM

;
Application_Main:
	ld ix,Mapper_instance
	call Mapper_Construct

	ld hl,Application_MainContinue
	call System_TryCall

	ld ix,Mapper_instance
	call Mapper_Destruct
	jp System_Rethrow

Application_MainContinue:
	call Application_CheckStack

	call VDP_InitMirrorsOnVDPUpgrades

	ld hl,Application_welcome
	call System_Print

	ld ix,Heap_main
	call Heap_Construct
	ld bc,HEAP_SIZE
	ld de,HEAP
	call Heap_Free

	call Player_InitCommandsJumpTable

	ld ix,Application_instance
	call Application_Construct

	push ix
	ld hl,Application_EnterMainLoop
	call System_TryCall
	pop ix

	call Application_Destruct

	call System_Rethrow
	jp Application_CheckMemoryLeak

; ix = this
; ix <- this
Application_Construct:
	ld h,80H
	call Mapper_instance.GetPH
	ld (ix + Application.freebieSegment + MapperSegment.segment),a
	call Memory_GetSlot
	ld (ix + Application.freebieSegment + MapperSegment.slot),a
	push ix
	call Application_GetCLI
	call CLI_Construct
	pop ix
	push ix
	call Application_GetDrivers
	call Drivers_Construct
	pop ix
	ret

; ix = this
; ix <- this
Application_Destruct:
	push ix
	call Application_GetPlayerFactory
	call StaticFactory_Destroy
	pop ix
	push ix
	call Application_GetVGMFactory
	call StaticFactory_Destroy
	pop ix
	push ix
	call Application_GetMappedBufferFactory
	call StaticFactory_Destroy
	pop ix
	push ix
	call Application_GetCLI
	call CLI_Destruct
	pop ix
	push ix
	call Application_GetDrivers
	call Drivers_Destruct
	pop ix
	ret

; ix = this
; ix <- Command-line interface
Application_GetCLI:
	ld de,Application.cli
	add ix,de
	ret

; ix = this
; ix <- mapped buffer factory
Application_GetMappedBufferFactory:
	ld de,Application.mappedBufferFactory
	add ix,de
	ret

; ix = this
; ix <- loader
Application_GetLoader:
	ld de,Application.loader
	add ix,de
	ret

; ix = this
; ix <- reader
Application_GetReader:
	ld de,Application.reader
	add ix,de
	ret

; ix = this
; ix <- driver manager
Application_GetDrivers:
	ld de,Application.drivers
	add ix,de
	ret

; ix = this
; ix <- VGM
Application_GetVGM:
	ld de,Application.vgm
	add ix,de
	ret

; ix = this
; ix <- VGM factory
Application_GetVGMFactory:
	ld de,Application.vgmFactory
	add ix,de
	ret

; ix = this
; ix <- player
Application_GetPlayer:
	ld de,Application.player
	add ix,de
	ret

; ix = this
; ix <- player factory
Application_GetPlayerFactory:
	ld de,Application.playerFactory
	add ix,de
	ret

; ix = this
Application_EnterMainLoop:
	call Application_CheckUsageInstructions
	call Application_LoadFile
	call Application_ConstructVGM
	call Application_ConstructPlayer
	call Application_LoadDataBlocks
	jp Application_Play

; ix = this
Application_CheckUsageInstructions:
	push ix
	call Application_GetCLI
	call CLI_GetFileInfoBlock
	ld hl,Application_usageInstructions
	call z,System_ThrowExceptionWithMessage
	pop ix
	ret

; ix = this
Application_LoadFile:
	call Application_PrintLoading
	call Application_ConstructMappedBuffer
	call Application_IsVGZ
	jp z,Application_InflateFile
	push ix
	call Application_GetCLI
	call CLI_GetFileInfoBlock
	ex (sp),ix
	pop hl
	push ix
	call Application_GetLoader
	ld de,MappedBuffer_instance
	call MappedBufferLoader_Construct
	push ix
	ld hl,MappedBufferLoader_Load
	call System_TryCall
	pop ix
	call MappedBufferLoader_Destruct
	pop ix
	jp System_Rethrow

; ix = this
Application_ConstructMappedBuffer:
	push ix
	ld a,(ix + Application.freebieSegment + MapperSegment.segment)
	ld b,(ix + Application.freebieSegment + MapperSegment.slot)
	call Application_GetMappedBufferFactory
	call StaticFactory_Create
	pop ix
	ret

; ix = this
; f <- z: is vgz
Application_IsVGZ:
	push ix
	call Application_GetCLI
	call CLI_GetFileInfoBlock
	call FileInfoBlock_GetName
	pop ix
	ld a,0
	ld bc,256
	cpir
	dec hl
	dec hl
	ld a,(hl)
	and 11011111B  ; upper-case
	cp "Z"
	ret nz
	dec hl
	ld a,(hl)
	and 11011111B  ; upper-case
	cp "G"
	ret nz
	dec hl
	ld a,(hl)
	and 11011111B  ; upper-case
	cp "V"
	ret nz
	dec hl
	ld a,(hl)
	cp "."
	ret

; ix = this
Application_InflateFile:
	push ix
	call Application_GetCLI
	call CLI_GetFileInfoBlock
	ld e,ixl
	ld d,ixh
	pop ix
	ld hl,READBUFFER
	ld bc,READBUFFER_SIZE
	push ix
	call FileReader_class.New
	call FileReader_Construct
	push ix
	ld a,GZIP_CRC32 ? -1 : 0
	call GzipArchive_class.New
	call GzipArchive_Construct
	push ix
	call Mapped32KWriter_class.New
	ld de,MappedBuffer_instance
	call Mapped32KWriter_Construct
	ld e,ixl
	ld d,ixh
	ex (sp),ix
	push ix
	ld hl,GzipArchive_Extract
	call System_TryCall
	pop ix
	ex (sp),ix
	call Mapped32KWriter_Destruct
	call Mapped32KWriter_class.Delete
	pop ix
	call GzipArchive_Destruct
	call GzipArchive_class.Delete
	pop ix
	call FileReader_Destruct
	call FileReader_class.Delete
	pop ix
	jp System_Rethrow

; ix = this
Application_PrintLoading:
	ld hl,Application_loadingFile
	call System_Print
	push ix
	call Application_GetCLI
	call CLI_GetFileInfoBlock
	call FileInfoBlock_GetName
	pop ix
	call System_Print
	ld hl,Application_dotDotDot
	jp System_Print

; ix = this
Application_ConstructVGM:
	push ix
	push ix
	call Application_GetDrivers
	ex (sp),ix
	push ix
	call Application_GetReader
	ld de,MappedBuffer_instance
	ld hl,Application_VGMREADER_BASE_ADDRESS
	call MappedReader_Construct
	ex (sp),ix
	call Application_GetVGMFactory
	pop de
	pop hl
	call StaticFactory_Create
	call VGM_PrintInfo
	pop ix
	ret

; ix = this
Application_ConstructPlayer:
	push ix
	call Application_GetCLI
	ld a,(ix + CLI.loops)
	pop ix
	push ix
	push ix
	call Application_GetVGM
	ex (sp),ix
	call Application_GetPlayerFactory
	pop de
	call StaticFactory_Create
	pop ix
	ret

Application_LoadDataBlocks:
	push ix
	call Application_GetPlayer
	call Player_HasDataBlocks
	ld hl,Application_loadingSamples
	call z,System_Print
	call z,Player_LoadDataBlocks
	pop ix
	ret

; ix = this
Application_Play:
	ld hl,Application_playing
	call System_Print
	call Application_EnterBlackout
	push ix
	call Application_GetPlayer
	ld hl,Player_Play
	call System_TryCall
	pop ix
	jp Application_ExitBlackout

; If enabled, blacks out the screen to minimise audio output interference.
; ix = this
Application_EnterBlackout:
	push ix
	call Application_GetCLI
	ld a,(ix + CLI.blackout)
	pop ix
	and a
	ret z
	ld a,00H
	call VDP_SetColor
	jp VDP_EnableBlank

; ix = this
Application_ExitBlackout:
	ld a,(VDP_MIRROR_0 + 7)
	call VDP_SetColor
	jp VDP_DisableBlank

; Check if the stack is well above the heap
Application_CheckStack:
	ld hl,-(HEAP + HEAP_SIZE + STACK_SIZE)
	add hl,sp
	ld hl,Application_insufficientTPAError
	call nc,System_ThrowExceptionWithMessage
	ret

; Check if the heap capacity matches the free space
Application_CheckMemoryLeak:
	ld ix,Heap_main
	call Heap_GetFreeSpace
	ld hl,HEAP_SIZE
	and a
	sbc hl,bc
	call nz,System_ThrowException
	ret

;
	SECTION RAM

Application_instance: Application

	ENDS

Application_welcome:
	db "VGMPlay 1.3 by Grauw",13,10,10,0

Application_loadingFile:
	db "Loading ",0

Application_dotDotDot:
	db "...",13,10,0

Application_loadingSamples:
	db 13,10,"Loading samples...",13,10,0

Application_playing:
	db 13,10,"Playing...",13,10,0

Application_insufficientTPAError:
	db "Insufficient TPA space.",13,10,0

Application_usageInstructions:
	db "Usage: vgmplay [options] <file.vgm>",13,10
	db 13,10
	db "Options:",13,10
	db "  /l  Number of playback loops. Default: 2.",13,10
	db "  /b  Enter blackout mode during playback.",13,10,0