<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.IO;
using System.Windows.Threading;

namespace Sharpness
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        // 6502 ---------------------------------------------
        // Acumulador
        public byte A;

        // Registradores
        public byte X;
        public byte Y;

        // Flags
        public byte P;

        // Stack Pointer
        public byte S;

        // Program Counter
        public ushort PC;

        // Memória Principal
        public byte[] mem = new byte[0x10000];

        // Flags
        public bool N, V, B, D, I, Z, C;

        // ROM ----------------------------------------------
        string NesConfirm;
        byte formatConfirm;
        // Tamanho da ROM PRG (em blocos de 16kb)
        byte PRGSize;

        // Tamanho da ROM CHR (em blocos de 8kb)
        byte CHRSize;

        // Tamango da ROM CHR (em blocos de 8kb; 0 = 8kb)
        byte PRGRamSize;

        // Tipo de TV (0 = NTSC, 1 = PAL)
        byte TVSystem;

        // Auxiliar para decodificação do Opcode com mais de um argumento
        ushort argop;

        DispatcherTimer timer;
        Log logwin;

        //Tells whether log window is visible or not
        bool logOpen = false;

        public MainWindow()
        {
            InitializeComponent();

            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(100);
            timer.Tick += timer_Tick;
            
        }

        private void Logwin_resetBit(bool obj)
        {
            //Update from another thread
            logOpen = false;
        }

        private void MainWindow1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (logOpen)
            {
                logwin.Close();
            }
        }

        void timer_Tick(object sender, EventArgs e)
        {
            Dispatcher.BeginInvoke(new Action(() =>
            {
                if (logOpen)
                {
                    logwin.LogMessage("Opcode: " + mem[PC].ToString("X2"));
                    logwin.LogMessage("PC: " + PC.ToString("X4"));
                }
                    
                //txtDebug.Text += "PC: " + PC.ToString("X4") + "\n";
                Emulate();
            }));
        }

        private void Window(object sender, EventArgs e)
        {

        }


        private void viewLog_Checked(object sender, RoutedEventArgs e)
        {
            logwin = new Log();
            logwin.Show();
            Application curApp = Application.Current;
            Window mainWindow = curApp.MainWindow;
            logwin.Left = mainWindow.Left + (mainWindow.ActualWidth);
            logwin.Top = mainWindow.Top;
            logOpen = true;
            logwin.resetBit += Logwin_resetBit;
        }

        private void runCheckbox_Checked(object sender, RoutedEventArgs e)
        {
            timer.Start();
        }

        private void runCheckbox_Unchecked(object sender, RoutedEventArgs e)
        {
            timer.Stop();
        }

        private void viewLog_Unchecked(object sender, RoutedEventArgs e)
        {
            logwin.Close();
            logOpen = false;
        }

        private void Window_Loaded(object sender, EventArgs e)
        {
            if (logOpen)
            {
                Application curApp = Application.Current;
                Window mainWindow = curApp.MainWindow;
                logwin.Left = mainWindow.Left + (mainWindow.ActualWidth);
                logwin.Top = mainWindow.Top;
            }
        }

        private void btnLoad_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.RestoreDirectory = true;
            fileDialog.ShowDialog();

            //Review code below
            if (true)
            {
                if (File.Exists(fileDialog.FileName))
                {
                    using (BinaryReader reader = new BinaryReader(File.Open(fileDialog.FileName, FileMode.Open)))
                    {
                        byte[] header = reader.ReadBytes(16);
                        NesConfirm = header[0].ToString() + header[1].ToString() + header[2].ToString();
                        formatConfirm = header[3];
                        PRGSize = header[4];
                        CHRSize = header[5];
                        byte romControl1 = header[6];
                        byte romControl2 = header[7];
                        short romControl = (short)((romControl1 >> 2) + header[7]);
                        PRGRamSize = header[8];
                        TVSystem = header[9];

                        string info = "ROM Info" + "\n";
                        info += "Raw text: " + NesConfirm + "\n";
                        info += "Format value: " + formatConfirm.ToString("X2") + "\n";
                        info += "RomControl: " + romControl.ToString("X2") + "\n";
                        info += "PRG Size: " + 16384 * PRGSize + " bytes\n";
                        info += "CHR Size: " + 8192 * CHRSize + " bytes\n";
                        info += "TV System: " + (TVSystem == 1 ? "PAL" : "NTSC") + "\n";

                        byte[] PRG = reader.ReadBytes(16384 * PRGSize);

                        InitVM();

                        if (PRGSize == 1)
                        {
                            for (int i = 0; i < PRG.Length; i++)
                            {
                                mem[0x8000 + i] = PRG[i];
                                mem[0xC000 + i] = PRG[i];
                            }
                            info += "PRG Size = " + PRGSize + "; Mirroring PRG...";
                        }
                        else if (PRGSize == 2)<commit_msg>Quick fix<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.IO;
using System.Windows.Threading;

namespace Sharpness
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        // 6502 ---------------------------------------------
        // Acumulador
        public byte A;

        // Registradores
        public byte X;
        public byte Y;

        // Flags
        public byte P;

        // Stack Pointer
        public byte S;

        // Program Counter
        public ushort PC;

        // Memória Principal
        public byte[] mem = new byte[0x10000];

        // Flags
        public bool N, V, B, D, I, Z, C;

        // ROM ----------------------------------------------
        string NesConfirm;
        byte formatConfirm;
        // Tamanho da ROM PRG (em blocos de 16kb)
        byte PRGSize;

        // Tamanho da ROM CHR (em blocos de 8kb)
        byte CHRSize;

        // Tamango da ROM CHR (em blocos de 8kb; 0 = 8kb)
        byte PRGRamSize;

        // Tipo de TV (0 = NTSC, 1 = PAL)
        byte TVSystem;

        // Auxiliar para decodificação do Opcode com mais de um argumento
        ushort argop;

        DispatcherTimer timer;
        Log logwin;

        //Tells whether log window is visible or not
        bool logOpen = false;

        public MainWindow()
        {
            InitializeComponent();

            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(100);
            timer.Tick += timer_Tick;
            
        }

        private void Logwin_resetBit(bool obj)
        {
            //Update from another thread
            logOpen = false;
        }

        private void MainWindow1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (logOpen)
            {
                logwin.Close();
            }
        }

        void timer_Tick(object sender, EventArgs e)
        {
            Dispatcher.BeginInvoke(new Action(() =>
            {
                if (logOpen)
                {
                    logwin.LogMessage("Opcode: " + mem[PC].ToString("X2"));
                    logwin.LogMessage("PC: " + PC.ToString("X4"));
                }
                    
                //txtDebug.Text += "PC: " + PC.ToString("X4") + "\n";
                Emulate();
            }));
        }

        private void Window(object sender, EventArgs e)
        {

        }


        private void viewLog_Checked(object sender, RoutedEventArgs e)
        {
            logwin = new Log();
            logwin.Show();
            Application curApp = Application.Current;
            Window mainWindow = curApp.MainWindow;
            logwin.Left = mainWindow.Left + (mainWindow.ActualWidth);
            logwin.Top = mainWindow.Top;
            logOpen = true;
            logwin.resetBit += Logwin_resetBit;
        }

        private void runCheckbox_Checked(object sender, RoutedEventArgs e)
        {
            timer.Start();
        }

        private void runCheckbox_Unchecked(object sender, RoutedEventArgs e)
        {
            timer.Stop();
        }

        private void viewLog_Unchecked(object sender, RoutedEventArgs e)
        {
            logwin.Close();
            logOpen = false;
        }

        private void Window_Loaded(object sender, EventArgs e)
        {
            if (logOpen)
            {
                Application curApp = Application.Current;
                Window mainWindow = curApp.MainWindow;
                logwin.Left = mainWindow.Left + (mainWindow.ActualWidth);
                logwin.Top = mainWindow.Top;
            }
        }

        private void btnLoad_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.RestoreDirectory = true;
            fileDialog.ShowDialog();

            //Review code below
            if (true)
            {
                if (File.Exists(fileDialog.FileName))
                {
                    using (BinaryReader reader = new BinaryReader(File.Open(fileDialog.FileName, FileMode.Open)))
                    {
                        byte[] header = reader.ReadBytes(16);
                        NesConfirm = header[0].ToString() + header[1].ToString() + header[2].ToString();
                        formatConfirm = header[3];
                        PRGSize = header[4];
                        CHRSize = header[5];
                        byte romControl1 = header[6];
                        byte romControl2 = header[7];
                        short romControl = (short)((header[7] >> 2) + header[6]);
                        PRGRamSize = header[8];
                        TVSystem = header[9];

                        string info = "ROM Info" + "\n";
                        info += "Raw text: " + NesConfirm + "\n";
                        info += "Format value: " + formatConfirm.ToString("X2") + "\n";
                        info += "RomControl: " + romControl.ToString("X2") + "\n";
                        info += "PRG Size: " + 16384 * PRGSize + " bytes\n";
                        info += "CHR Size: " + 8192 * CHRSize + " bytes\n";
                        info += "TV System: " + (TVSystem == 1 ? "PAL" : "NTSC") + "\n";

                        byte[] PRG = reader.ReadBytes(16384 * PRGSize);

                        InitVM();

                        if (PRGSize == 1)
                        {
                            for (int i = 0; i < PRG.Length; i++)
                            {
                                mem[0x8000 + i] = PRG[i];
                                mem[0xC000 + i] = PRG[i];
                            }
                            info += "PRG Size = " + PRGSize + "; Mirroring PRG...";
                        }
                        else if (PRGSize == 2)
{'fraction_non_alphanumeric': 0.11220441199809554, 'fraction_numerical': 0.011585462624980163, 'mean_word_length': 1.2586021505376344, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '7030349', 'n_tokens_mistral': 3423, 'n_tokens_neox': 3167, 'n_words': 814}