Skip to content

Commit

Permalink
Release 2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
mzattera committed Jan 19, 2021
1 parent 86206e7 commit f7040ad
Show file tree
Hide file tree
Showing 10 changed files with 27,040 additions and 25,841 deletions.
52,632 changes: 26,865 additions & 25,767 deletions 6502bf.bf

Large diffs are not rendered by default.

129 changes: 56 additions & 73 deletions 6502bf.fbf
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@
#dim CONST_1
set CONST_1 1

#dim CONST_2
set CONST_2 2

#dim CONST_64
set CONST_64 64

Expand All @@ -61,12 +58,6 @@ set CONST_128 128
#dim CONST_255
set CONST_255 255

#dim CONST_256
set CONST_256 256

#dim CONST_4096
set CONST_4096 4096

#dim CONST_FF00
set CONST_FF00 65280

Expand Down Expand Up @@ -107,53 +98,6 @@ set CONST_FF00 65280
returnfrom _doElse
#endblock

-- -----------------------------------------------------------------------------------

-- Clears tape cells starting from _ct_start, it will clear as many cells as contained in _ct_start.
-- For example, if _ct_start=3 this will clear _ct_start and the next 2 cells on the right.
-- _ct_start must be greater than 0.
#block _clean_tape _ct_start
moveto _ct_start
-- unfortunately, we need to leave a 1 in first cell as a marker, this means the code for the first cell is special
brainfuck -[ if there is at least 2 cells to clear enter the main body
brainfuck >[-]< clears next cell
brainfuck [->+<] copies current into next
brainfuck +>- increase first cell making it 1 then move to next cell notice the next is decremented twice because the check is done immediately
brainfuck [
brainfuck >[-]< clears next cell
brainfuck [->+<] copies current
brainfuck >- next is decremented by one
brainfuck ]
brainfuck -[+<-] go back to the cell with 1
brainfuck ]
returnfrom _ct_start
#endblock

-- -----------------------------------------------------------------------------------
-- MATH FUNCTIONS
-- -----------------------------------------------------------------------------------

-- Work area for math functions.
-- These MUST be consecutive cell for the algorithm to work
#dim _mf_c0 _mf_c1 _mf_c2 _mf_c3
#custom 2

-- Original code: https://esolangs.org/wiki/Brainfuck_algorithms#Divmod_algorithm
#block divmod _d_n _d_d _d_result _d_mod
-- >n d
-- # >0 d-n%d n%d n/d
set _mf_c0 6
_clean_tape _mf_c0
copy _d_n _mf_c0
copy _d_d _mf_c1
moveto _mf_c0
brainfuck [->[->+>>]>[<<+>>[-<+>]>+>>]<<<<<]
brainfuck >[>>>]>[[-<+>]>+>>]<<<<<
returnfrom _mf_c0
copy _mf_c3 _d_result
copy _mf_c2 _d_mod
#endblock

-- ------------------
-- BITWISE FUNCTIONS
-- ------------------
Expand All @@ -162,20 +106,40 @@ set CONST_FF00 65280
-- Original code: https://codegolf.stackexchange.com/questions/9178/bitwise-operators-in-brainfuck

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

-- Resets the working area for the bitwise algorithms.
-- This works only fro 16 bit cells, 32 bit cells ill require a bigger workign area.
-- The implementation is a bit verbose (clears 59 cells), but we prefer to do it fast.
#block _clean_bw_wa
-- TODO For each function calculate separately the working area needed and clear only that one. Use libraries\lib.fbf
set _bw_c0 59
_clean_tape _bw_c0
moveto _bw_c0

brainfuck [-]>[-]>[-]>[-]>[-]> [-]>[-]>[-]>[-]>[-]>
brainfuck [-]>[-]>[-]>[-]>[-]> [-]>[-]>[-]>[-]>[-]>
brainfuck [-]>[-]>[-]>[-]>[-]> [-]>[-]>[-]>[-]>[-]>

brainfuck [-]>[-]>[-]>[-]>[-]> [-]>[-]>[-]>[-]>[-]>
brainfuck [-]>[-]>[-]>[-]>[-]> [-]>[-]>[-]>[-]>[-]>
brainfuck [-]>[-]>[-]>[-]>[-]> [-]>[-]>[-]>[-]>

brainfuck <<<<< <<<<<
brainfuck <<<<< <<<<<
brainfuck <<<<< <<<<<

brainfuck <<<<< <<<<<
brainfuck <<<<< <<<<<
brainfuck <<<<< <<<<

returnfrom _bw_c0
#endblock

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

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

#block OR _or_a _or_b _or_res
-- 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.
-- Uses 59 16-bit cells or 106 32-bit cells.
_clean_bw_wa
copy _or_a _bw_c1
copy _or_b _bw_c2
Expand All @@ -206,6 +172,7 @@ set CONST_FF00 65280

#block AND _and_a _and_b _and_res
-- 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.
-- Uses 59 16-bit cells or 106 32-bit cells.
_clean_bw_wa
copy _and_a _bw_c1
copy _and_b _bw_c2
Expand All @@ -217,6 +184,7 @@ set CONST_FF00 65280

#block XOR _xor_a _xor_b _xor_res
-- 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.
-- Uses 59 16-bit cells or 106 32-bit cells.
_clean_bw_wa
copy _xor_a _bw_c1
copy _xor_b _bw_c2
Expand Down Expand Up @@ -284,8 +252,12 @@ set CONST_FF00 65280
-- 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
-- Notice this uses _outv_N as working area, it expects 5 of them to be available and contogue.
#block out _o_v
set _outv_0 12
_clean_tape _outv_0
-- Clean working area
moveto _outv_1
brainfuck [-]>[-]>[-]>[-]>[-]> [-]>[-]>[-]>[-]>[-]> [-]>
brainfuck <<<<< <<<<< <
returnfrom _outv_1

copy _o_v _outv_0
moveto _outv_0
brainfuck [>>+>+<<<-]>>>[<<<+>>>-]<<+>[<->[>++++++++++<[->-[>+>>]>[+[-<+>]>+>>]<<<<<]>[-]
Expand All @@ -305,7 +277,8 @@ set CONST_FF00 65280
copy _o16_v _outv_0
set _outv_1 4
uneq _outv_1 0
divmod _outv_0 CONST_4096 _outv_2 _outv_0
div _outv_0 4096 _outv_2
mod _outv_0 4096 _outv_0
SHL_4 _outv_0 _outv_0
inc _outv_2 '0'
comp _outv_2 '9' _outv_3
Expand Down Expand Up @@ -534,19 +507,22 @@ set CONST_FF00 65280
#block set_flags
copy status _stsv_1
set C_flag 0
divmod _stsv_1 CONST_2 _stsv_1 _stsv_0
mod _stsv_1 2 _stsv_0
div _stsv_1 2 _stsv_1
ifnoteq _stsv_0 0
set C_flag 1
end

set Z_flag 0
divmod _stsv_1 CONST_2 _stsv_1 _stsv_0
mod _stsv_1 2 _stsv_0
div _stsv_1 2 _stsv_1
ifnoteq _stsv_0 0
set Z_flag 1
end

set I_flag 0
divmod _stsv_1 CONST_2 _stsv_1 _stsv_0
mod _stsv_1 2 _stsv_0
div _stsv_1 2 _stsv_1
ifnoteq _stsv_0 0
set I_flag 1
end
Expand All @@ -561,7 +537,8 @@ set CONST_FF00 65280
div _stsv_1 8 _stsv_1

set V_flag 0
divmod _stsv_1 CONST_2 _stsv_1 _stsv_0
mod _stsv_1 2 _stsv_0
div _stsv_1 2 _stsv_1
ifnoteq _stsv_0 0
set V_flag 1
end
Expand Down Expand Up @@ -2636,7 +2613,8 @@ _endif

#block execute_ROR _ror_v
-- Beware of overwriting tmp0
divmod _ror_v CONST_2 _ror_v tmp4
mod _ror_v 2 tmp4
div _ror_v 2 _ror_v
SHL_7 C_flag C_flag
add _ror_v C_Flag _ror_v
copy tmp4 C_flag
Expand Down Expand Up @@ -2748,7 +2726,8 @@ _endif

addr_absolute
dec P 1
divmod P CONST_256 tmp0 tmp1
div P 256 tmp0
mod P 256 tmp1
push_stack tmp0
push_stack tmp1
copy operand_addr P
Expand Down Expand Up @@ -2860,7 +2839,8 @@ _endif
copy P tmp4

inc P 2
divmod P CONST_256 tmp0 tmp1
div P 256 tmp0
mod P 256 tmp1
push_stack tmp0
push_stack tmp1
set_status
Expand Down Expand Up @@ -3064,7 +3044,8 @@ _endif

-- # char read goes into A,X as return value
sub tmp0 tmp2 tmp2
divmod tmp2 CONST_256 X A
div tmp2 256 X
mod tmp2 256 A

inc P 1
set exception 0
Expand All @@ -3073,10 +3054,12 @@ _endif
-- 50 Writes clock
-- The internal "clock" (instruction counter) it is pushed to the 6502 stack.
ifeq tmp0 80
divmod ticLo CONST_256 tmp1 tmp0
div ticLo 256 tmp1
mod ticLo 256 tmp0
push_stack tmp0
push_stack tmp1
divmod ticHi CONST_256 tmp1 tmp0
div ticHi 256 tmp1
mod ticHi 256 tmp0
push_stack tmp0
push_stack tmp1

Expand Down
Binary file modified archive/benchmarks.xlsx
Binary file not shown.
Binary file modified bin/CheckFile.jar
Binary file not shown.
Binary file modified bin/Linker.jar
Binary file not shown.
Binary file modified bin/TestRunner.jar
Binary file not shown.
Binary file modified bin/TweakCCode.jar
Binary file not shown.
Binary file modified cc65/6502bf.lib
Binary file not shown.
2 changes: 1 addition & 1 deletion eclipse/6502bf tools/src/org/mzattera/bf6502/Linker.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class Linker {
public final static String CODE_NAME = "6502bf";

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

// Start address of 6502 program
public final static int START_ADDRESS = 0x0200;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/**
*/
package org.mzattera.bf6502.optimization;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import org.mzattera.util.FileUtil;

/**
* Tries to misure the extend of the tape area accessed.
*
* @author Massimiliano "Maxi" Zattera
*
*/
public class MeasureWorkAreaSize {

private final static String IN_FILE = "D:\\Users\\mzatt\\Projects\\Git - FuckBench\\wip\\logfuntest.c";
private final static String OUT_FILE = "D:\\Users\\mzatt\\Projects\\Git - FuckBench\\wip\\logfuntest.1.c";
private final static String CELL_TYPE = "uint16_t";
private final static int TAPE_SIZE = 30_000;

/**
* @param args
*/
public static void main(String[] args) {
try {
String inputFileName = IN_FILE;
String outputFileName = OUT_FILE;
int tapeSize = TAPE_SIZE;

for (int i = 0; i < args.length; i++) {
if (args[i].equals("-i"))
inputFileName = args[++i];
else if (args[i].equals("-o"))
outputFileName = args[++i];
else if (args[i].equals("-s"))
tapeSize = Integer.parseInt(args[++i]);
else
throw new IllegalArgumentException("Unrecognized parameter: " + args[i]);
}

execute(inputFileName, outputFileName, tapeSize);
System.exit(0);

} catch (Exception e) {
e.printStackTrace();
printUsage();
System.exit(-1);
}
}

public static void execute(String inputFileName, String outputFileName, int tapeSize) throws IOException {

// TODO Add parameters error checking

System.out.println(MeasureWorkAreaSize.class.getName() + " tweaking generated code.");
System.out.println("Input File Name : " + new File(inputFileName).getCanonicalPath());
System.out.println("Output File Name: " + new File(outputFileName).getCanonicalPath());
System.out.println("Total tape size : " + tapeSize);

// Read the emulator .c code
// String source = new String(Files.readAllBytes(Paths.get(inputFileName)));

// Original source code
List<String> source = Files.readAllLines(Paths.get(inputFileName));

// New source code
List<String> newCode = new ArrayList<>(source.size());

// Takes opcode check source code from the emulator.
int i = 0;

// Fix header, tape and cell size
for (i = 0; i < source.size(); ++i) {
String line = source.get(i);
if (line.equals("#include <stdint.h>")) {
newCode.add("#include <stdint.h>");
newCode.add("#include <stddef.h>");
} else if (line.equals("static uint8_t m[30000], *p = m;")) {
newCode.add("static " + CELL_TYPE + " m[" + tapeSize + "], *p = m;");
newCode.add("size_t _min = " + tapeSize + ", _max = 0;");
newCode.add("size_t i(size_t s) {");
newCode.add(" size_t base = p - m;");
newCode.add(" size_t curr = base + s;");

newCode.add(" if (curr < _min) _min = curr;");
newCode.add(" if (curr > _max) _max = curr;");
newCode.add(" return s;");
newCode.add("}");
} else if (line.trim().equals("return 0;")) {
newCode.add(" printf(\"\\n\\n>>> Min. %lu Max. %lu\\n\", _min, _max);");
newCode.add(" return 0;");
} else {
newCode.add(line.replaceAll("\\[([^\\]]+)\\]", "[i($1)]"));
}
}

FileUtil.write(newCode, outputFileName);
}

private static void printUsage() {
System.out.println();
System.out.println();
System.out.println("Usage: java -jar " + MeasureWorkAreaSize.class.getSimpleName()
+ ".jar [-i <in>] [-o <out>] [-s <size>]");
System.out.println();
System.out.println(" <in> : Input .c file name (defaults to \"" + IN_FILE + "\").");
System.out.println(
" <out> : Output .c file name; can be same of inupt file (defaults to \"" + IN_FILE + "\").");
System.out.println(" <size> : Total size of tape (defaults to " + TAPE_SIZE + ").");
System.out.println();
}
}

0 comments on commit f7040ad

Please sign in to comment.