Skip to content

Commit f7040ad

Browse files
committed
Release 2.1
1 parent 86206e7 commit f7040ad

File tree

10 files changed

+27040
-25841
lines changed

10 files changed

+27040
-25841
lines changed

6502bf.bf

Lines changed: 26865 additions & 25767 deletions
Large diffs are not rendered by default.

6502bf.fbf

Lines changed: 56 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@
4949
#dim CONST_1
5050
set CONST_1 1
5151

52-
#dim CONST_2
53-
set CONST_2 2
54-
5552
#dim CONST_64
5653
set CONST_64 64
5754

@@ -61,12 +58,6 @@ set CONST_128 128
6158
#dim CONST_255
6259
set CONST_255 255
6360

64-
#dim CONST_256
65-
set CONST_256 256
66-
67-
#dim CONST_4096
68-
set CONST_4096 4096
69-
7061
#dim CONST_FF00
7162
set CONST_FF00 65280
7263

@@ -107,53 +98,6 @@ set CONST_FF00 65280
10798
returnfrom _doElse
10899
#endblock
109100

110-
-- -----------------------------------------------------------------------------------
111-
112-
-- Clears tape cells starting from _ct_start, it will clear as many cells as contained in _ct_start.
113-
-- For example, if _ct_start=3 this will clear _ct_start and the next 2 cells on the right.
114-
-- _ct_start must be greater than 0.
115-
#block _clean_tape _ct_start
116-
moveto _ct_start
117-
-- unfortunately, we need to leave a 1 in first cell as a marker, this means the code for the first cell is special
118-
brainfuck -[ if there is at least 2 cells to clear enter the main body
119-
brainfuck >[-]< clears next cell
120-
brainfuck [->+<] copies current into next
121-
brainfuck +>- increase first cell making it 1 then move to next cell notice the next is decremented twice because the check is done immediately
122-
brainfuck [
123-
brainfuck >[-]< clears next cell
124-
brainfuck [->+<] copies current
125-
brainfuck >- next is decremented by one
126-
brainfuck ]
127-
brainfuck -[+<-] go back to the cell with 1
128-
brainfuck ]
129-
returnfrom _ct_start
130-
#endblock
131-
132-
-- -----------------------------------------------------------------------------------
133-
-- MATH FUNCTIONS
134-
-- -----------------------------------------------------------------------------------
135-
136-
-- Work area for math functions.
137-
-- These MUST be consecutive cell for the algorithm to work
138-
#dim _mf_c0 _mf_c1 _mf_c2 _mf_c3
139-
#custom 2
140-
141-
-- Original code: https://esolangs.org/wiki/Brainfuck_algorithms#Divmod_algorithm
142-
#block divmod _d_n _d_d _d_result _d_mod
143-
-- >n d
144-
-- # >0 d-n%d n%d n/d
145-
set _mf_c0 6
146-
_clean_tape _mf_c0
147-
copy _d_n _mf_c0
148-
copy _d_d _mf_c1
149-
moveto _mf_c0
150-
brainfuck [->[->+>>]>[<<+>>[-<+>]>+>>]<<<<<]
151-
brainfuck >[>>>]>[[-<+>]>+>>]<<<<<
152-
returnfrom _mf_c0
153-
copy _mf_c3 _d_result
154-
copy _mf_c2 _d_mod
155-
#endblock
156-
157101
-- ------------------
158102
-- BITWISE FUNCTIONS
159103
-- ------------------
@@ -162,20 +106,40 @@ set CONST_FF00 65280
162106
-- Original code: https://codegolf.stackexchange.com/questions/9178/bitwise-operators-in-brainfuck
163107

164108
-- Work area for the bitwise algorithms.
109+
-- See each block to see how much workarea total they need
165110
-- These MUST be consecutive cell for the algorithm to work
166111
#dim _bw_c0 _bw_c1 _bw_c2 _bw_c3 _bw_c4 _bw_c5
167112
#custom 53
168113

169114
-- Resets the working area for the bitwise algorithms.
115+
-- This works only fro 16 bit cells, 32 bit cells ill require a bigger workign area.
116+
-- The implementation is a bit verbose (clears 59 cells), but we prefer to do it fast.
170117
#block _clean_bw_wa
171-
-- TODO For each function calculate separately the working area needed and clear only that one. Use libraries\lib.fbf
172-
set _bw_c0 59
173-
_clean_tape _bw_c0
118+
moveto _bw_c0
119+
120+
brainfuck [-]>[-]>[-]>[-]>[-]> [-]>[-]>[-]>[-]>[-]>
121+
brainfuck [-]>[-]>[-]>[-]>[-]> [-]>[-]>[-]>[-]>[-]>
122+
brainfuck [-]>[-]>[-]>[-]>[-]> [-]>[-]>[-]>[-]>[-]>
123+
124+
brainfuck [-]>[-]>[-]>[-]>[-]> [-]>[-]>[-]>[-]>[-]>
125+
brainfuck [-]>[-]>[-]>[-]>[-]> [-]>[-]>[-]>[-]>[-]>
126+
brainfuck [-]>[-]>[-]>[-]>[-]> [-]>[-]>[-]>[-]>
127+
128+
brainfuck <<<<< <<<<<
129+
brainfuck <<<<< <<<<<
130+
brainfuck <<<<< <<<<<
131+
132+
brainfuck <<<<< <<<<<
133+
brainfuck <<<<< <<<<<
134+
brainfuck <<<<< <<<<
135+
136+
returnfrom _bw_c0
174137
#endblock
175138

176139
#block NOT _not_a _not_res
177140
-- Assumes A is in cell 0, stores NOT A in cell 1, pointer starts and ends in cell 0.
178-
_clean_bw_wa
141+
-- Uses only 2 cells; notice that this algorithm will take forever for 32 bit cells.
142+
set _bw_c1 0
179143
copy _not_a _bw_c0
180144
moveto _bw_c0
181145
brainfuck +[>-<-]
@@ -185,6 +149,7 @@ set CONST_FF00 65280
185149

186150
#block ROL _rol_a _rol_res
187151
-- Assumes A is in cell 0, stores A ROL 1 in cell 1, pointer starts and ends in cell 0.
152+
-- Uses 59 cells, regardless their size.
188153
_clean_bw_wa
189154
copy _rol_a _bw_c0
190155
moveto _bw_c0
@@ -195,6 +160,7 @@ set CONST_FF00 65280
195160

196161
#block OR _or_a _or_b _or_res
197162
-- Assumes A and B are in cells 1 and 2, stores A OR B in cell 2, pointer starts in cell 0 and ends in cell 5.
163+
-- Uses 59 16-bit cells or 106 32-bit cells.
198164
_clean_bw_wa
199165
copy _or_a _bw_c1
200166
copy _or_b _bw_c2
@@ -206,6 +172,7 @@ set CONST_FF00 65280
206172

207173
#block AND _and_a _and_b _and_res
208174
-- Assumes A and B are in cells 1 and 2, stores A AND B in cell 4, pointer starts in cell 0 and ends in cell 5.
175+
-- Uses 59 16-bit cells or 106 32-bit cells.
209176
_clean_bw_wa
210177
copy _and_a _bw_c1
211178
copy _and_b _bw_c2
@@ -217,6 +184,7 @@ set CONST_FF00 65280
217184

218185
#block XOR _xor_a _xor_b _xor_res
219186
-- Assumes A and B are in cells 1 and 2, stores A XOR B in cell 2, pointer starts in cell 0 and ends in cell 5.
187+
-- Uses 59 16-bit cells or 106 32-bit cells.
220188
_clean_bw_wa
221189
copy _xor_a _bw_c1
222190
copy _xor_b _bw_c2
@@ -284,8 +252,12 @@ set CONST_FF00 65280
284252
-- Original algorithm from: https://esolangs.org/wiki/Brainfuck_algorithms#Print_value_of_cell_x_as_number_for_ANY_sized_cell_.28ie_8bit.2C_16bit.2C_etc.29
285253
-- Notice this uses _outv_N as working area, it expects 5 of them to be available and contogue.
286254
#block out _o_v
287-
set _outv_0 12
288-
_clean_tape _outv_0
255+
-- Clean working area
256+
moveto _outv_1
257+
brainfuck [-]>[-]>[-]>[-]>[-]> [-]>[-]>[-]>[-]>[-]> [-]>
258+
brainfuck <<<<< <<<<< <
259+
returnfrom _outv_1
260+
289261
copy _o_v _outv_0
290262
moveto _outv_0
291263
brainfuck [>>+>+<<<-]>>>[<<<+>>>-]<<+>[<->[>++++++++++<[->-[>+>>]>[+[-<+>]>+>>]<<<<<]>[-]
@@ -305,7 +277,8 @@ set CONST_FF00 65280
305277
copy _o16_v _outv_0
306278
set _outv_1 4
307279
uneq _outv_1 0
308-
divmod _outv_0 CONST_4096 _outv_2 _outv_0
280+
div _outv_0 4096 _outv_2
281+
mod _outv_0 4096 _outv_0
309282
SHL_4 _outv_0 _outv_0
310283
inc _outv_2 '0'
311284
comp _outv_2 '9' _outv_3
@@ -534,19 +507,22 @@ set CONST_FF00 65280
534507
#block set_flags
535508
copy status _stsv_1
536509
set C_flag 0
537-
divmod _stsv_1 CONST_2 _stsv_1 _stsv_0
510+
mod _stsv_1 2 _stsv_0
511+
div _stsv_1 2 _stsv_1
538512
ifnoteq _stsv_0 0
539513
set C_flag 1
540514
end
541515

542516
set Z_flag 0
543-
divmod _stsv_1 CONST_2 _stsv_1 _stsv_0
517+
mod _stsv_1 2 _stsv_0
518+
div _stsv_1 2 _stsv_1
544519
ifnoteq _stsv_0 0
545520
set Z_flag 1
546521
end
547522

548523
set I_flag 0
549-
divmod _stsv_1 CONST_2 _stsv_1 _stsv_0
524+
mod _stsv_1 2 _stsv_0
525+
div _stsv_1 2 _stsv_1
550526
ifnoteq _stsv_0 0
551527
set I_flag 1
552528
end
@@ -561,7 +537,8 @@ set CONST_FF00 65280
561537
div _stsv_1 8 _stsv_1
562538

563539
set V_flag 0
564-
divmod _stsv_1 CONST_2 _stsv_1 _stsv_0
540+
mod _stsv_1 2 _stsv_0
541+
div _stsv_1 2 _stsv_1
565542
ifnoteq _stsv_0 0
566543
set V_flag 1
567544
end
@@ -2636,7 +2613,8 @@ _endif
26362613

26372614
#block execute_ROR _ror_v
26382615
-- Beware of overwriting tmp0
2639-
divmod _ror_v CONST_2 _ror_v tmp4
2616+
mod _ror_v 2 tmp4
2617+
div _ror_v 2 _ror_v
26402618
SHL_7 C_flag C_flag
26412619
add _ror_v C_Flag _ror_v
26422620
copy tmp4 C_flag
@@ -2748,7 +2726,8 @@ _endif
27482726

27492727
addr_absolute
27502728
dec P 1
2751-
divmod P CONST_256 tmp0 tmp1
2729+
div P 256 tmp0
2730+
mod P 256 tmp1
27522731
push_stack tmp0
27532732
push_stack tmp1
27542733
copy operand_addr P
@@ -2860,7 +2839,8 @@ _endif
28602839
copy P tmp4
28612840

28622841
inc P 2
2863-
divmod P CONST_256 tmp0 tmp1
2842+
div P 256 tmp0
2843+
mod P 256 tmp1
28642844
push_stack tmp0
28652845
push_stack tmp1
28662846
set_status
@@ -3064,7 +3044,8 @@ _endif
30643044

30653045
-- # char read goes into A,X as return value
30663046
sub tmp0 tmp2 tmp2
3067-
divmod tmp2 CONST_256 X A
3047+
div tmp2 256 X
3048+
mod tmp2 256 A
30683049

30693050
inc P 1
30703051
set exception 0
@@ -3073,10 +3054,12 @@ _endif
30733054
-- 50 Writes clock
30743055
-- The internal "clock" (instruction counter) it is pushed to the 6502 stack.
30753056
ifeq tmp0 80
3076-
divmod ticLo CONST_256 tmp1 tmp0
3057+
div ticLo 256 tmp1
3058+
mod ticLo 256 tmp0
30773059
push_stack tmp0
30783060
push_stack tmp1
3079-
divmod ticHi CONST_256 tmp1 tmp0
3061+
div ticHi 256 tmp1
3062+
mod ticHi 256 tmp0
30803063
push_stack tmp0
30813064
push_stack tmp1
30823065

archive/benchmarks.xlsx

381 Bytes
Binary file not shown.

bin/CheckFile.jar

2.76 KB
Binary file not shown.

bin/Linker.jar

2.72 KB
Binary file not shown.

bin/TestRunner.jar

2.75 KB
Binary file not shown.

bin/TweakCCode.jar

2.72 KB
Binary file not shown.

cc65/6502bf.lib

0 Bytes
Binary file not shown.

eclipse/6502bf tools/src/org/mzattera/bf6502/Linker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class Linker {
2727
public final static String CODE_NAME = "6502bf";
2828

2929
// Cells on the BF tape to skip before finding 6502 memory (start of mem[] array on tape)
30-
public final static int SKIP = 124;
30+
public final static int SKIP = 115;
3131

3232
// Start address of 6502 program
3333
public final static int START_ADDRESS = 0x0200;
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/**
2+
*/
3+
package org.mzattera.bf6502.optimization;
4+
5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.nio.file.Files;
8+
import java.nio.file.Paths;
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
12+
import org.mzattera.util.FileUtil;
13+
14+
/**
15+
* Tries to misure the extend of the tape area accessed.
16+
*
17+
* @author Massimiliano "Maxi" Zattera
18+
*
19+
*/
20+
public class MeasureWorkAreaSize {
21+
22+
private final static String IN_FILE = "D:\\Users\\mzatt\\Projects\\Git - FuckBench\\wip\\logfuntest.c";
23+
private final static String OUT_FILE = "D:\\Users\\mzatt\\Projects\\Git - FuckBench\\wip\\logfuntest.1.c";
24+
private final static String CELL_TYPE = "uint16_t";
25+
private final static int TAPE_SIZE = 30_000;
26+
27+
/**
28+
* @param args
29+
*/
30+
public static void main(String[] args) {
31+
try {
32+
String inputFileName = IN_FILE;
33+
String outputFileName = OUT_FILE;
34+
int tapeSize = TAPE_SIZE;
35+
36+
for (int i = 0; i < args.length; i++) {
37+
if (args[i].equals("-i"))
38+
inputFileName = args[++i];
39+
else if (args[i].equals("-o"))
40+
outputFileName = args[++i];
41+
else if (args[i].equals("-s"))
42+
tapeSize = Integer.parseInt(args[++i]);
43+
else
44+
throw new IllegalArgumentException("Unrecognized parameter: " + args[i]);
45+
}
46+
47+
execute(inputFileName, outputFileName, tapeSize);
48+
System.exit(0);
49+
50+
} catch (Exception e) {
51+
e.printStackTrace();
52+
printUsage();
53+
System.exit(-1);
54+
}
55+
}
56+
57+
public static void execute(String inputFileName, String outputFileName, int tapeSize) throws IOException {
58+
59+
// TODO Add parameters error checking
60+
61+
System.out.println(MeasureWorkAreaSize.class.getName() + " tweaking generated code.");
62+
System.out.println("Input File Name : " + new File(inputFileName).getCanonicalPath());
63+
System.out.println("Output File Name: " + new File(outputFileName).getCanonicalPath());
64+
System.out.println("Total tape size : " + tapeSize);
65+
66+
// Read the emulator .c code
67+
// String source = new String(Files.readAllBytes(Paths.get(inputFileName)));
68+
69+
// Original source code
70+
List<String> source = Files.readAllLines(Paths.get(inputFileName));
71+
72+
// New source code
73+
List<String> newCode = new ArrayList<>(source.size());
74+
75+
// Takes opcode check source code from the emulator.
76+
int i = 0;
77+
78+
// Fix header, tape and cell size
79+
for (i = 0; i < source.size(); ++i) {
80+
String line = source.get(i);
81+
if (line.equals("#include <stdint.h>")) {
82+
newCode.add("#include <stdint.h>");
83+
newCode.add("#include <stddef.h>");
84+
} else if (line.equals("static uint8_t m[30000], *p = m;")) {
85+
newCode.add("static " + CELL_TYPE + " m[" + tapeSize + "], *p = m;");
86+
newCode.add("size_t _min = " + tapeSize + ", _max = 0;");
87+
newCode.add("size_t i(size_t s) {");
88+
newCode.add(" size_t base = p - m;");
89+
newCode.add(" size_t curr = base + s;");
90+
91+
newCode.add(" if (curr < _min) _min = curr;");
92+
newCode.add(" if (curr > _max) _max = curr;");
93+
newCode.add(" return s;");
94+
newCode.add("}");
95+
} else if (line.trim().equals("return 0;")) {
96+
newCode.add(" printf(\"\\n\\n>>> Min. %lu Max. %lu\\n\", _min, _max);");
97+
newCode.add(" return 0;");
98+
} else {
99+
newCode.add(line.replaceAll("\\[([^\\]]+)\\]", "[i($1)]"));
100+
}
101+
}
102+
103+
FileUtil.write(newCode, outputFileName);
104+
}
105+
106+
private static void printUsage() {
107+
System.out.println();
108+
System.out.println();
109+
System.out.println("Usage: java -jar " + MeasureWorkAreaSize.class.getSimpleName()
110+
+ ".jar [-i <in>] [-o <out>] [-s <size>]");
111+
System.out.println();
112+
System.out.println(" <in> : Input .c file name (defaults to \"" + IN_FILE + "\").");
113+
System.out.println(
114+
" <out> : Output .c file name; can be same of inupt file (defaults to \"" + IN_FILE + "\").");
115+
System.out.println(" <size> : Total size of tape (defaults to " + TAPE_SIZE + ").");
116+
System.out.println();
117+
}
118+
}

0 commit comments

Comments
 (0)