Sign in to follow this  
ed209

Getting overflow in vb.net

Recommended Posts

ed209    122
I'm working on a chip 8 emulator and i'm trying to read in a rom into memory but I keep getting an overflow. I'm reading one character at a time into an array of bytes.
Public Class Form1
    Dim ram(&HFFF) As Byte
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ReadFile()

    End Sub

    Sub ReadFile()
        Dim ofile As IO.File
        Dim oRead As IO.StreamReader
        Dim i As Integer
        i = 0

        oRead = ofile.OpenText("c:\roms/c8games/UFO")
        While Not oRead.EndOfStream

            ram(i) = oRead.Read()

            i += 1
        End While
     
     End Sub
End Class
[Edited by - ed209 on March 3, 2008 9:31:42 AM]

Share this post


Link to post
Share on other sites
benryves    1999
A number of problems here; you open the stream as text (the files you are reading are binary); you do not dispose the opened stream; no bounds checking on i.

Rather than correct those, there are methods in the class library that make your life easier, and so here's a simpler version! [smile]

Sub ReadFile(ByVal filename As String)

' Clear the contents of Ram.
Array.Clear(Ram, 0, Ram.Length)

' Now read the entire source file into an array of bytes.
Dim ProgramFile As Byte() = IO.File.ReadAllBytes(filename)

' The largest program file we can fit in memory is between address &H200 and &HFFF.
' This is a maximum of &HE00 bytes, which is 3584.
' In case the specified file is too large, resize it:
If ProgramFile.Length > 3584 Then
Array.Resize(ProgramFile, 3584)
End If

' Finally, we need to copy our program to &H200 in memory.
Array.Copy(ProgramFile, 0, Ram, &H200, ProgramFile.Length)

End Sub

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this