<commit_before>/**
 * Copyright 2012 <NAME> <<EMAIL>>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.codesourcery.jasm16.ide.ui.views;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

import de.codesourcery.jasm16.Address;
import de.codesourcery.jasm16.Size;
import de.codesourcery.jasm16.WordAddress;
import de.codesourcery.jasm16.emulator.EmulationListener;
import de.codesourcery.jasm16.emulator.IEmulationListener;
import de.codesourcery.jasm16.emulator.IEmulator;
import de.codesourcery.jasm16.emulator.memory.MemUtils;
import de.codesourcery.jasm16.utils.Misc;

public class StackView extends AbstractView
{
    public static final String VIEW_ID = "stack-view";
    
    private JPanel panel;
    private final JTextArea textArea = new JTextArea();
    
    private IEmulator emulator;
    private Address startOfStack = Address.wordAddress( 0 );
    private int numberOfWordsToDump = 16;
    private boolean printASCII = true;
    
    private final IEmulationListener listener = new EmulationListener() {

        @Override
        public void afterCommandExecution(IEmulator emulator, int commandDuration)
        {
        	if ( ! isFullSpeedMode() ) {
        		refreshDisplay();
        	}
        }

        @Override
        public void afterReset(IEmulator emulator)
        {
        	if ( ! isFullSpeedMode() ) {
        		refreshDisplay();
        	}
        }

        @Override
        public void afterMemoryLoad(IEmulator emulator, Address startAddress, int lengthInBytes)
        {
        	if ( ! isFullSpeedMode() ) {
        		refreshDisplay();
        	}
        }

		@Override
		public void onStopHook(IEmulator emulator, Address previousPC, Throwable emulationError) {
			refreshDisplay();
		}

     };
     
    public StackView(IEmulator emulator) {
        setEmulator( emulator );
    }
    
    public StackView() {
    }
    
    @Override
    public void refreshDisplay() 
    {
        if ( emulator == null ) {
            return;
        }
        
        
        final int realStart;
        if ( startOfStack.getValue() - numberOfWordsToDump < 0 ) { // handle case where SP is at 0x0000
            realStart = ((int) ( WordAddress.MAX_ADDRESS+1 - numberOfWordsToDump) ) & 0xffff;            
        } else {
            realStart = (startOfStack.toWordAddress().getValue() - numberOfWordsToDump) & 0xffff;
        }
                
        final byte[] data = MemUtils.getBytes( emulator.getMemory() , 
        		Address.wordAddress( realStart ) , 
        		Size.words( numberOfWordsToDump) , 
        		true 
        );
        SwingUtilities.invokeLater( new Runnable() {

            @Override
            public void run()
            {
                final List<String> lines = Misc.toHexDumpLines( Address.wordAddress( realStart ), data, data.length , 1 , printASCII , true , true );
                Collections.reverse( lines ); // reverse lines => print stack from highest to lowest address
                
                Address current = Address.wordAddress( ( realStart + numberOfWordsToDump  -1 ) );
                
                StringBuilder result = new StringBuilder();
                for (Iterator<String> iterator = lines.iterator(); iterator.hasNext();) {
                    String line = iterator.next();
                    result.append( line );
                    
                    if ( current.equals( emulator.getCPU().getSP() ) ) {
                        result.append("  << ");
                    }
                    if ( iterator.hasNext() ) {
                        result.append("\n");
                    }
                    current = current.decrementByOne();
                }
                textArea.setText( result.toString() );                
            }
        });
    }
    
    public void setEmulator(IEmulator emulator)<commit_msg>fixed StackView not refreshing after SP changed<commit_after>/**
 * Copyright 2012 <NAME> <<EMAIL>>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.codesourcery.jasm16.ide.ui.views;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

import de.codesourcery.jasm16.Address;
import de.codesourcery.jasm16.Size;
import de.codesourcery.jasm16.WordAddress;
import de.codesourcery.jasm16.emulator.EmulationListener;
import de.codesourcery.jasm16.emulator.IEmulationListener;
import de.codesourcery.jasm16.emulator.IEmulator;
import de.codesourcery.jasm16.emulator.memory.MemUtils;
import de.codesourcery.jasm16.utils.Misc;

public class StackView extends AbstractView
{
    public static final String VIEW_ID = "stack-view";
    
    private JPanel panel;
    private final JTextArea textArea = new JTextArea();
    
    private IEmulator emulator;
    private int numberOfWordsToDump = 16;
    private boolean printASCII = true;
    
    private final IEmulationListener listener = new EmulationListener() {

        @Override
        public void afterCommandExecution(IEmulator emulator, int commandDuration)
        {
        	if ( ! isFullSpeedMode() ) {
        		refreshDisplay();
        	}
        }

        @Override
        public void afterReset(IEmulator emulator)
        {
        	if ( ! isFullSpeedMode() ) {
        		refreshDisplay();
        	}
        }

        @Override
        public void afterMemoryLoad(IEmulator emulator, Address startAddress, int lengthInBytes)
        {
        	if ( ! isFullSpeedMode() ) {
        		refreshDisplay();
        	}
        }

		@Override
		public void onStopHook(IEmulator emulator, Address previousPC, Throwable emulationError) {
			refreshDisplay();
		}

     };
     
    public StackView(IEmulator emulator) {
        setEmulator( emulator );
    }
    
    public StackView() {
    }
    
    @Override
    public void refreshDisplay() 
    {
        if ( emulator == null ) {
            return;
        }
        
        Address startOfStack = emulator.getCPU().getSP();
        final int realStart = (startOfStack.toWordAddress().getValue() - numberOfWordsToDump + 1 ) & 0xffff;
                
        final byte[] data = MemUtils.getBytes( emulator.getMemory() , 
        		Address.wordAddress( realStart ) , 
        		Size.words( numberOfWordsToDump) , 
        		true 
        );
        SwingUtilities.invokeLater( new Runnable() {

            @Override
            public void run()
            {
                final List<String> lines = Misc.toHexDumpLines( Address.wordAddress( realStart ), data, data.length , 1 , printASCII , true , true );
                Collections.reverse( lines ); // reverse lines => print stack from highest to lowest address
                
                Address current = Address.wordAddress( ( realStart + numberOfWordsToDump  -1 ) & 0xffff );
                
                StringBuilder result = new StringBuilder();
                for (Iterator<String> iterator = lines.iterator(); iterator.hasNext();) {
                    String line = iterator.next();
                    result.append( line );
                    
                    if ( current.equals( emulator.getCPU().getSP() ) ) {
                        result.append("  << ");
                    }
                    if ( iterator.hasNext() ) {
                        result.append("\n");
                    }
                    current = current.decrementByOne();
                }
                textArea.setText( result.toString() );                
            }
        });
    }
    
    public void setEmulator(IEmulator emulator)
{'fraction_non_alphanumeric': 0.0893861029907803, 'fraction_numerical': 0.008207780526197436, 'mean_word_length': 2.0939130434782607, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7589507', 'n_tokens_mistral': 2481, 'n_tokens_neox': 2359, 'n_words': 613}