Browse Source

Initial commit

Yann Weber 5 years ago
commit
a10dcfac9b
4 changed files with 597 additions and 0 deletions
  1. 346
    0
      FORMAT-draft-erlehmann
  2. 28
    0
      Makefile
  3. 44
    0
      sdl.asm
  4. 179
    0
      yaglitch.asm

+ 346
- 0
FORMAT-draft-erlehmann View File

@@ -0,0 +1,346 @@
1
+                                       Nils Dagsson Moskopp // erlehmann
2
+                                         http://dieweltistgarnichtso.net
3
+                                                           December 2011
4
+
5
+
6
+ The application/x-glitch Media Type for the glitch file format
7
+
8
+Status of This Memo
9
+
10
+   This memo provides information for the Internet community.  It does
11
+   not specify an Internet standard of any kind.  This memo is licensed
12
+   under the Creative Commons Share-Alike license, either Version 3, or,
13
+   at your option, any later version.
14
+
15
+Abstract
16
+
17
+   The glitch file format is a lightweight, text-based, language-
18
+   independent program interchange format. It is used to specify
19
+   programs to be executed on a stack machine that generates a
20
+   bitstream.
21
+
22
+   Outputs of such programs are commonly interpreted as audio waveforms.
23
+   Doing this, many programs have been found to produce interesting
24
+   musical results, referred to as “bytebeat” [BYTEBEAT].
25
+
26
+
27
+1.  Introduction
28
+
29
+   The glitch file format is a text format for the serialization of
30
+   programs generating bitstreams. It is derived from the implementation
31
+   of the iOS application Glitch Machine [GLITCHMACHINE].
32
+
33
+   Programs noted in the glitch file format (glitches) contain
34
+   instructions for a stack machine and integer constants. Its design
35
+   goals were for it to be minimal, portable and textual.
36
+
37
+   The glitch file format was intended to be usable in microblogging
38
+   applications. Those commonly limit textual messages to 140 characters
39
+   to force messages to be terse and discourage intelligent discourse
40
+   [HTML].
41
+
42
+1.1.  Conventions Used in This Document
43
+
44
+   The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
45
+   "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this
46
+   document are to be interpreted as described in [RFC2119].
47
+
48
+   The grammatical rules in this document are to be interpreted as
49
+   described in [RFC4234].
50
+
51
+2.   Glitch File Format Grammar
52
+
53
+   A glitch consists of sequences of characters. In general, a glitch is
54
+   written as follows:
55
+
56
+   <title>!<instructions>
57
+
58
+   Title names consist of a sequence of zero or more characters, with
59
+   an upper limit of 16 characters. The lower case letters "a"--"z",
60
+   digits and the character underscore ("_") are allowed.
61
+
62
+   Instructions consist of one or more lines, with an upper limit of 16
63
+   lines.
64
+
65
+   Lines start with an exclamation mark followed by a sequence of one or
66
+   more token, with an upper limit of 16 tokens.
67
+
68
+   A tokens can be an opcode, a number or a period (".").
69
+
70
+   Opcodes tokens consist of single characters. The upper case letters
71
+   "G"--"Z" and the lower case letters "a"--"z" are allowed.
72
+
73
+   Numbers tokens consist of one to eight characters. The upper case
74
+   letters "A"--"F" and the numbers "0"--"9" are allowed.
75
+
76
+   Periods tokens are used as spacers whenever two numbers would
77
+   otherwise be directly adjacent.
78
+
79
+2.1    ABNF for the Glitch File Format
80
+
81
+   alphalower     = %x61-7A
82
+   underscore     = %x5F
83
+   title          = 0*16(alphalower / digit / underscore)
84
+
85
+   opcode         = %x47-5A / %x61-7A
86
+   number         = 1*8hexdig
87
+   period         = %x2E
88
+   token          = opcode / number / period
89
+
90
+   newline        = %0x21
91
+   line           = newline 1*16token
92
+
93
+   instructions   = 1*16line
94
+   glitch         = title instructions [%x0A]
95
+
96
+
97
+2.2.   Advice for Implementors
98
+
99
+   For interoperability with implementations using fixed-width glyphs,
100
+   programs SHOULD NOT produce glitches with lines of more than 16
101
+   characters.
102
+
103
+   Programs interpreting glitches SHOULD output a warning when
104
+   encountering any title or line exceeding 16 characters. For
105
+   robustness, programs SHOULD try to interpret such input, by
106
+   truncating the title to 16 characters and splitting lines.
107
+
108
+   If a program automatically truncates or splits lines, it SHOULD
109
+   output a warning and SHOULD NOT let users save the changed content
110
+   without further user intervention.
111
+
112
+   Programs interpreting glitches MUST output a warning when
113
+   encountering a sequence of more than eight characters from the set of
114
+   the upper case letters "A"--"F" and the numbers "0"--"9" and SHOULD
115
+   stop interpretation.
116
+
117
+   Programs interpreting glitches MUST output a warning when
118
+   encountering any character other than "A"--"Z", "a"--"z", "0"--"9",
119
+   "_", ".", "!" or a single new line (LF) at the end of input and
120
+   SHOULD stop interpretation.
121
+
122
+   Warnings MUST NOT be output to the standard output.
123
+
124
+3.    Glitch Stack Machine
125
+
126
+3.1.   Stack Machine
127
+
128
+   Interpreting programs written in the glitch file format is done using
129
+   a virtual stack machine. The stack is a ring buffer with 256 cells
130
+   that can hold an unsigned 32-bit integer and are initialized to hold
131
+   a value of zero (0). In this context, setting a cell to a value means
132
+   setting it to that value truncated to the last 32bits.
133
+
134
+   A top-of-stack pointer (TOSP), holding an unsigned 8-bit integer,
135
+   points to the current cell. Pushing a value to the stack means adding
136
+   one (1) to the TOSP, then setting the current cell to that value.
137
+   Popping from the stack means getting the current value, then subtracting
138
+   one (1) from the TOSP.
139
+
140
+   The stack machine has a global register referred to as “t”.
141
+
142
+   […]
143
+
144
+3.2.   Tokenizer
145
+
146
+   […]
147
+
148
+3.3.   Opcodes
149
+
150
+   The following table details opcode names and their shorthands. For
151
+   brevity, abbreviations are used for the top-of-stack pointer (TOSP),
152
+   cell values (using stack indices within brackets “[” and “]”) and
153
+   values retrieved or computed in previous numbered steps (V1, V2 …).
154
+
155
+   Shorthand   Name           Description
156
+
157
+   a           T           1. Push the value of t to the stack.
158
+
159
+   b           PUT         1. Get [TOSP].
160
+                           2. Compute V1 modulo 256.
161
+                           3. Compute V2 plus 1.
162
+                           4. Get [TOSP + 1]
163
+                           5. Set [TOSP - V3] to V4.
164
+                           6. Pop the stack.
165
+
166
+   c           DROP        1. Pop from the stack.
167
+
168
+   d           MUL         1. Pop from the stack.
169
+                           2. Pop from the stack.
170
+                           3. Multiply V2 and V1.
171
+                           4. Push V3 to the stack.
172
+
173
+   e           DIV         1. Pop from the stack.
174
+                           2. Pop from the stack.
175
+                           3. Divide V2 by V1, in case of a division by
176
+                              zero yielding zero.
177
+                           4. Push V3 to the stack.
178
+
179
+   f           ADD         1. Pop from the stack.
180
+                           2. Pop from the stack.
181
+                           3. Add V2 and V1.
182
+                           4. Push V3 to the stack.
183
+
184
+   g           SUB         1. Pop from the stack.
185
+                           2. Pop from the stack.
186
+                           3. From V2, subtract V1.
187
+                           4. Push V3 to the stack.
188
+
189
+   h           MOD         1. Pop from the stack.
190
+                           2. Pop from the stack.
191
+                           3. Compute V2 modulo V1, in case of a
192
+                              division by zero yielding zero.
193
+                           4. Push V3 to the stack.
194
+
195
+   j           LSHIFT      1. Pop from the stack.
196
+                           2. Pop from the stack.
197
+                           3. Compute V2 bit-shifted to the left by V1,
198
+                              shifting in zeros, in case of an overflow
199
+                              yielding zero.
200
+                           4. Push V3 to the stack.
201
+
202
+   k           RSHIFT      1. Pop from the stack.
203
+                           2. Pop from the stack.
204
+                           3. Compute V2 bit-shifted to the right by V1,
205
+                              shifting in zeros, in case of an overflow
206
+                              yielding zero.
207
+                           4. Push V3 to the stack.
208
+
209
+   l           AND         1. Pop from the stack.
210
+                           2. Pop from the stack.
211
+                           3. Compute a bitwise AND of V2 and V1.
212
+                           4. Push V3 to the stack.
213
+
214
+   m           OR          1. Pop from the stack.
215
+                           2. Pop from the stack.
216
+                           3. Compute a bitwise OR of V2 and V1.
217
+                           4. Push V3 to the stack.
218
+
219
+   n           XOR         1. Pop from the stack.
220
+                           2. Pop from the stack.
221
+                           3. Compute a bitwise XOR of V2 and V1.
222
+                           4. Push V3 to the stack.
223
+
224
+   o           NOT         1. Pop from the stack.
225
+                           2. Compute the bitwise NOT of V1.
226
+                           3. Push V2 to the stack.
227
+
228
+   p           DUP         1. Pop from the stack.
229
+                           2. Push V1 to the stack.
230
+                           3. Push V1 to the stack.
231
+
232
+   q           PICK        1. Get [TOSP].
233
+                           2. Compute V1 plus 1.
234
+                           3. Compute V2 modulo 256.
235
+                           4. Get [TOSP - V3].
236
+                           5. Pop the stack.
237
+                           6. Push V4 to the stack.
238
+
239
+   r           SWAP        1. Pop from the stack.
240
+                           2. Pop from the stack.
241
+                           3. Push V1 to the stack.
242
+                           4. Push V2 to the stack.
243
+
244
+   s           LT          1. Pop from the stack.
245
+                           2. Pop from the stack.
246
+                           3. If V2 is smaller than V1, push 0xFFFFFFFF
247
+                              to the stack.
248
+                           4. If V2 is greater than V1, push 0x00000000
249
+                              to the stack.
250
+
251
+   t           GT          1. Pop from the stack.
252
+                           2. Pop from the stack.
253
+                           3. If V2 is greater than V1, push 0xFFFFFFFF
254
+                              to the stack.
255
+                           4. If V2 is smaller than V1, push 0x00000000
256
+                              to the stack.
257
+
258
+   u           EQ          1. Pop from the stack.
259
+                           2. Pop from the stack.
260
+                           3. If V2 and V1 are equal, push 0xFFFFFFFF to
261
+                              the stack.
262
+                           4. If V2 and V1 are not equal, push
263
+                              0x00000000 to the stack.
264
+
265
+   Not listed lower case and upper case letters are reserved and MUST
266
+   NOT be used for opcodes. Additional opcodes can be added to the
267
+   specification as soon as two independent implementations with equal
268
+   output exist and at least one other implementator pledges to also
269
+   implement the specified behaviour.
270
+
271
+   Programs interpreting glitches MAY optimize opcode behaviour, if the
272
+   output does not change.
273
+
274
+3.4.   Execution and Output
275
+
276
+   Programs interpreting glitches SHOULD execute all contained opcodes
277
+   8000 times per second. Programs MUST preserve the contents of the
278
+   stack between successive interpretations of the same glitch.
279
+
280
+   After each time a glitch is executed, a program MUST add one to the
281
+   value of t. The last 8 bits of the value on the top of the stack
282
+   at this time are then taken as a sample. Samples SHOULD be written to
283
+   standard output, sound devices or files.
284
+
285
+   Sample output MAY happen immediately or be buffered. If buffering is
286
+   used, programs that output samples on standard output SHOULD buffer
287
+   at most 256 samples at a time. The resulting output frequency of
288
+   31.25hz is adequate for visualisations.
289
+
290
+4. IANA Considerations
291
+
292
+   The MIME media type for the glitch file format is
293
+   application/x-glitch.
294
+
295
+   Type name: application
296
+
297
+   Subtype name: x-glitch
298
+
299
+   Required parameters: n/a
300
+
301
+   Optional parameters: n/a
302
+
303
+   Encoding considerations:
304
+
305
+      A glitch MUST be represented using ASCII.
306
+
307
+
308
+5.  Security Considerations
309
+
310
+   The glitch file format does not in itself pose a security threat. Due
311
+   to lack of branching and looping instructions a conforming
312
+   implementation can not enter an infinite loop.
313
+
314
+   Implementors SHOULD ensure their implementations handle variable
315
+   overflows and underflows securely. Users SHOULD take note that there
316
+   is no general guarantee that an implementation contains no security
317
+   holes.
318
+
319
+6.  Applications that use this media type
320
+
321
+   The glitch file format has been used in libglitch [LIBGLITCH].
322
+
323
+
324
+7.  References
325
+
326
+7.1.   Normative References
327
+
328
+   [RFC2119]   Bradner, S., "Key words for use in RFCs to Indicate
329
+               Requirement Levels", BCP 14, RFC 2119, March 1997.
330
+
331
+   [RFC4234]   Crocker, D. and P.  Overell, "Augmented BNF for Syntax
332
+               Specifications: ABNF", RFC 4234, October 2005.
333
+
334
+7.2.   Informative References
335
+
336
+   [BYTEBEAT]  <http://canonical.org/~kragen/bytebeat/>
337
+
338
+   [GLITCHMACHINE]
339
+               <http://madgarden.net/apps/glitch-machine/>
340
+
341
+   [HTML]
342
+               <http://www.whatwg.org/specs/web-apps/current-work/
343
+               multipage/common-input-element-attributes.html
344
+               #attr-input-maxlength>
345
+
346
+   [LIBGLITCH] <https://github.com/erlehmann/libglitch>

+ 28
- 0
Makefile View File

@@ -0,0 +1,28 @@
1
+DEBUG=0
2
+
3
+NASM=nasm
4
+
5
+ifeq ($(DEBUG), 0)
6
+	ASFLAGS=-felf64
7
+	LDFLAGS=-s -melf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -lSDL
8
+else
9
+	ASFLAGS=-felf64 -g -F dwarf -l bf.lst
10
+	LDFLAGS=-g -melf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -lSDL
11
+endif
12
+
13
+TARGET=yaglitch
14
+OBJ=$(TARGET).o
15
+SRC=$(TARGET).asm
16
+
17
+all: $(TARGET)
18
+
19
+$(TARGET): $(OBJ)
20
+	ld $(LDFLAGS) $< -o $@
21
+
22
+$(OBJ): $(SRC) sdl.asm Makefile
23
+	$(NASM) $(ASFLAGS) $<
24
+
25
+.PHONY: clean
26
+
27
+clean:
28
+	-rm -fv $(TARGET) $(OBJ) $(TARGET).lst

+ 44
- 0
sdl.asm View File

@@ -0,0 +1,44 @@
1
+
2
+EXTERN SDL_Init
3
+Extern SDL_Quit
4
+Extern SDL_SetVideoMode
5
+Extern SDL_Delay
6
+Extern SDL_OpenAudio
7
+Extern SDL_PauseAudio
8
+
9
+
10
+
11
+%define SDL_SWSURFACE 0
12
+
13
+%define AUDIO_U8        0x0008  ; Unsigned 8-bit samples 
14
+%define AUDIO_S8        0x8008  ; Signed 8-bit samples 
15
+%define AUDIO_U16LSB    0x0010  ; Unsigned 16-bit samples 
16
+%define AUDIO_S16LSB    0x8010  ; Signed 16-bit samples 
17
+%define AUDIO_U16MSB    0x1010  ; As above, but big-endian byte order 
18
+%define AUDIO_S16MSB    0x9010  ; As above, but big-endian byte order 
19
+%define AUDIO_U16       AUDIO_U16LSB
20
+%define AUDIO_S16       AUDIO_S16LSB
21
+
22
+STRUC SDL_AudioSpec_STRUC
23
+	.freq: resw 1
24
+	.format: resw 1
25
+	.channels: resb 1
26
+	.silence: resb 1
27
+	.samples: resw 1
28
+	.size: resw 1
29
+	.callback: resq 1
30
+	.userdata: resq 1
31
+ENDSTRUC
32
+%macro def_SDL_AudioSpec 1
33
+	%define %1.freq %1+SDL_AudioSpec_STRUC.freq
34
+	%define %1.format %1+SDL_AudioSpec_STRUC.format
35
+	%define %1.channels %1+SDL_AudioSpec_STRUC.channels
36
+	%define %1.silence %1+SDL_AudioSpec_STRUC.silence
37
+	%define %1.samples %1+SDL_AudioSpec_STRUC.samples
38
+	%define %1.size %1+SDL_AudioSpec_STRUC.size
39
+	%define %1.callback %1+SDL_AudioSpec_STRUC.callback
40
+	%define %1.userdata %1+SDL_AudioSpec_STRUC.userdata
41
+	%1:
42
+		ISTRUC SDL_AudioSpec_STRUC
43
+%endmacro
44
+

+ 179
- 0
yaglitch.asm View File

@@ -0,0 +1,179 @@
1
+; yaglitch : Yet Another Glitch
2
+; Copyright (C) 2018 Weber Yann
3
+; 
4
+; This program is free software; you can redistribute it and/or modify
5
+; it under the terms of the GNU General Public License as published by
6
+; the Free Software Foundation; either version 3 of the License, or
7
+; any later version.
8
+; 
9
+; This program is distributed in the hope that it will be useful,
10
+; but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
+; GNU General Public License for more details.
13
+; 
14
+; You should have received a copy of the GNU General Public License
15
+; along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
+;
17
+
18
+[bits 64]
19
+
20
+%include "sdl.asm"
21
+
22
+%define STACK_SZ 256
23
+
24
+section .data
25
+
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
36
+	
37
+
38
+	val: db 0
39
+	dir: db 0
40
+
41
+	dot: db "."
42
+	nl: db 0xA
43
+
44
+
45
+section .bss
46
+
47
+	; stack machine ring buffer
48
+	stack_buff: resd STACK_SZ
49
+	; top of stack pointer
50
+	tosp: resq 1
51
+	; stack machine memory
52
+	t: resb 1
53
+	; audiospec returned from SDL
54
+	audiospec_recv: resb 32
55
+
56
+section .text
57
+global _start
58
+_start:
59
+
60
+	mov rdi, 0x0000FFFF
61
+	call SDL_Init
62
+
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
+	mov rdi, audiospec_wanted
71
+	mov rsi, audiospec_recv
72
+	call SDL_OpenAudio
73
+
74
+	xor rdi, rdi
75
+	call SDL_PauseAudio
76
+
77
+	mov rdi, 2000
78
+	call SDL_Delay
79
+
80
+
81
+exit:
82
+	call SDL_Quit
83
+
84
+	mov rax, 1
85
+	mov rdi, rax
86
+	mov rsi, nl
87
+	mov rdx, rax
88
+	syscall ; print nl
89
+
90
+	xor rdi, 0
91
+	mov rax, 0x3c
92
+	syscall
93
+
94
+audio_cllbck:
95
+	; rdi -> *userdata
96
+	; rsi -> *stream
97
+	; rdx -> stream_len
98
+	mov rcx, rdx
99
+	mov rdi, rsi
100
+	
101
+	mov al, [val]
102
+	.loop:
103
+		test al, al
104
+		jnz .nxt
105
+		xor [dir], byte 1
106
+		.nxt:
107
+		cmp [dir], byte 0
108
+		jz .inc
109
+		sub al, 1
110
+		jmp .stoloop
111
+		.inc:
112
+		add al, 1
113
+		.stoloop:
114
+		stosb
115
+		loop .loop
116
+	mov [val], al
117
+	ret
118
+
119
+
120
+
121
+audio_cllbck2:
122
+	; rdi -> *userdata
123
+	; rsi -> *stream
124
+	; rdx -> stream_len
125
+	mov rcx, rdx
126
+	mov rdi, rsi
127
+	
128
+	mov rax, r11
129
+
130
+	; LFSR attempt
131
+	mov rsi, 1
132
+	.loop:
133
+		mov rbx, rax
134
+		and rbx, rsi
135
+		popcnt rbx, rbx
136
+		shl rsi, 3
137
+
138
+		mov rdx, rax
139
+		and rdx, rsi
140
+		popcnt rdx, rdx
141
+		xor rbx, rdx
142
+
143
+		shl rsi, 2
144
+		mov rdx, rax
145
+		and rdx, rsi
146
+		popcnt rdx, rdx
147
+		xor rbx, rdx
148
+
149
+		shl rsi, 1
150
+		mov rdx, rax
151
+		and rdx, rsi
152
+		popcnt rdx, rdx
153
+		xor rbx, rdx
154
+
155
+		shl rsi, 2
156
+		mov rdx, rax
157
+		and rdx, rsi
158
+		popcnt rdx, rdx
159
+		xor rbx, rdx
160
+
161
+		shl rax, 1
162
+		add al, bl
163
+		stosb
164
+		loop .loop
165
+	mov r11, rax
166
+	ret
167
+
168
+;audio_cllbck:
169
+;	; rdi -> *userdata
170
+;	; rsi -> *stream
171
+;	; rdx -> stream_len
172
+;	mov rcx, rdx
173
+;	mov rdi, rsi
174
+;	
175
+;	.loop:
176
+;		add al, 8
177
+;		stosb
178
+;		loop .loop
179
+;	ret

Loading…
Cancel
Save