function [inst_addr] = pb_inst_pbonly(flags, inst, inst_data, length)
% FUNCTION NAME:
%   pb_inst_pbonly
%
% DESCRIPTION:
%   Program an instruction of a pulse program to the board.
%   This is the instruction programming function for boards without a DDS.
%   (for example PulseBlaster and PulseBlasterESR boards)

% INPUTS:
%   flags - Set every bit to one for each flag you want to set high.
%
%   inst - Which instruction to use. See manual for details.
%
%   inst_data - Some instructions require additional data. This allows that data 
%               to be specified. See manual for details.
%   
%   length - Time until the next instruction is executed in nanoseconds.
%
% OUTPUTS:
%   inst_addr - The address of the instruction, can be used for branch 
%               instructions. Counts from 0.
%
% $Revision: R2017a$ 
% $Author: SpinCore Technologies, Inc.$
% $Date: November 17, 2023$
% ------------------------------------------------------------------------------

global SPINAPI_DLL_NAME;

if(isnumeric(flags))
    num_flags = flags;
elseif islogical(flags)
    charvec = char('0' + flags);
    dec = bin2dec(charvec);
    num_flags = dec;
else
    num_flags = hex2dec(flags);
end

if(isa(inst, 'char'))
    inst = upper(inst);
end

switch inst
	case {'CONTINUE', 0}
		op_code = 0;
	case {'STOP', 1}
		op_code = 1;
	case {'LOOP', 2}
		op_code = 2;
	case {'END_LOOP', 3}
		op_code = 3;
	case {'JSR', 4}
		op_code = 4;
	case {'RTS', 5}
		op_code = 5;
	case {'BRANCH', 6}
		op_code = 6;
	case {'LONG_DELAY', 7}
		op_code = 7;
	case {'WAIT', 8}
		op_code = 8;
end

inst_addr = calllib(SPINAPI_DLL_NAME, 'pb_inst_pbonly', num_flags, op_code, inst_data, length);

end