Selaa lähdekoodia

Implementing reduced glitch parser

Yann Weber 5 vuotta sitten
vanhempi
commit
8a30614b02
3 muutettua tiedostoa jossa 452 lisäystä ja 26 poistoa
  1. 2
    2
      Makefile
  2. 5
    0
      utils.asm
  3. 445
    24
      yaglitch.asm

+ 2
- 2
Makefile Näytä tiedosto

@@ -1,4 +1,5 @@
1 1
 DEBUG=0
2
+TARGET=yaglitch
2 3
 
3 4
 NASM=nasm
4 5
 
@@ -6,11 +7,10 @@ ifeq ($(DEBUG), 0)
6 7
 	ASFLAGS=-felf64
7 8
 	LDFLAGS=-s -melf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -lSDL
8 9
 else
9
-	ASFLAGS=-felf64 -g -F dwarf -l bf.lst
10
+	ASFLAGS=-felf64 -g -F dwarf -l $(TARGET).lst
10 11
 	LDFLAGS=-g -melf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -lSDL
11 12
 endif
12 13
 
13
-TARGET=yaglitch
14 14
 OBJ=$(TARGET).o
15 15
 SRC=$(TARGET).asm
16 16
 

+ 5
- 0
utils.asm Näytä tiedosto

@@ -0,0 +1,5 @@
1
+%macro def_str 2
2
+	%1: db %2
3
+	__%1_len:
4
+	%define %1_len (__%1_len - %1)
5
+%endmacro

+ 445
- 24
yaglitch.asm Näytä tiedosto

@@ -18,23 +18,60 @@
18 18
 [bits 64]
19 19
 
20 20
 %include "sdl.asm"
21
+%include "utils.asm"
21 22
 
22 23
 %define STACK_SZ 256
24
+%define MAX_FILE_SZ (16 * 17) ; 16 lines of 16 chars + newline chr
25
+%define NL "!"
23 26
 
24 27
 section .data
25 28
 
26
-audiospec_wanted:
27
-	dd 48000 ; freq
28
-	dw AUDIO_U8 ; format
29
-	db 1 ; channels
30
-	db 0 ; silence
31
-	dw 4096 ; samples
32
-	dd 0 ; size
33
-dw 0 ; allign
34
-	dq audio_cllbck
35
-	dq 0 ; userdata
29
+	op_ptrs:
30
+		dq OP.t
31
+		dq OP.put
32
+		dq OP.drop
33
+		dq OP.mul
34
+		dq OP.div
35
+		dq OP.add
36
+		dq OP.sub
37
+		dq OP.mod
38
+		dq 0 ; syntax error
39
+		dq OP.lshift
40
+		dq OP.rshift
41
+		dq OP.and
42
+		dq OP.or
43
+		dq OP.xor
44
+		dq OP.not
45
+		dq OP.dup
46
+		dq OP.pick
47
+		dq OP.swap
48
+		dq OP.lt
49
+		dq OP.gt
50
+		dq OP.eq
51
+
52
+
53
+	audiospec_wanted:
54
+		dd 48000 ; freq
55
+		dw AUDIO_U8 ; format
56
+		db 1 ; channels
57
+		db 0 ; silence
58
+		dw 4096 ; samples
59
+		dd 0 ; size
60
+	dw 0 ; allign
61
+		dq audio_cllbck
62
+		dq 0 ; userdata
36 63
 	
37 64
 
65
+	def_str usage, "Usage : "
66
+	def_str opts, {" FILE.glitch",0xA}
67
+	def_str openerr, {'Error opening file : "', 0xA}
68
+	def_str syntax_error, {"Syntax error", 0xA}
69
+	def_str bigline_error, {"Line with more than 16 tokens !", 0xA}
70
+	def_str nl_error, {"Character \n is not the last one", 0xA}
71
+
72
+	;
73
+	; Old stuff for first oscillator
74
+	;
38 75
 	val: db 0
39 76
 	dir: db 0
40 77
 
@@ -44,11 +81,17 @@ dw 0 ; allign
44 81
 
45 82
 section .bss
46 83
 
84
+	; glitch name (1st line) : len 16 + \0
85
+	glitch_name: resb 17
86
+	; program internal repr for stack machine
87
+	; each token uses 2 words : 1st for the callback address 2nd for
88
+	; optional value
89
+	stack_pgm: resq 16 * 16 * 2
47 90
 	; stack machine ring buffer
48 91
 	stack_buff: resd STACK_SZ
49 92
 	; top of stack pointer
50 93
 	tosp: resq 1
51
-	; stack machine memory
94
+	; stack machine rgister
52 95
 	t: resb 1
53 96
 	; audiospec returned from SDL
54 97
 	audiospec_recv: resb 32
@@ -57,20 +100,207 @@ section .text
57 100
 global _start
58 101
 _start:
59 102
 
103
+	; checking args
104
+	mov rcx, [rsp]
105
+	cmp rcx, 2
106
+	jne exit.badarg
107
+
108
+	mov rax, 0x2 ; sys_open
109
+	mov rdi, [rsp+16] ; argv[1]
110
+	xor rsi, rsi ; no flags
111
+	xor rdx, rdx ; O_RDONLY
112
+	syscall
113
+	cmp rax, 0
114
+	jl exit.err_open
115
+	push rax  ; source fd
116
+
117
+	mov rax, 0xc ; brk
118
+	xor rdi, rdi
119
+	syscall
120
+	push rax ; heap start
121
+
122
+	mov rdi, rax
123
+	add rdi, MAX_FILE_SZ ; new heap end
124
+	mov rax, 0xc ; brk
125
+	syscall
126
+
127
+	pop rsi ; heap start
128
+	mov r14, rsi
129
+	xor rax, rax ; sys_read
130
+	mov rdi, [rsp] ; source_fd
131
+	mov rdx, MAX_FILE_SZ
132
+	syscall
133
+	cmp rax, 0
134
+	jl exit.err_open
135
+	mov r15, rax
136
+
137
+	mov rax, 0x3 ; sys_close
138
+	pop rdi ; source_fd
139
+	syscall
140
+
141
+	mov rbx, r15 ;read size
142
+	mov rdi, r14 ; heap start
143
+	add rdi, rbx ; heap end
144
+	mov r15, rdi
145
+	mov rax, 0xc ; brk
146
+	syscall ; shrink heap to read size
147
+
148
+	; init pgm space
149
+	mov rdi, stack_pgm
150
+	xor rax, rax
151
+	stosq
152
+	stosq
153
+
154
+	mov rsi, r14
155
+	push rsi
156
+	mov rdi, stack_pgm
157
+	xor r13, r13
158
+	xor rbx, rbx
159
+parse:
160
+	; go trhough file , with rsi current ptr, r15 file stop
161
+	; r13 will contain updated lineno and rbx chr in current line
162
+	; rdi store the destination pointer
163
+	; parse glitch name
164
+	mov rcx, 16
165
+	.name_loop:
166
+		inc rbx
167
+		lodsb
168
+		test al, al ; EOF
169
+		jz .no_nl
170
+		cmp al, 0xA
171
+		je .trailing_nl
172
+		cmp al, "!" ; EOL
173
+		je .name_end
174
+		cmp al, "_"
175
+		je .chrname
176
+		cmp al, "0"
177
+		jl exit.syntax_error
178
+		cmp al, "9"
179
+		jle .chrname
180
+		cmp al, "a"
181
+		jl exit.syntax_error
182
+		cmp al, "z"
183
+		jg exit.syntax_error
184
+		.chrname:
185
+			stosb
186
+			loop .name_loop
187
+	jmp exit.bigline
188
+	.name_end:
189
+		inc r13 ; lineno
190
+		xor rbx, rbx
191
+		xor al, al
192
+		stosb
193
+
194
+	; parsing tokens
195
+	xor edx, edx ; 32bits numeric token value
196
+	xor eax, eax
197
+	xor r10, r10 ; numeric token flag (1 mean in numeric token)
198
+	xor r11, r11 ; numeric token len
199
+	mov rcx, r15
200
+	sub rcx, r14
201
+	.parse_loop:
202
+		inc rbx
203
+		lodsb
204
+		test al, al
205
+		jz .no_nl
206
+		cmp al, 0xA
207
+		je .trailing_nl
208
+		cmp al, "!" ; EOL
209
+		je .next_line
210
+		cmp al, "." ; token separator
211
+		je .next_token
212
+		cmp al, "0"
213
+		jl exit.syntax_error
214
+		cmp al, "9"
215
+		jle .dec_token
216
+		cmp al, "A"
217
+		jl exit.syntax_error
218
+		cmp al, "F"
219
+		jle .hex_token
220
+		jmp .op_match ; allowing loop near jump
221
+		.end_op:
222
+			stosq
223
+			xor rax, rax
224
+			stosq
225
+			jmp .next_token
226
+
227
+		.hex_token:
228
+			sub al, "F"
229
+			jmp .numeric_token
230
+		.dec_token:
231
+			sub al, "0"
232
+		.numeric_token:
233
+			cmp r11, 8
234
+			je exit.syntax_error ; Numeric constant OF
235
+			inc r11
236
+			mov r10, 1
237
+			shl edx, 8
238
+			add edx, eax
239
+		.next_token:
240
+			;inc rbx ; chrno
241
+		loop .parse_loop
242
+
243
+		.next_line:
244
+			inc r13
245
+			xor rbx, rbx
246
+		loop .parse_loop
247
+	.trailing_nl:
248
+		; check that NL is the last chr, else syntax error
249
+		cmp rsi, r15
250
+		jne exit.nl_not_last
251
+		jmp .parse_end
252
+	.op_match:
253
+		; allow loop .parse_loop near jump
254
+		cmp al, "a"
255
+		jl exit.syntax_error
256
+		cmp al, "u"
257
+		jg exit.syntax_error
258
+		; OP shortand matching
259
+		; checking for previous numeric token to write
260
+		test r10, r10
261
+		jz .match_op
262
+		push rax
263
+		; add previous numeric token
264
+		mov rax, OP.numeric
265
+		stosq
266
+		xor rax, rax
267
+		xchg rax, r11
268
+		stosq
269
+		pop rax
270
+		.match_op:
271
+			sub al, "a"
272
+			shl rax, 3 ; mul by 8 (size of ptr)
273
+			add rax, op_ptrs
274
+			mov rax, [rax]
275
+			test rax, rax
276
+			jz exit.syntax_error
277
+			jmp .end_op
278
+	
279
+	.no_nl: ; TODO : print warning
280
+
281
+	.parse_end:
282
+
283
+	; clean heap
284
+	mov rax, 0xc
285
+	pop rdi
286
+	syscall
287
+	
288
+
60 289
 	mov rdi, 0x0000FFFF
61 290
 	call SDL_Init
62 291
 
63
-	mov rdi, 256
64
-	mov rsi, 255
65
-	mov rdx, 32
66
-	;mov rcx, SDL_SWSURFACE
67
-	xor rcx, rcx
68
-	call SDL_SetVideoMode
69
-
70 292
 	mov rdi, audiospec_wanted
71 293
 	mov rsi, audiospec_recv
72 294
 	call SDL_OpenAudio
73 295
 
296
+	;mov rdi, 256
297
+	;mov rsi, 255
298
+	;mov rdx, 32
299
+	;;mov rcx, SDL_SWSURFACE
300
+	;xor rcx, rcx
301
+	;call SDL_SetVideoMode
302
+
303
+	;start audio
74 304
 	xor rdi, rdi
75 305
 	call SDL_PauseAudio
76 306
 
@@ -81,15 +311,206 @@ _start:
81 311
 exit:
82 312
 	call SDL_Quit
83 313
 
314
+	xor rdi, rdi
315
+	mov rax, 0x3c
316
+	syscall
317
+
318
+
319
+	.err_open:
320
+		mov rax, 1
321
+		mov rdi, 2
322
+		mov rsi, openerr
323
+		mov rdx, openerr_len - 1
324
+		syscall
325
+
326
+		mov rdi, [rsp+16]
327
+		push rdi
328
+		call strlen
329
+
330
+		mov rdx, rax
331
+		mov rax, 1
332
+		mov rdi, 2
333
+		pop rsi
334
+		syscall
335
+
336
+		mov rax, 1
337
+		mov rdi, 2
338
+		mov rsi, openerr + openerr_len - 2
339
+		mov rdx, 2
340
+		syscall
341
+	.badarg:
342
+		mov rax, 1
343
+		mov rdi, 2
344
+		mov rsi, usage
345
+		mov rdx, usage_len
346
+		syscall
347
+
348
+		mov rdi, [rsp+8]
349
+		call strlen
350
+		mov rdx, rax
351
+
352
+		mov rax, 1
353
+		mov rdi, 2
354
+		mov rsi, [rsp+8]
355
+		syscall
356
+
357
+		mov rax, 1
358
+		mov rdi, 2
359
+		mov rsi, opts
360
+		mov rdx, opts_len
361
+		syscall
362
+		mov rdi, 1
363
+	.exit_err: ; with rdi error code
364
+		mov rax, 0x3c ; exit
365
+		syscall
366
+
367
+	; expect : r13 lineno, rbx chr num in line
368
+	; TODO: real error message
369
+	.nl_not_last:
370
+		mov rsi, nl_error
371
+		mov rsi, nl_error_len
372
+		push qword 3
373
+		jmp exit.parse_error
374
+	.syntax_error:
375
+		mov rsi, syntax_error
376
+		mov rdx, syntax_error_len
377
+		push qword 2
378
+		jmp exit.parse_error
379
+	.bigline:
380
+		mov rsi, bigline_error
381
+		mov rdx, bigline_error_len
382
+		push qword 2
383
+		jmp exit.parse_error
384
+	.parse_error:
385
+		; print error lineno & chrno
386
+		push rsi
387
+		push rdx
388
+		push rbx
389
+		mov rdi, "chr:0x"
390
+		mov rsi, 6
391
+		call short_err
392
+		mov rdi, r15
393
+		sub rdi, [rsp+(8*3)]
394
+		call print_hnum
395
+		mov rdi, ",line:0x"
396
+		mov rsi, 8
397
+		call short_err
398
+		mov rdi, r13
399
+		mov rsi, 2
400
+		call print_hnum
401
+		mov rdi, ",col:0x"
402
+		mov rsi, 7
403
+		call short_err
404
+		pop rdi
405
+		mov rsi, 2
406
+		call print_hnum
407
+		mov rdi, ` :\t`
408
+		mov rsi, 3
409
+		call short_err
410
+		pop rdx
411
+		pop rsi
412
+		mov rax, 1
413
+		mov rdi, 2
414
+		syscall
415
+		pop rdi
416
+		jmp exit.exit_err
417
+
418
+short_err:
419
+	; rdi is the message (less than 8 chr)
420
+	; rsi is message len
421
+	push rdi
422
+	mov rdx, rsi
423
+	mov rsi, rsp
84 424
 	mov rax, 1
85
-	mov rdi, rax
86
-	mov rsi, nl
87
-	mov rdx, rax
88
-	syscall ; print nl
425
+	mov rdi, 1
426
+	syscall
427
+	pop rdi
428
+	ret
89 429
 
90
-	xor rdi, 0
91
-	mov rax, 0x3c
430
+OP:
431
+	.numeric:
432
+	.t:
433
+	.put:
434
+	.drop:
435
+	.mul:
436
+	.div:
437
+	.add:
438
+	.sub:
439
+	.mod:
440
+	.lshift:
441
+	.rshift:
442
+	.and:
443
+	.or:
444
+	.xor:
445
+	.not:
446
+	.dup:
447
+	.pick:
448
+	.swap:
449
+	.lt:
450
+	.gt:
451
+	.eq:
452
+
453
+strlen:
454
+	; rdi containing str pointer
455
+	; rax will contain strlen
456
+	mov rsi, rdi
457
+	xor rdx, rdx
458
+	pushf
459
+	cld
460
+	.loop:
461
+		inc rdx
462
+		lodsb
463
+		mov cl, al
464
+		test al, al
465
+		jnz .loop
466
+	mov rax, rdx
467
+	popf
468
+	ret
469
+
470
+print_hnum:
471
+	; rdi : number to print
472
+	; rsi : output fd
473
+	pushf
474
+	mov rax, rdi
475
+	xor rcx, rcx
476
+	push rcx ; using stack as buffer
477
+	std
478
+	lea rdi, [rsp + 8]
479
+	.loop:
480
+		test rax, rax
481
+		jz .endloop
482
+		inc rcx
483
+		inc rcx
484
+		push rax
485
+		and al, 0x0F
486
+		call .al2digit
487
+		stosb
488
+		mov rax, [rsp]
489
+		shr al, 4
490
+		call .al2digit
491
+		stosb
492
+		pop rax
493
+		shr rax, 8
494
+		jmp .loop
495
+	.endloop:
496
+	inc rcx
497
+	mov rax, 1
498
+	xchg rdi, rsi
499
+	;mov rdi, rsi
500
+	;mov rsi, rsp
501
+	mov rdx, rcx
92 502
 	syscall
503
+	pop rax
504
+	popf
505
+	ret
506
+	.al2digit:
507
+		cmp al, 9
508
+		jg .hex
509
+		add al, "0"
510
+		ret
511
+		.hex:
512
+		add al, "A" - 10
513
+		ret
93 514
 
94 515
 audio_cllbck:
95 516
 	; rdi -> *userdata

Loading…
Peruuta
Tallenna