<commit_before>//
//  device-time.h
//

#ifndef rv_device_htif_h
#define rv_device_htif_h

namespace riscv {

	/* HTIF MMIO device */

	template <typename P>
	struct htif_mmio_device : memory_segment<typename P::ux>
	{
		typedef typename P::ux UX;
		typedef std::shared_ptr<console_device<P>> console_device_ptr;

		enum {
			total_size = sizeof(u64) * 2
		};

		P &proc;

		/* HTIF registers */

		u64 htif_tohost;
		u64 htif_fromhost;

		console_device_ptr console;

		/* HTIF constructor */

		htif_mmio_device(P &proc, UX mpa, console_device_ptr console) :
			memory_segment<UX>("HTIF", mpa, /*uva*/0, /*size*/total_size,
				pma_type_io | pma_prot_read | pma_prot_write),
			proc(proc),
			htif_tohost(0),
			htif_fromhost(0),
			console(console)
		{}

		/* HTIF interface */

		void print_registers()
		{
			debug("htif_mmio:htif_tohost      %llu", htif_tohost);
			debug("htif_mmio:htif_fromhost    %llu", htif_fromhost);
		}

		void handle_output()
		{
			if (htif_tohost == 1) {
				proc.raise(P::internal_cause_poweroff, proc.pc);
			}
			u8 device = htif_tohost >> 56;
			u8 cmd = (htif_tohost >> 48) & 0xff;
			if (device == 1 && cmd == 1) {
				u8 c = htif_tohost & 0xff;
				console->write_char(c);
			}
		}

		void handle_input()
		{
			htif_fromhost = 0;
			if (console->has_char()) {
				htif_fromhost = ((uint64_t)1 << 56) | ((uint64_t)0 << 48) | console->read_char();
			}
		}

		/* HTIF MMIO */<commit_msg>Clear HTIF tohost register after console character output<commit_after>//
//  device-time.h
//

#ifndef rv_device_htif_h
#define rv_device_htif_h

namespace riscv {

	/* HTIF MMIO device */

	template <typename P>
	struct htif_mmio_device : memory_segment<typename P::ux>
	{
		typedef typename P::ux UX;
		typedef std::shared_ptr<console_device<P>> console_device_ptr;

		enum {
			total_size = sizeof(u64) * 2
		};

		P &proc;

		/* HTIF registers */

		u64 htif_tohost;
		u64 htif_fromhost;

		console_device_ptr console;

		/* HTIF constructor */

		htif_mmio_device(P &proc, UX mpa, console_device_ptr console) :
			memory_segment<UX>("HTIF", mpa, /*uva*/0, /*size*/total_size,
				pma_type_io | pma_prot_read | pma_prot_write),
			proc(proc),
			htif_tohost(0),
			htif_fromhost(0),
			console(console)
		{}

		/* HTIF interface */

		void print_registers()
		{
			debug("htif_mmio:htif_tohost      %llu", htif_tohost);
			debug("htif_mmio:htif_fromhost    %llu", htif_fromhost);
		}

		void handle_output()
		{
			if (htif_tohost == 1) {
				proc.raise(P::internal_cause_poweroff, proc.pc);
			}
			u8 device = htif_tohost >> 56;
			u8 cmd = (htif_tohost >> 48) & 0xff;
			if (device == 1 && cmd == 1) {
				u8 c = htif_tohost & 0xff;
				console->write_char(c);
				htif_tohost = 0;
			}
		}

		void handle_input()
		{
			htif_fromhost = 0;
			if (console->has_char()) {
				htif_fromhost = ((uint64_t)1 << 56) | ((uint64_t)0 << 48) | console->read_char();
			}
		}

		/* HTIF MMIO */
{'fraction_non_alphanumeric': 0.14124100102845388, 'fraction_numerical': 0.02296880356530682, 'mean_word_length': 3.2974963181148746, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 27, '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': '4515216', 'n_tokens_mistral': 1429, 'n_tokens_neox': 1177, 'n_words': 225}