// This is the user file inclued in parserRL
// Created Thu Dec 15 05:00:13 2022

// Copyright (c) 2021-2022 HotWright Inc.
// Created Thu Dec 15 05:00:13 2022

// You have access to clk, rst, hlt as well as all the input variables and output states
//output state[0] pop;
//output state[1] push;
//output state[2] cr_data;



integer file, count, outfile,result;

reg [7:0] data_in [0:65535];
reg [7:0] data_out [0:65535];
reg [15:0] data_in_address;
reg [15:0] data_out_address;
wire [7:0] data;

initial begin
 result = 0;
 data_in_address = 0;
 data_out_address = 0;
 file = $fopen("text.txt","r");
 outfile = $fopen("parser.out","w");
 for (count = 0; count < 65536;count = count+1) begin
     data_in[count] = $fgetc(file);
     data_out[count] <= 0;
 end

end

assign valid = 1;
assign din7 = data_in[data_in_address][7];
assign din6 = data_in[data_in_address][6];
assign din5 = data_in[data_in_address][5];
assign din4 = data_in[data_in_address][4];
assign din3 = data_in[data_in_address][3];
assign din2 = data_in[data_in_address][2];
assign din1 = data_in[data_in_address][1];
assign din0 = data_in[data_in_address][0];
assign cr_wrap_around = cr_data;

assign data = cr_data ? 8'ha:data_in[data_in_address];

 always @(posedge clk) begin
  if (ready == 0) begin
  
  end
  else if (valid == 1) begin
       if (pop == 1) begin
             data_in_address <= data_in_address + 1;
       end 
       if (push == 1) begin
             data_out[data_out_address] <= data;
             $fwrite(outfile,"%c",data);
             data_out_address <= data_out_address + 1;
       end
  end 
  if (push & cr_data) result <= result + 1;
//if (data_in_address > 65530) begin
if (data_in_address > 2000) begin
   $fclose(outfile);
   $finish();
end


end


