<commit_before>/*
 * plio.c - busy-wait I/O routines for diagnosis
 *
 * Specific to the TS-7200 ARM evaluation board
 *
 */

#include <ts7200.h>
#include <plio.h>
#include <bwio.h>

static char *buffer;
static unsigned int *buffer_send_index;
static unsigned int *buffer_save_index;

// static unsigned int max_send_index = 0;
// static unsigned int max_save_index = 0;
static unsigned int total_send[CHANNEL_COUNT];
static unsigned int total_save[CHANNEL_COUNT];

void plstat() {
	int i = 0;
	for(i = 0; i < CHANNEL_COUNT; i++) {
		bwprintf( COM2, "Channel #%d Send total: 0x%x\n", i, total_send[i]);
		bwprintf( COM2, "Channel #%d Save total: 0x%x\n", i, total_save[i]);
	}
	return;
}

void plbootstrap(char *buf_array, unsigned int *buf_send_array, unsigned int *buf_save_array) {
	buffer = buf_array;
	buffer_send_index = buf_send_array;
	buffer_save_index = buf_save_array;
	
	// max_send_index = 0;
	// max_save_index = 0;
	// plstat();
	
	int i, j;
	for(i = 0; i < CHANNEL_COUNT; i++) {
		for(j = 0; j < OUTPUT_BUFFER_SIZE; j++) {
			buffer[(i * OUTPUT_BUFFER_SIZE) + j] = '\0';
		}
		buffer_send_index[i] = 0;
		buffer_save_index[i] = 0;
		
		total_send[i] = 0;
		total_save[i] = 0;
	}
	
	// bwprintf(COM2, "BOOTSTRAP buffer: 0x%x, send_index: 0x%x, save_index: 0x%x\n", buffer, buffer_send_index, buffer_save_index);
	return;
}

void plflush( int channel ) {
	while(plsend(channel) != 0);
}

int plsend( int channel ) {
	if(channel != COM1 && channel != COM2) return -1;
	
	// If something is waiting to be sent
	if(buffer_send_index[channel] != buffer_save_index[channel]) {
		
		unsigned int actual_index = (channel * OUTPUT_BUFFER_SIZE) + buffer_send_index[channel];
		char c = buffer[actual_index];
		int *flags;
		char *data;
		
		switch( channel ) {
			case COM1:
				flags = (int *)( UART1_BASE + UART_FLAG_OFFSET );
				data = (char *)( UART1_BASE + UART_DATA_OFFSET );
				// If COM1 UART not CTS, return
				if( !( *flags & CTS_MASK ) ) return 3;
				break;
			case COM2:
				flags = (int *)( UART2_BASE + UART_FLAG_OFFSET );
				data = (char *)( UART2_BASE + UART_DATA_OFFSET );
				break;
			default:
				return -1;
				break;
		}
		// If UART FIFO full, return
		if( ( *flags & TXFF_MASK ) ) return 2;
		
		*data = c;
		buffer[actual_index] = '\0';
		
		// Stat data
		// if(buffer_send_index[channel] > max_send_index) {<commit_msg>Do not send to COM1 if Receive Buffer is not empty<commit_after>/*
 * plio.c - busy-wait I/O routines for diagnosis
 *
 * Specific to the TS-7200 ARM evaluation board
 *
 */

#include <ts7200.h>
#include <plio.h>
#include <bwio.h>

static char *buffer;
static unsigned int *buffer_send_index;
static unsigned int *buffer_save_index;

// static unsigned int max_send_index = 0;
// static unsigned int max_save_index = 0;
static unsigned int total_send[CHANNEL_COUNT];
static unsigned int total_save[CHANNEL_COUNT];

void plstat() {
	int i = 0;
	for(i = 0; i < CHANNEL_COUNT; i++) {
		bwprintf( COM2, "Channel #%d Send total: 0x%x\n", i, total_send[i]);
		bwprintf( COM2, "Channel #%d Save total: 0x%x\n", i, total_save[i]);
	}
	return;
}

void plbootstrap(char *buf_array, unsigned int *buf_send_array, unsigned int *buf_save_array) {
	buffer = buf_array;
	buffer_send_index = buf_send_array;
	buffer_save_index = buf_save_array;
	
	// max_send_index = 0;
	// max_save_index = 0;
	// plstat();
	
	int i, j;
	for(i = 0; i < CHANNEL_COUNT; i++) {
		for(j = 0; j < OUTPUT_BUFFER_SIZE; j++) {
			buffer[(i * OUTPUT_BUFFER_SIZE) + j] = '\0';
		}
		buffer_send_index[i] = 0;
		buffer_save_index[i] = 0;
		
		total_send[i] = 0;
		total_save[i] = 0;
	}
	
	// bwprintf(COM2, "BOOTSTRAP buffer: 0x%x, send_index: 0x%x, save_index: 0x%x\n", buffer, buffer_send_index, buffer_save_index);
	return;
}

void plflush( int channel ) {
	while(plsend(channel) != 0);
}

int plsend( int channel ) {
	if(channel != COM1 && channel != COM2) return -1;
	
	// If something is waiting to be sent
	if(buffer_send_index[channel] != buffer_save_index[channel]) {
		
		unsigned int actual_index = (channel * OUTPUT_BUFFER_SIZE) + buffer_send_index[channel];
		char c = buffer[actual_index];
		int *flags;
		char *data;
		
		switch( channel ) {
			case COM1:
				flags = (int *)( UART1_BASE + UART_FLAG_OFFSET );
				data = (char *)( UART1_BASE + UART_DATA_OFFSET );
				// If COM1 UART not CTS, return
				if( !( *flags & CTS_MASK ) || !( *flags & RXFE_MASK )) return 3;
				break;
			case COM2:
				flags = (int *)( UART2_BASE + UART_FLAG_OFFSET );
				data = (char *)( UART2_BASE + UART_DATA_OFFSET );
				break;
			default:
				return -1;
				break;
		}
		// If UART FIFO full, return
		if( ( *flags & TXFF_MASK ) ) return 2;
		
		*data = c;
		buffer[actual_index] = '\0';
		
		// Stat data
		// if(buffer_send_index[channel] > max_send_index) {
{'fraction_non_alphanumeric': 0.13926701570680627, 'fraction_numerical': 0.018638743455497382, 'mean_word_length': 3.627906976744186, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '279181', 'n_tokens_mistral': 2035, 'n_tokens_neox': 1816, 'n_words': 472}