% PRBS.m
%
% By Adriaan Van Nuffel & Robin Reussens
% afvnuffe@vub.ac.be
% rreusens@vub.ac.be
%
% This program is to test the random method. The method used is PRBS
% (pseudo random binary sequence). It works as follows: bit zero and bit 5
% of a byte are EXOR'ed. The byte gets 'rotated to the right', and the
% result of the EXOR is stored in the free place.
% See also: Art Of Control Engineering, p. 418
%
% Assembler code:
%
%   BTFSC 	RANDOM, H'0'
% 	BSF 	BIT_0, H'0'
% 	BTFSC	RANDOM, H'5'
% 	BSF	    BIT_5, H'0'
% 	MOVF	BIT_0, 0
% 	XORWF	BIT_5, 1
% 	BCF	    STATUS, C
% 	BTFSC	BIT_5, H'0'
% 	BSF	    STATUS, C
% 	RRF	    RANDOM, 1
%
%

% We write the random byte as an array of true (1) and false (0)
% 'Bit 0' is the last element of the array (element 8)
% 'Bit 5' is the third element of the array.
%        bit:    7     6     5      4     3      2     1      0
random_byte = [true, false, true, false, false, true, false, true];     % I just chose this value
for j = 1:256
    new_bit = XOR(random_byte(3), random_byte(8));
    % rotate right
    for i = 8:-1:2
        random_byte(i) = random_byte(i-1);
    end
    random_byte(1) = new_bit;
    graf(j) = random_byte(8);
end

figure
plot(graf)
axis([0 256 -10 10])

% The method also works using bit 0 and bit 3. It DOESN'T work with bit 0
% and bit 4: it doesn't generate a random series...

% To test the cowboys:
%
% Cowboy 1 = 000
% Cowboy 2 = 001
% Cowboy 3 = 010
% Cowboy 4 = 011
% Cowboy 5 = 100
% Cowboy 6 = 101
% nothing =  110
% nothing =  111

random_byte = [true, false, true, false, false, true, false, true];     % I just chose this value
for j = 1:99*3
    for k = 1:3
        new_bit = XOR(random_byte(3), random_byte(8));
        % rotate right
        for i = 8:-1:2
            random_byte(i) = random_byte(i-1);
        end
        random_byte(1) = new_bit;
    end
    if random_byte(6:8) == [0,0,0]
        result(j) = 1;
    end
    if random_byte(6:8) == [0,0,1]
        result(j) = 2;
    end
    if random_byte(6:8) == [0,1,0]
        result(j) = 3;
    end
    if random_byte(6:8) == [0,1,1]
        result(j) = 4;
    end
    if random_byte(6:8) == [1,0,0]
        result(j) = 5;
    end
    if random_byte(6:8) == [1,0,1]
        result(j) = 6;
    end
    if random_byte(6:8) == [1,1,0]
        result(j) = 0;
    end
    if random_byte(6:8) == [1,1,1]
        result(j) = 1;
    end
end

figure
plot(result)
axis([0 99*3 -1 10])

% In assembler, this was:
% 
% test_combination_0
% 	MOVLW	B'00000000'		; In case the last three bits of RANDOM are 000,
% 	SUBWF	DECIDE, 0		; cowboy 1 will become visible.
% 	BTFSS	STATUS, Z
% 	GOTO	test_combination_1
% 	BSF	COWBOY_STATS, H'00'
% 	GOTO	end_combinations_tests
% 
% test_combination_1
% 	MOVLW	B'00000001'		; In case the last three bits of RANDOM are 001,
% 	SUBWF	DECIDE, 0		; cowboy 2 will become visible.
% 	BTFSS	STATUS, Z
% 	GOTO	test_combination_2
% 	BSF	COWBOY_STATS, H'01'
% 	GOTO	end_combinations_tests
% 
% test_combination_2
% 	MOVLW	B'00000010'		; In case the last three bits of RANDOM are 010,
% 	SUBWF	DECIDE, 0		; cowboy 3 will become visible.
% 	BTFSS	STATUS, Z
% 	GOTO	test_combination_3
% 	BSF	COWBOY_STATS, H'02'
% 	GOTO	end_combinations_tests
% 
% test_combination_3
% 	MOVLW	B'00000011'		; In case the last three bits of RANDOM are 011,
% 	SUBWF	DECIDE, 0		; cowboy 4 will become visible.
% 	BTFSS	STATUS, Z
% 	GOTO	test_combination_4
% 	BSF	COWBOY_STATS, H'03'
% 	GOTO	end_combinations_tests
% 
% test_combination_4
% 	MOVLW	B'00000100'		; In case the last three bits of RANDOM are 100,
% 	SUBWF	DECIDE, 0		; cowboy 5 will become visible.
% 	BTFSS	STATUS, Z
% 	GOTO	test_combination_5
% 	BSF	COWBOY_STATS, H'04'
% 	GOTO	end_combinations_tests
% 
% test_combination_5
% 	MOVLW	B'00000101'		; In case the last three bits of RANDOM are 101,
% 	SUBWF	DECIDE, 0		; cowboy 6 will become visible.
% 	BTFSS	STATUS, Z
% 	GOTO	end_combinations_tests	; !!!
% 	BSF	COWBOY_STATS, H'05'
% 
% end_combinations_tests			; Indeed, in case the last three bits were 110 or 111, nothing is changed.