• Advertisement
Sign in to follow this  

Getting overflow in vb.net

This topic is 3671 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

    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
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
Sign in to follow this  

  • Advertisement